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(this IAcCompanyDbSetBase ctx, TCompany company) where TCompany : class, IAcCompany where TProfile : class, IAcProfile 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(this IAcCompanyDbSetBase ctx, TCompany company) where TCompany : class, IAcCompany where TProfile : class, IAcProfile 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(this IAcCompanyDbSetBase ctx, TCompany company) where TCompany : class, IAcCompany where TProfile : class, IAcProfile where TAddress : class, IAcAddress { ctx.RemoveProfile(company.ProfileId); return ctx.Companies.Remove(company).State == EntityState.Deleted; } public static bool RemoveServiceProvider(this IAcCompanyDbSetBase ctx, Guid companyId) where TCompany : class, IAcCompany where TProfile : class, IAcProfile where TAddress : class, IAcAddress { var company = ctx.GetServiceProviderById(companyId); return company == null || ctx.RemoveServiceProvider(company); } #endregion Add, Update, Remove public static TCompany? GetServiceProviderById(this IAcCompanyDbSetBase ctx, Guid companyId) where TCompany : class, IAcCompanyBase => ctx.Companies.FirstOrDefault(x => x.Id == companyId); public static IQueryable GetServiceProviders(this IAcCompanyDbSetBase ctx) where TCompany : class, IAcCompanyBase => ctx.Companies; public static List GetServiceProvidersByOwnerId(this IAcCompanyDbSetBase ctx, Guid ownerId) where TCompany : class, IAcCompanyBase => ctx.Companies.Where(x => x.OwnerId == ownerId).ToList(); }