AyCode.Core/AyCode.Database/DbSets/Companies/AcCompanyDbSetExtensions.cs

85 lines
3.5 KiB
C#

using AyCode.Core.Extensions;
using AyCode.Database.DbSets.Profiles;
using AyCode.Interfaces.Addresses;
using AyCode.Interfaces.Profiles;
using AyCode.Interfaces.Profiles.Dtos;
using AyCode.Interfaces.ServiceProviders;
using AyCode.Interfaces.Users;
using AyCode.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
namespace AyCode.Database.DbSets.Companies;
public static class AcCompanyDbSetExtensions
{
#region Add, Update, Remove
public static bool AddServiceProvider<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.ProfileId.IsNullOrEmpty() || companyProfile.Id != company.ProfileId || companyProfile.AddressId.IsNullOrEmpty() || companyProfile.Address.Id != companyProfile.AddressId)
{
return false;
}
if (!company.OwnerId.IsNullOrEmpty())
company.AddUser(company.OwnerId.Value, 1);
return ctx.Companies.Add(company).State == EntityState.Added;
}
public static bool UpdateServiceProvider<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.ProfileId.IsNullOrEmpty() || companyProfile.Id != company.ProfileId || companyProfile.AddressId.IsNullOrEmpty() || companyProfile.Address.Id != companyProfile.AddressId)
{
return false;
}
//if (!company.OwnerId.IsNullOrEmpty())
//{
// companyProfile
// company.AddUser(company.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)
where TCompany : class, IAcCompany<TProfile, TAddress>
where TProfile : class, IAcProfile<TAddress>
where TAddress : class, IAcAddress
{
ctx.RemoveProfile(company.ProfileId);
return ctx.Companies.Remove(company).State == EntityState.Deleted;
}
public static bool RemoveServiceProvider<TCompany, TProfile, TAddress>(this IAcCompanyDbSetBase<TCompany, TProfile, TAddress> ctx, Guid companyId)
where TCompany : class, IAcCompany<TProfile, TAddress>
where TProfile : class, IAcProfile<TAddress>
where TAddress : class, IAcAddress
{
var company = ctx.GetServiceProviderById(companyId);
return company == null || ctx.RemoveServiceProvider(company);
}
#endregion Add, Update, Remove
public static TCompany? GetServiceProviderById<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
=> ctx.Companies;
public static List<TCompany> GetServiceProvidersByOwnerId<TCompany>(this IAcCompanyDbSetBase<TCompany> ctx, Guid ownerId) where TCompany : class, IAcCompanyBase
=> ctx.Companies.Where(x => x.OwnerId == ownerId).ToList();
}