Skip to content

Commit

Permalink
[ODS-6250] Update to Virtual Models for Admin.DataAccess (#945)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Fuqua <[email protected]>
  • Loading branch information
2 people authored and axelmarquezh committed Jun 13, 2024
1 parent a897529 commit b0df7f6
Show file tree
Hide file tree
Showing 28 changed files with 1,309 additions and 790 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ namespace EdFi.Admin.DataAccess.Contexts
public class SqlServerUsersContext : UsersContext
{
public SqlServerUsersContext(DbContextOptions options) : base(options) { }

}
}
5 changes: 5 additions & 0 deletions Application/EdFi.Admin.DataAccess/Contexts/UsersContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
r =>
r.HasOne<ApiClient>().WithMany().HasForeignKey("ApiClientId"));

modelBuilder.Entity<Application>()
.HasMany(a => a.Profiles)
.WithMany(a => a.Applications)
.UsingEntity("ProfileApplications");

modelBuilder.UseUnderscoredFkColumnNames();

modelBuilder.Model.FindEntityTypes(typeof(ApiClient)).First().GetProperty("CreatorOwnershipTokenId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EdFi.Suite3.Common" Version="6.1.19" />
<PackageReference Include="EdFi.Suite3.Common" Version="6.2.2" />
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.1" />
Expand Down
21 changes: 19 additions & 2 deletions Application/EdFi.Admin.DataAccess/EdFi.Admin.DataAccess.sln
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.960
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EdFi.Admin.DataAccess", "EdFi.Admin.DataAccess.csproj", "{D2A9D9A2-A4DB-4E65-82C3-C7E9F6DBA1E2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EdFi.Admin.DataAccess.UnitTests", "..\..\tests\EdFi.Admin.DataAccess.UnitTests\EdFi.Admin.DataAccess.UnitTests.csproj", "{7BEC38BB-C538-40C8-83BA-C45CE192A088}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EdFi.Common", "..\EdFi.Common\EdFi.Common.csproj", "{92A9A0CD-0AE6-433D-9673-EB88E52D88C4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EdFi.Admin.DataAccess.IntegrationTests", "..\..\tests\EdFi.Admin.DataAccess.IntegrationTests\EdFi.Admin.DataAccess.IntegrationTests.csproj", "{DE3AD0EB-ECDF-47EB-AAE4-C4CA65674652}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test Support", "Test Support", "{A0B2E4A0-067E-4558-9412-79A4EA5E5D30}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EdFi.TestFixture", "..\..\tests\EdFi.TestFixture\EdFi.TestFixture.csproj", "{AF2CCF4F-21AA-4680-B2B9-2A96796933AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,10 +33,21 @@ Global
{92A9A0CD-0AE6-433D-9673-EB88E52D88C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92A9A0CD-0AE6-433D-9673-EB88E52D88C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92A9A0CD-0AE6-433D-9673-EB88E52D88C4}.Release|Any CPU.Build.0 = Release|Any CPU
{DE3AD0EB-ECDF-47EB-AAE4-C4CA65674652}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE3AD0EB-ECDF-47EB-AAE4-C4CA65674652}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE3AD0EB-ECDF-47EB-AAE4-C4CA65674652}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE3AD0EB-ECDF-47EB-AAE4-C4CA65674652}.Release|Any CPU.Build.0 = Release|Any CPU
{AF2CCF4F-21AA-4680-B2B9-2A96796933AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF2CCF4F-21AA-4680-B2B9-2A96796933AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF2CCF4F-21AA-4680-B2B9-2A96796933AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF2CCF4F-21AA-4680-B2B9-2A96796933AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{AF2CCF4F-21AA-4680-B2B9-2A96796933AE} = {A0B2E4A0-067E-4558-9412-79A4EA5E5D30}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E7C6FBAA-92CE-4FEB-BFCC-F547FF6D40FD}
EndGlobalSection
Expand Down
11 changes: 8 additions & 3 deletions Application/EdFi.Admin.DataAccess/Models/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace EdFi.Admin.DataAccess.Models
/// Class representing EdFi client application information persisted in a data store.
/// A Client has a list of domains that are valid for access
/// </summary>
[Table("ApiClients")]
public class ApiClient
{
public ApiClient()
Expand Down Expand Up @@ -125,9 +126,12 @@ public string SandboxTypeName
public int? ActivationRetried { get; set; }

/// <summary>
/// Have a reference to OwnershipToken table ownershiptokenid for specific apiclient.
/// </summary>
public virtual OwnershipToken CreatorOwnershipTokenId { get; set; }
/// Have a reference to OwnershipToken table ownershiptokenid for specific apiclient.
/// </summary>
public virtual OwnershipToken CreatorOwnershipToken { get; set; }

[Column("CreatorOwnershipTokenId_OwnershipTokenId")]
public short? CreatorOwnershipTokenId { get; set; }

/// <summary>
/// Fully namespaced URI reference to the StudentIdentificationSystemDescriptor value to use for identification mapping
Expand All @@ -150,6 +154,7 @@ public string SandboxTypeName
/// EdOrg is the key, Domain Connection information is the value.
/// The end-user should never see the Data Connection information
/// </remarks>
[NotMapped]
public Dictionary<string, string> Domains { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ public class ApiClientOwnershipToken
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ApiClientOwnershipTokenId { get; set; }

[Required]
[Column("OwnershipToken_OwnershipTokenId")]
public OwnershipToken OwnershipToken { get; set; }
public virtual ApiClient ApiClient { get; set; }

[Required]
[Column("ApiClient_ApiClientId")]
public ApiClient ApiClient { get; set; }
public virtual OwnershipToken OwnershipToken { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace EdFi.Admin.DataAccess.Models
{
public sealed class ApplicationEducationOrganization
public class ApplicationEducationOrganization
{
public ApplicationEducationOrganization()
{
Expand All @@ -21,10 +21,10 @@ public ApplicationEducationOrganization()
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ApplicationEducationOrganizationId { get; set; }

public Application Application { get; set; }
public virtual Application Application { get; set; }

public int EducationOrganizationId { get; set; }

public ICollection<ApiClient> Clients { get; set; }
public virtual ICollection<ApiClient> Clients { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Guid Id
set { _id = value; }
}

public ApiClient ApiClient { get; set; }
public virtual ApiClient ApiClient { get; set; }

public DateTime Expiration { get; set; }

Expand Down
1 change: 1 addition & 0 deletions Application/EdFi.Admin.DataAccess/Models/OwnershipToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public OwnershipToken()
/// </summary>
[StringLength(50)]
public string Description { get; set; }
public virtual ICollection<ApiClient> ApiClients { get; set; }

public ICollection<ApiClient> Clients { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

namespace EdFi.Admin.DataAccess.Models
{
public sealed class VendorNamespacePrefix
public class VendorNamespacePrefix
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int VendorNamespacePrefixId { get; set; }

[Required]
public Vendor Vendor { get; set; }
public virtual Vendor Vendor { get; set; }

[Required]
[StringLength(255)]
Expand Down
36 changes: 17 additions & 19 deletions Application/EdFi.Admin.DataAccess/Repositories/ClientAppRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ public IEnumerable<User> GetUsers()
{
using (var context = _contextFactory.CreateContext())
{
return context.Users.Include(u => u.ApiClients.Select(ac => ac.Application))
.ToList();
return context.Users.Include(u => u.ApiClients).ThenInclude(ac => ac.Application).ToList();
}
}

Expand All @@ -127,7 +126,7 @@ public User GetUser(int userId)
using (var context = _contextFactory.CreateContext())
{
return
context.Users.Include(u => u.ApiClients.Select(ac => ac.Application))
context.Users.Include(u => u.ApiClients).ThenInclude(ac => ac.Application)
.FirstOrDefault(u => u.UserId == userId);
}
}
Expand All @@ -137,7 +136,7 @@ public User GetUser(string userName)
using (var context = _contextFactory.CreateContext())
{
return
context.Users.Include(u => u.ApiClients.Select(ac => ac.Application))
context.Users.Include(u => u.ApiClients).ThenInclude(a => a.Application)
.Include(u => u.Vendor)
.FirstOrDefault(x => x.Email == userName);
}
Expand All @@ -148,7 +147,7 @@ public void DeleteUser(User userProfile)
using (var context = _contextFactory.CreateContext())
{
var user =
context.Users.Include(u => u.ApiClients.Select(ac => ac.Application))
context.Users.Include(u => u.ApiClients).ThenInclude(ac => ac.Application)
.FirstOrDefault(x => x.UserId == userProfile.UserId);

if (user == null)
Expand All @@ -172,12 +171,14 @@ public ApiClient GetClient(string key)
{
using (var context = _contextFactory.CreateContext())
{
return context.Clients.Include(c => c.Application)
.Include(c => c.Application.Vendor)
.Include(c => c.Application.Vendor.VendorNamespacePrefixes)
.Include(c => c.Application.Profiles)
return context.Clients
.Include(c => c.Application)
.ThenInclude(c => c.Vendor)
.ThenInclude(c => c.VendorNamespacePrefixes)
.Include(c => c.Application)
.ThenInclude(c => c.Profiles)
.Include(c => c.ApplicationEducationOrganizations)
.Include(c => c.CreatorOwnershipTokenId)
.Include(c => c.CreatorOwnershipToken)
.FirstOrDefault(c => c.Key == key);
}
}
Expand All @@ -186,10 +187,12 @@ public async Task<ApiClient> GetClientAsync(string key)
{
using (var context = _contextFactory.CreateContext())
{
return await context.Clients.Include(c => c.Application)
.Include(c => c.Application.Vendor)
.Include(c => c.Application.Vendor.VendorNamespacePrefixes)
.Include(c => c.Application.Profiles)
return await context.Clients
.Include(c => c.Application)
.ThenInclude(c => c.Vendor)
.ThenInclude(c => c.VendorNamespacePrefixes)
.Include(c => c.Application)
.ThenInclude(c => c.Profiles)
.Include(c => c.ApplicationEducationOrganizations)
.Include(c => c.CreatorOwnershipTokenId)
.FirstOrDefaultAsync(c => c.Key == key);
Expand Down Expand Up @@ -220,11 +223,6 @@ public void DeleteClient(string key)
{
var client = context.Clients.First(x => x.Key == key);

// TODO SF: AA-518
// Assuming that this is used by Admin App, although that will not actually be clear
// until we are able to start testing Admin App thoroughly.
// Convert this to ANSI SQL for PostgreSql support and don't use a SqlParameter.
// Be sure to write integration tests in project EdFi.Ods.Admin.Models.IntegrationTests.
context.ExecuteSqlCommandAsync(
@"delete from dbo.ClientAccessTokens where ApiClient_ApiClientId = @p0; delete from dbo.ApiClients where ApiClientId = @p0",
client.ApiClientId).Wait();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Application FindOrCreateUpdatedDefaultSandboxApplication(int vendorId, Sa
{
var vendor = context.Vendors
.Where(x => x.VendorId == vendorId)
.Include(x => x.Applications.Select<Application, ICollection<ApplicationEducationOrganization>>(a => a.ApplicationEducationOrganizations))
.Include(x => x.Applications).ThenInclude(x => x.ApplicationEducationOrganizations)
.Single();

var defaultAppName = _configuration.GetSection("DefaultApplicationName").Value ?? "Default Sandbox Application";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using System;
using System.Configuration;
using System.Data.SqlClient;
using EdFi.Common.Extensions;
using Microsoft.Data.SqlClient;

namespace EdFi.Admin.DataAccess.Utils
{
Expand Down

This file was deleted.

Loading

0 comments on commit b0df7f6

Please sign in to comment.