improvements, fixes, etc...

This commit is contained in:
Loretta 2024-06-24 07:07:12 +02:00
parent e727e773a6
commit 252b1a0f90
7 changed files with 51 additions and 19 deletions

View File

@ -8,11 +8,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MessagePack.Annotations" Version="2.5.168" />
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>

View File

@ -14,17 +14,16 @@ public static class AcCompanyDbSetExtensions
{
#region Add, Update, Remove
public static bool AddServiceProvider<TCompany, TProfile, TAddress>(this IAcCompanyDbSetBase<TCompany> ctx, TCompany company)
public static bool AddCompany<TCompany, TProfile, TAddress>(this IAcCompanyDbSetBase<TCompany> ctx, TCompany company)
where TCompany : class, IAcCompany<TProfile, TAddress>
where TProfile : class, IAcProfile<TAddress>
where TAddress : class, IAcAddress
{
var companyProfile = company.Profile;
if (company.Profile == null!)
company.SetProfile((Activator.CreateInstance(typeof(TProfile), company.ProfileId, company.Name) as TProfile)!);
if (company.ProfileId.IsNullOrEmpty() || companyProfile.Id != company.ProfileId || companyProfile.AddressId.IsNullOrEmpty() || companyProfile.Address.Id != companyProfile.AddressId)
{
return false;
}
if (company.Profile!.Address == null!)
company.Profile.SetAddress((Activator.CreateInstance(typeof(TAddress), company.Profile.AddressId, company.Name) as TAddress)!);
if (!company.OwnerId.IsNullOrEmpty())
company.AddUser(company.OwnerId.Value, 1);
@ -32,10 +31,11 @@ public static class AcCompanyDbSetExtensions
return ctx.Companies.Add(company).State == EntityState.Added;
}
public static bool UpdateServiceProvider<TCompany, TProfile, TAddress>(this IAcCompanyDbSetBase<TCompany> ctx, TCompany company)
public static bool UpdateCompany<TCompany, TProfile, TAddress, TUserToCompany>(this IAcCompanyDbSetBase<TCompany, TProfile, TAddress, TUserToCompany> ctx, TCompany company)
where TCompany : class, IAcCompany<TProfile, TAddress>
where TProfile : class, IAcProfile<TAddress>
where TAddress : class, IAcAddress
where TUserToCompany : class, IAcUserToCompanyBase
{
var companyProfile = company.Profile;
@ -44,42 +44,49 @@ public static class AcCompanyDbSetExtensions
return false;
}
//if (!company.OwnerId.IsNullOrEmpty())
//{
// companyProfile
// company.AddUser(company.OwnerId.Value, 1);
//}
var ownerId = company.OwnerId;
if (!ownerId.IsNullOrEmpty() && !company.HasUser(ownerId.Value))
{
if (!ctx.UserToCompanies.Any(x => x.UserId == company.OwnerId && x.ServiceProviderId == company.Id))
{
company.AddUser(ownerId.Value, 1);
}
}
return ctx.Companies.Update(company).State == EntityState.Modified;
}
public static bool RemoveServiceProvider<TCompany, TProfile, TAddress>(this IAcCompanyDbSetBase<TCompany, TProfile, TAddress> ctx, TCompany company)
public static bool RemoveCompany<TCompany, TProfile, TAddress, TUserToCompany>(this IAcCompanyDbSetBase<TCompany, TProfile, TAddress, TUserToCompany> ctx, TCompany company)
where TCompany : class, IAcCompany<TProfile, TAddress>
where TProfile : class, IAcProfile<TAddress>
where TAddress : class, IAcAddress
where TUserToCompany : class, IAcUserToCompanyBase
{
ctx.RemoveProfile(company.ProfileId);
ctx.UserToCompanies.RemoveRange(ctx.UserToCompanies.Where(x => x.ServiceProviderId == company.Id));
//TODO: RemoveProduct, UserToProduct, etc... - J.
return ctx.Companies.Remove(company).State == EntityState.Deleted;
}
public static bool RemoveServiceProvider<TCompany, TProfile, TAddress>(this IAcCompanyDbSetBase<TCompany, TProfile, TAddress> ctx, Guid companyId)
public static bool RemoveCompany<TCompany, TProfile, TAddress, TUserToCompany>(this IAcCompanyDbSetBase<TCompany, TProfile, TAddress, TUserToCompany> ctx, Guid companyId)
where TCompany : class, IAcCompany<TProfile, TAddress>
where TProfile : class, IAcProfile<TAddress>
where TAddress : class, IAcAddress
where TUserToCompany : class, IAcUserToCompanyBase
{
var company = ctx.GetServiceProviderById(companyId);
return company == null || ctx.RemoveServiceProvider(company);
var company = ctx.GetCompanyById(companyId);
return company == null || ctx.RemoveCompany(company);
}
#endregion Add, Update, Remove
public static TCompany? GetServiceProviderById<TCompany>(this IAcCompanyDbSetBase<TCompany> ctx, Guid companyId) where TCompany : class, IAcCompanyBase
public static TCompany? GetCompanyById<TCompany>(this IAcCompanyDbSetBase<TCompany> ctx, Guid companyId) where TCompany : class, IAcCompanyBase
=> ctx.Companies.FirstOrDefault(x => x.Id == companyId);
public static IQueryable<TCompany> GetServiceProviders<TCompany>(this IAcCompanyDbSetBase<TCompany> ctx) where TCompany : class, IAcCompanyBase
public static IQueryable<TCompany> GetCompanies<TCompany>(this IAcCompanyDbSetBase<TCompany> ctx) where TCompany : class, IAcCompanyBase
=> ctx.Companies;
public static List<TCompany> GetServiceProvidersByOwnerId<TCompany>(this IAcCompanyDbSetBase<TCompany> ctx, Guid ownerId) where TCompany : class, IAcCompanyBase
public static List<TCompany> GetCompaniesByOwnerId<TCompany>(this IAcCompanyDbSetBase<TCompany> ctx, Guid ownerId) where TCompany : class, IAcCompanyBase
=> ctx.Companies.Where(x => x.OwnerId == ownerId).ToList();
}

View File

@ -23,4 +23,11 @@ public interface IAcCompanyDbSetBase<TCompany, TProfile, TAddress> : IAcCompanyD
where TCompany : class, IAcCompany<TProfile, TAddress>
where TProfile : class, IAcProfile<TAddress>
where TAddress : class, IAcAddress
{ }
public interface IAcCompanyDbSetBase<TCompany, TProfile, TAddress, TUserToCompany> : IAcCompanyDbSetBase<TCompany, TProfile, TAddress>, IAcUserToCompanyDbSetBase<TUserToCompany>
where TCompany : class, IAcCompany<TProfile, TAddress>
where TProfile : class, IAcProfile<TAddress>
where TAddress : class, IAcAddress
where TUserToCompany : class, IAcUserToCompanyBase
{ }

View File

@ -0,0 +1,9 @@
using AyCode.Interfaces.Users;
using Microsoft.EntityFrameworkCore;
namespace AyCode.Database.DbSets.Users;
public interface IAcUserToCompanyDbSetBase<TUserToCompany> where TUserToCompany : class, IAcUserToCompanyBase
{
DbSet<TUserToCompany> UserToCompanies { get; set; }
}

View File

@ -1,6 +1,7 @@

using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Reflection.Metadata.Ecma335;
using AyCode.Core.Extensions;
using AyCode.Interfaces.Addresses;
using AyCode.Interfaces.Profiles;
@ -74,6 +75,9 @@ namespace AyCode.Entities.ServiceProviders
ProfileId = profile.Id;
}
public bool HasUser(Guid userId)
=> UserToServiceProviders.Any(x => x.UserId == userId);
public void AddUser(Guid userId, int permissions)
{
if (UserToServiceProviders.Any(x => x.UserId == userId))

View File

@ -8,4 +8,5 @@ namespace AyCode.Interfaces.Profiles;
public interface IAcProfile<TAddress> : IAcProfileDtoBase<TAddress>, ITimeStampInfo where TAddress : IAcAddressDtoBase
{
public string? GetFullName(string lang = "ENG");
public void SetAddress(TAddress address);
}

View File

@ -15,5 +15,7 @@ public interface IAcCompanyBase : IEntityGuid, IAcProfileForeignKey, ITimeStampI
Guid AffiliateId { get; set; }
Guid? ReferralId { get; set; }
public bool HasUser(Guid userId);
public void AddUser(Guid userId, int permissions);
}