Improvements efcore relation mappings, UserModels, etc...

This commit is contained in:
jozsef.b@aycode.com 2023-12-19 11:42:26 +01:00
parent 733fdd2484
commit 0b0aa70e5c
11 changed files with 25 additions and 15 deletions

View File

@ -15,7 +15,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0">

View File

@ -54,6 +54,8 @@ namespace TIAM.Database.Test
[TestMethod]
[DataRow("540271F6-C604-4C16-8160-D5A7CAFEDF00")]
[DataRow("dcf451d2-cc4c-4ac2-8c1f-da00041be1fd")]
[DataRow("ac612aa8-863b-4b4f-9d63-f5d261b5c5f9")]
public async Task SerializeUserModelDto_ReturnDeserializedUser_WhenUserAndRelationsExists(string userIdString)
{
var userId = Guid.Parse(userIdString);
@ -72,7 +74,9 @@ namespace TIAM.Database.Test
Assert.IsNotNull(userModel);
Assert.IsNotNull(userModel.UserDto);
Assert.IsNotNull(userModel.Profile);
if (userId != Guid.Parse("540271F6-C604-4C16-8160-D5A7CAFEDF00")) return; //csak az "540271F6.."-nek van product-ja! - J.
Assert.IsTrue(userModel.Products.Count > 0);
Assert.IsTrue(userModel.UserProductMappings.Count > 0);
}

View File

@ -60,7 +60,7 @@ namespace TIAM.Database.DbContexts.ServiceProviders
base.OnModelCreating(modelBuilder);
new UserEntityTypeDefaultConfiguration().Configure(modelBuilder.Entity<User>());
new UserProductMappingEntityTypeDefaultConfiguration().Configure(modelBuilder.Entity<UserProductMapping>());
//new UserProductMappingEntityTypeDefaultConfiguration().Configure(modelBuilder.Entity<UserProductMapping>());
new ProductEntityTypeDefaultConfiguration().Configure(modelBuilder.Entity<Product>());
}

View File

@ -53,8 +53,9 @@ namespace TIAM.Database.DbContexts.Users
base.OnModelCreating(modelBuilder);
new UserEntityTypeDefaultConfiguration().Configure(modelBuilder.Entity<User>());
modelBuilder.Entity<Product>().BuildProductToServiceProviderRelation();
new ProductEntityTypeDefaultConfiguration().Configure(modelBuilder.Entity<Product>());
//modelBuilder.Entity<Product>().BuildProductToServiceProviderRelation();
//new UserProductMappingEntityTypeDefaultConfiguration().Configure(modelBuilder.Entity<UserProductMapping>());
}
}

View File

@ -22,12 +22,12 @@ public static class UserDbSetExtensions
=> ctx.UsersWithProductRelations(autoInclude).FirstOrDefault(x => x.EmailAddress == email);
public static UserModelDto? GetUserModelDtoById(this IUserDbSet ctx, Guid userId)
=> ctx.GetUsersById(userId).Select(x => new UserModelDto(x, x.Profile, x.UserProductMappings, x.Products)).FirstOrDefault();
=> ctx.GetUsersById(userId).Select(user => new UserModelDto(user)).FirstOrDefault();
public static UserModelDto? GetUserModelDtoByEmail(this IUserDbSet ctx, string email)
=> ctx.GetUsersByEmail(email).Select(x => new UserModelDto(x, x.Profile, x.UserProductMappings, x.Products)).FirstOrDefault();
=> ctx.GetUsersByEmail(email).Select(user => new UserModelDto(user)).FirstOrDefault();
public static IQueryable<UserModelDto> GetAllUsersModelDto(this IUserDbSet ctx)
=> ctx.Users.Select(x => new UserModelDto(x, x.Profile, x.UserProductMappings, x.Products));
=> ctx.Users.Select(user => new UserModelDto(user));
}

View File

@ -31,6 +31,11 @@ public static class UserEntityTypeBuilderExtensions
modelBuilder.Navigation(e => e.UserProductMappings).AutoInclude(autoInclude);
}
public static void BuildUserProfileRelation<TUser, TProfile>(this EntityTypeBuilder<TUser> modelBuilder) where TUser : class, IUserBase<TProfile> where TProfile : class, IAcProfileBase
{
modelBuilder.Navigation(e => e.Profile).AutoInclude(true);
}
#endregion User
#region UserProductMapping

View File

@ -21,6 +21,7 @@ public class UserEntityTypeDefaultConfiguration : IAcEntityTypeConfiguration<Use
{
//base.Configure(builder);
builder.BuildUserProfileRelation<User, Profile>();
builder.BuildUserToUserProductMappingRelation();
builder.BuildUserToServiceProviderRelation<User, Profile>();
}

View File

@ -14,14 +14,14 @@ public class UserModelDto : AcUserModelDtoBase<UserDto, Profile, ProfileDto>
public List<Product> Products = new();
public UserModelDto(){}
public UserModelDto(User user, Profile profile, List<UserProductMapping> userProductMappings, List<Product> products) : base(user, profile)
public UserModelDto(User user) : base(user)
{
foreach (var product in products)
foreach (var product in user.Products)
{
Products.Add(new Product(product.Id, product.ServiceProviderId, product.ProductType, product.Name, product.Description, product.Price, product.JsonDetails));
}
foreach (var userProduct in userProductMappings)
foreach (var userProduct in user.UserProductMappings)
{
UserProductMappings.Add(new UserProductMapping(userProduct.Id, userProduct.UserId, userProduct.ProductId));
}

View File

@ -13,7 +13,7 @@ else
@foreach (var dest in Users)
{
<p>
@(dest.Profile == null ? dest.UserDto.Id.ToString() : dest.Profile.Name)
@(dest.Profile.Name.IsNullOrWhiteSpace() ? dest.Id.ToString() : dest.Profile.Name)
</p>
}
</div>

View File

@ -38,7 +38,6 @@ namespace TIAMWebApp.Client.Services
public async Task<UserSessionModel> IsLoggedInAsync(Guid id)
{
UserSessionModel user = null;
//api call to get user
var userModelDto = await GetUserByIdAsync(id);
@ -56,7 +55,7 @@ namespace TIAMWebApp.Client.Services
logToBrowserConsole.LogToBC($"{hasProperties.Count} properties found");
//create user session model
user = new UserSessionModel(userModelDto.Id, UserType.User, userModelDto.Profile?.Name, hasProperties, 1);
var user = new UserSessionModel(userModelDto.Id, UserType.User, userModelDto.Profile?.Name, hasProperties, 1);
return user;
}
else

View File

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GoogleApi" Version="5.2.4" />
<PackageReference Include="GoogleApi" Version="5.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />