Company.OwnerId set to Guid?; improvements, fixes...

This commit is contained in:
jozsef.b@aycode.com 2024-05-28 19:11:01 +02:00
parent 269e8bd679
commit ceb46f215b
3 changed files with 34 additions and 15 deletions

View File

@ -225,13 +225,13 @@ namespace TIAM.Database.DataLayers.Admins
//15. (IServiceProviderDataService) Create service provider //15. (IServiceProviderDataService) Create service provider
public Task<bool> CreateServiceProviderAsync(Company serviceProvider) => TransactionAsync(ctx => ctx.AddServiceProvider(serviceProvider)); public Task<bool> CreateServiceProviderAsync(Company serviceProvider) => TransactionAsync(ctx => ctx.AddServiceProvider(serviceProvider));
public bool CreateProductAsync(Product product) //public bool CreateProductAsync(Product product)
{ //{
Context.CreateProduct(product); // Context.CreateProduct(product);
GlobalLogger.Info($@"Saving product to db {product.Id}, {product.Name}, {product.ServiceProviderId}"); // GlobalLogger.Info($@"Saving product to db {product.Id}, {product.Name}, {product.ServiceProviderId}");
var result = Context.SaveChangesAsync(); // var result = Context.SaveChangesAsync();
return result.Result > 0; // return result.Result > 0;
} //}
public Task<List<Company>> GetServiceProvidersAsync() => SessionAsync(ctx => ctx.GetServiceProviders().ToList()); public Task<List<Company>> GetServiceProvidersAsync() => SessionAsync(ctx => ctx.GetServiceProviders().ToList());

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore; using AyCode.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
using TIAM.Database.DbSets.Permissions; using TIAM.Database.DbSets.Permissions;
using TIAM.Database.DbSets.Products; using TIAM.Database.DbSets.Products;
using TIAM.Database.DbSets.Transfers; using TIAM.Database.DbSets.Transfers;
@ -97,10 +98,13 @@ namespace TIAM.Database.DbContexts.Admins
public static bool CreateProduct(this IAdminDbContext ctx, Product myproduct) public static bool CreateProduct(this IAdminDbContext ctx, Product myproduct)
{ {
if (myproduct == null) return false; if (myproduct == null) return false;
//Automatically add assigneduser for owner //Automatically add assigneduser for owner
Company? productOwner = ctx.ServiceProviders.FirstOrDefault(x => x.Id == myproduct.ServiceProviderId); Company? company = ctx.ServiceProviders.FirstOrDefault(x => x.Id == myproduct.ServiceProviderId);
if (productOwner == null) return false; if (company == null || company.OwnerId.IsNullOrEmpty()) return false;
var userProductMapping = new UserProductMapping(myproduct.Id, productOwner.OwnerId);
var userProductMapping = new UserProductMapping(myproduct.Id, company.OwnerId.Value);
ctx.CreateAssignedUser(userProductMapping); ctx.CreateAssignedUser(userProductMapping);
ctx.AddProduct(myproduct); ctx.AddProduct(myproduct);
@ -118,10 +122,10 @@ namespace TIAM.Database.DbContexts.Admins
public static Company CreateServiceProvider(this IAdminDbContext ctx, Company serviceProvider) public static Company CreateServiceProvider(this IAdminDbContext ctx, Company serviceProvider)
{ {
if (serviceProvider == null) return null; if (serviceProvider == null || serviceProvider.OwnerId.IsNullOrEmpty()) return null;
ctx.ServiceProviders.Add(serviceProvider); ctx.ServiceProviders.Add(serviceProvider);
var userProductMapping = new UserProductMapping(serviceProvider.Id, serviceProvider.OwnerId); var userProductMapping = new UserProductMapping(serviceProvider.Id, serviceProvider.OwnerId.Value);
ctx.CreateAssignedUser(userProductMapping); ctx.CreateAssignedUser(userProductMapping);
return serviceProvider; return serviceProvider;
} }

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore; using AyCode.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
using TIAM.Database.DbSets.Transfers; using TIAM.Database.DbSets.Transfers;
using TIAM.Entities.ServiceProviders; using TIAM.Entities.ServiceProviders;
using TIAM.Entities.Transfers; using TIAM.Entities.Transfers;
@ -11,7 +12,21 @@ public static class ServiceProviderDbSetExtensions
#region Add, Update, Remove #region Add, Update, Remove
public static bool AddServiceProvider(this IServiceProviderDbSet ctx, Company company) public static bool AddServiceProvider(this IServiceProviderDbSet ctx, Company company)
=> ctx.ServiceProviders.Add(company).State == EntityState.Added; {
var companyProfile = company.Profile;
if (company.ProfileId.IsNullOrEmpty() || companyProfile.Id != company.ProfileId || companyProfile.AddressId.IsNullOrEmpty() || companyProfile.Address.Id != companyProfile.AddressId)
{
return false;
}
if (!company.OwnerId.IsNullOrEmpty())
{
}
return ctx.ServiceProviders.Add(company).State == EntityState.Added;
}
public static bool RemoveServiceProvider(this IServiceProviderDbSet ctx, Company company) public static bool RemoveServiceProvider(this IServiceProviderDbSet ctx, Company company)
=> ctx.ServiceProviders.Remove(company).State == EntityState.Deleted; => ctx.ServiceProviders.Remove(company).State == EntityState.Deleted;