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
public Task<bool> CreateServiceProviderAsync(Company serviceProvider) => TransactionAsync(ctx => ctx.AddServiceProvider(serviceProvider));
public bool CreateProductAsync(Product product)
{
Context.CreateProduct(product);
GlobalLogger.Info($@"Saving product to db {product.Id}, {product.Name}, {product.ServiceProviderId}");
var result = Context.SaveChangesAsync();
return result.Result > 0;
}
//public bool CreateProductAsync(Product product)
//{
// Context.CreateProduct(product);
// GlobalLogger.Info($@"Saving product to db {product.Id}, {product.Name}, {product.ServiceProviderId}");
// var result = Context.SaveChangesAsync();
// return result.Result > 0;
//}
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.Products;
using TIAM.Database.DbSets.Transfers;
@ -97,10 +98,13 @@ namespace TIAM.Database.DbContexts.Admins
public static bool CreateProduct(this IAdminDbContext ctx, Product myproduct)
{
if (myproduct == null) return false;
//Automatically add assigneduser for owner
Company? productOwner = ctx.ServiceProviders.FirstOrDefault(x => x.Id == myproduct.ServiceProviderId);
if (productOwner == null) return false;
var userProductMapping = new UserProductMapping(myproduct.Id, productOwner.OwnerId);
Company? company = ctx.ServiceProviders.FirstOrDefault(x => x.Id == myproduct.ServiceProviderId);
if (company == null || company.OwnerId.IsNullOrEmpty()) return false;
var userProductMapping = new UserProductMapping(myproduct.Id, company.OwnerId.Value);
ctx.CreateAssignedUser(userProductMapping);
ctx.AddProduct(myproduct);
@ -118,10 +122,10 @@ namespace TIAM.Database.DbContexts.Admins
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);
var userProductMapping = new UserProductMapping(serviceProvider.Id, serviceProvider.OwnerId);
var userProductMapping = new UserProductMapping(serviceProvider.Id, serviceProvider.OwnerId.Value);
ctx.CreateAssignedUser(userProductMapping);
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.Entities.ServiceProviders;
using TIAM.Entities.Transfers;
@ -11,7 +12,21 @@ public static class ServiceProviderDbSetExtensions
#region Add, Update, Remove
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)
=> ctx.ServiceProviders.Remove(company).State == EntityState.Deleted;