This commit is contained in:
Adam 2024-06-25 18:49:34 +02:00
commit a843236736
6 changed files with 88 additions and 31 deletions

View File

@ -303,6 +303,10 @@
<Name>RegisterDataTierApplication</Name>
<Value>False</Value>
</PropertyElementName>
<PropertyElementName>
<Name>PerformIndexOperationsOnline</Name>
<Value>False</Value>
</PropertyElementName>
<PropertyElementName>
<Name>RebuildIndexesOfflineForDataPhase</Name>
<Value>False</Value>

View File

@ -201,8 +201,7 @@ namespace TIAM.Database.Test
userProductMapping = await Dal.GetUserProductMappingByIdAsync(userProductMappingId, true);
Assert.IsNotNull(userProductMapping);
//userProductMapping.Permissions = 1;
Assert.IsNotNull(await Dal.UpdateUserProductMappingAsync(userProductMappingId, 1));
userProductMapping = await Dal.GetUserProductMappingByIdAsync(userProductMappingId, true);
@ -224,8 +223,8 @@ namespace TIAM.Database.Test
#endregion UserProductMapping
#region Product
//[DataTestMethod]
//[DataRow(["e24f6942-2210-47d7-8660-ace0ef302bae", "8e6a4170-0e15-4f8a-bdd2-46f9dbc63b93", "540271F6-C604-4C16-8160-D5A7CAFEDF00", "49c7805b-d8cd-4308-b1c5-7a54e5ee6287"])]
[DataTestMethod]
[DataRow(["e24f6942-2210-47d7-8660-ace0ef302bae", "88b12e3d-bfec-462b-b1dc-1f4243ee7a00", "540271F6-C604-4C16-8160-D5A7CAFEDF00", "49c7805b-d8cd-4308-b1c5-7a54e5ee6287"])]
public async Task ProductCrudTest(string[] productIdCompanyIdUserIdUserToCompanyIdStrings)
{
var productId = Guid.Parse(productIdCompanyIdUserIdUserToCompanyIdStrings[0]);
@ -235,33 +234,42 @@ namespace TIAM.Database.Test
await Dal.RemoveProductAsync(productId); //kitöröljük a szemetet, ha korábbról bentmaradt - J.
var company = new Company(companyId, "Test unit product...", null);
var product = new Product(productId, companyId, ProductType.Hotel, "Test unit product...", "Test unit product description...", 25000, null);
product.AddUser(userId, 1);
Assert.IsTrue(await Dal.AddCompanyAsync(company));
Assert.IsNotNull(company);
Assert.IsTrue(await Dal.AddProductAsync(product));
Assert.IsNotNull(product);
company = await Dal.GetCompanyByIdAsync(companyId);
product = await Dal.GetProductByIdAsync(productId);
Assert.IsNotNull(company);
Assert.IsTrue(company.UserToServiceProviders.Count == 0);
Assert.IsTrue(company.Id == companyId);
Assert.IsNotNull(product);
Assert.IsNotNull(product.Profile);
Assert.IsNotNull(product.Profile.Address);
company.OwnerId = userId;
Assert.IsTrue(await Dal.UpdateCompanyAsync(company));
Assert.IsTrue(product.UserProductMappings.Any(x=>x.UserId == userId && x.ProductId == productId));
Assert.IsTrue(product.Id == productId);
company = await Dal.GetCompanyByIdAsync(companyId);
product.Price = 30000;
product.UserProductMappings[0].Permissions = 2;
Assert.IsTrue(await Dal.UpdateProductAsync(product));
Assert.IsNotNull(company);
Assert.IsNotNull(company.UserToServiceProviders);
Assert.IsTrue(company.UserToServiceProviders.Any(x=>x.UserId == userId && x.ServiceProviderId == companyId));
product = await Dal.GetProductByIdAsync(productId);
Assert.IsNotNull(product);
Assert.IsNotNull(product.UserProductMappings);
Assert.IsTrue((int)product.Price == 30000);
Assert.IsTrue(product.UserProductMappings[0].Permissions == 2);
company.CommissionPercent = 5;
Assert.IsTrue(await Dal.UpdateCompanyAsync(company));
var addressId = product.Profile.AddressId;
Assert.IsTrue(await Dal.RemoveProductAsync(product)); //mielőbb kitöröljük, h ne maradjon szemét a db-ben - J.
Assert.IsTrue(await Dal.RemoveCompanyAsync(company)); //mielőbb kitöröljük, h ne maradjon szemét a db-ben - J.
Assert.IsNull(await Dal.GetProfileByIdAsync(product.ProfileId));
Assert.IsNull(await Dal.GetAddressByIdAsync(addressId));
Assert.IsTrue((await Dal.GetUserProductMappingsByProductIdAsync(productId)).Count == 0);
company = await Dal.GetCompanyByIdAsync(companyId);
Assert.IsNull(company); //a korábbi törlés miatt NULL kell legyen - J.
product = await Dal.GetProductByIdAsync(productId);
Assert.IsNull(product); //a korábbi törlés miatt NULL kell legyen - J.
}
#endregion Product
@ -374,8 +382,14 @@ namespace TIAM.Database.Test
company.CommissionPercent = 5;
Assert.IsTrue(await Dal.UpdateCompanyAsync(company));
var addressId = company.Profile.AddressId;
Assert.IsTrue(await Dal.RemoveCompanyAsync(company)); //mielőbb kitöröljük, h ne maradjon szemét a db-ben - J.
Assert.IsNull(await Dal.GetProfileByIdAsync(company.ProfileId));
Assert.IsNull(await Dal.GetAddressByIdAsync(addressId));
Assert.IsTrue(Dal.GetProductsByServiceProviderId(companyId).Count == 0);
company = await Dal.GetCompanyByIdAsync(companyId);
Assert.IsNull(company); //a korábbi törlés miatt NULL kell legyen - J.
}

View File

@ -1,11 +1,15 @@
using AyCode.Core.Extensions;
using AyCode.Database.DbSets.Profiles;
using AyCode.Interfaces.Addresses;
using AyCode.Interfaces.Entities;
using AyCode.Interfaces.Profiles;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using TIAM.Database.DbSets.Users;
using TIAM.Entities.Addresses;
using TIAM.Entities.Products;
using TIAM.Entities.Profiles;
using TIAM.Entities.Users;
namespace TIAM.Database.DbSets.Products;
@ -16,19 +20,27 @@ public static class ProductDbSetExtensions
public static bool AddProduct(this IProductDbSet ctx, Product product)
{
if (product.Id.IsNullOrEmpty()) product.Id = Guid.NewGuid();
if (product.Profile == null!)
product.SetProfile((Activator.CreateInstance(typeof(Profile), product.ProfileId, product.Name) as Profile)!);
if (product.Profile!.Address == null!)
product.Profile.SetAddress((Activator.CreateInstance(typeof(Address), product.Profile.AddressId, product.Name) as Address)!);
return ctx.Products.Add(product).State == EntityState.Added;
}
public static bool UpdateProduct(this IProductDbSet ctx, Product product)
=> ctx.Products.Update(product).State == EntityState.Modified;
{
return ctx.Products.Update(product).State == EntityState.Modified;
}
public static bool RemoveProduct(this IProductDbSet ctx, Product product)
{
ctx.RemoveProfile(product.ProfileId);
ctx.UserProductMappings.RemoveRange(ctx.UserProductMappings.Where(x => x.ProductId == product.Id));
//TODO: Transfer, etc... - J.
ctx.RemoveUserProductMappingsByProductId(product.Id);
//TODO: Transfer, etc... - J.
return ctx.Products.Remove(product).State == EntityState.Deleted;
}

View File

@ -86,4 +86,10 @@ public static class UserProductMappingDbSetExtensions
return userProductMapping == null || ctx.RemoveUserProductMapping(userProductMapping);
}
public static bool RemoveUserProductMappingsByProductId(this IUserProductMappingDbSet ctx, Guid productId)
{
ctx.UserProductMappings.RemoveRange(ctx.UserProductMappings.Where(x=>x.ProductId == productId));
return true;
}
}

View File

@ -1,6 +1,8 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using AyCode.Core.Extensions;
using TIAM.Core.Enums;
using TIAM.Entities.Profiles;
using TIAM.Entities.ServiceProviders;
using TIAM.Entities.Users;
@ -30,4 +32,23 @@ public class Product : ProductBase
ServiceProviderId = serviceProviderId;
ServiceProvider = serviceProvider;
}
public void SetProfile(Profile profile)
{
if (profile.Id.IsNullOrEmpty()) profile.Id = Guid.NewGuid();
Profile = profile;
ProfileId = profile.Id;
}
public bool HasUser(Guid userId)
=> UserProductMappings.Any(x => x.UserId == userId);
public void AddUser(Guid userId, int permissions)
{
if (UserProductMappings.Any(x => x.UserId == userId))
return;
UserProductMappings.Add(new UserProductMapping(Guid.NewGuid(), userId, Id) { Permissions = permissions });
}
}

View File

@ -137,13 +137,13 @@
{
_logger.Debug($"DataItemSaving");
var profileId = Guid.NewGuid();
product.Profile = new Profile(profileId, product.Name);
product.ProfileId = profileId;
// var profileId = Guid.NewGuid();
// product.Profile = new Profile(profileId, product.Name);
// product.ProfileId = profileId;
var addressId = Guid.NewGuid();
product.Profile.Address = new Address(addressId);
product.Profile.AddressId = addressId;
// var addressId = Guid.NewGuid();
// product.Profile.Address = new Address(addressId);
// product.Profile.AddressId = addressId;
//((Product)e.EditModel).UserProductMappings.Add(new UserProductMapping(Guid.NewGuid, ParentData.));
}