diff --git a/AyCode.Database/AyCode.Database.csproj b/AyCode.Database/AyCode.Database.csproj
index a2e1281..cc14ac3 100644
--- a/AyCode.Database/AyCode.Database.csproj
+++ b/AyCode.Database/AyCode.Database.csproj
@@ -8,11 +8,13 @@
+
+
diff --git a/AyCode.Database/DbSets/Companies/AcCompanyDbSetExtensions.cs b/AyCode.Database/DbSets/Companies/AcCompanyDbSetExtensions.cs
index 5ade2fd..fc6515c 100644
--- a/AyCode.Database/DbSets/Companies/AcCompanyDbSetExtensions.cs
+++ b/AyCode.Database/DbSets/Companies/AcCompanyDbSetExtensions.cs
@@ -14,17 +14,16 @@ public static class AcCompanyDbSetExtensions
{
#region Add, Update, Remove
- public static bool AddServiceProvider(this IAcCompanyDbSetBase ctx, TCompany company)
+ public static bool AddCompany(this IAcCompanyDbSetBase ctx, TCompany company)
where TCompany : class, IAcCompany
where TProfile : class, IAcProfile
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(this IAcCompanyDbSetBase ctx, TCompany company)
+ public static bool UpdateCompany(this IAcCompanyDbSetBase ctx, TCompany company)
where TCompany : class, IAcCompany
where TProfile : class, IAcProfile
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(this IAcCompanyDbSetBase ctx, TCompany company)
+ public static bool RemoveCompany(this IAcCompanyDbSetBase ctx, TCompany company)
where TCompany : class, IAcCompany
where TProfile : class, IAcProfile
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(this IAcCompanyDbSetBase ctx, Guid companyId)
+ public static bool RemoveCompany(this IAcCompanyDbSetBase ctx, Guid companyId)
where TCompany : class, IAcCompany
where TProfile : class, IAcProfile
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(this IAcCompanyDbSetBase ctx, Guid companyId) where TCompany : class, IAcCompanyBase
+ public static TCompany? GetCompanyById(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
+ public static IQueryable GetCompanies(this IAcCompanyDbSetBase ctx) where TCompany : class, IAcCompanyBase
=> ctx.Companies;
- public static List GetServiceProvidersByOwnerId(this IAcCompanyDbSetBase ctx, Guid ownerId) where TCompany : class, IAcCompanyBase
+ public static List GetCompaniesByOwnerId(this IAcCompanyDbSetBase ctx, Guid ownerId) where TCompany : class, IAcCompanyBase
=> ctx.Companies.Where(x => x.OwnerId == ownerId).ToList();
}
\ No newline at end of file
diff --git a/AyCode.Database/DbSets/Companies/IAcCompanyDbSetBase.cs b/AyCode.Database/DbSets/Companies/IAcCompanyDbSetBase.cs
index 4bb0e1a..a82d7f3 100644
--- a/AyCode.Database/DbSets/Companies/IAcCompanyDbSetBase.cs
+++ b/AyCode.Database/DbSets/Companies/IAcCompanyDbSetBase.cs
@@ -23,4 +23,11 @@ public interface IAcCompanyDbSetBase : IAcCompanyD
where TCompany : class, IAcCompany
where TProfile : class, IAcProfile
where TAddress : class, IAcAddress
+{ }
+
+public interface IAcCompanyDbSetBase : IAcCompanyDbSetBase, IAcUserToCompanyDbSetBase
+ where TCompany : class, IAcCompany
+ where TProfile : class, IAcProfile
+ where TAddress : class, IAcAddress
+ where TUserToCompany : class, IAcUserToCompanyBase
{ }
\ No newline at end of file
diff --git a/AyCode.Database/DbSets/Users/IAcUserToCompanyDbSetBase.cs b/AyCode.Database/DbSets/Users/IAcUserToCompanyDbSetBase.cs
new file mode 100644
index 0000000..c127bd7
--- /dev/null
+++ b/AyCode.Database/DbSets/Users/IAcUserToCompanyDbSetBase.cs
@@ -0,0 +1,9 @@
+using AyCode.Interfaces.Users;
+using Microsoft.EntityFrameworkCore;
+
+namespace AyCode.Database.DbSets.Users;
+
+public interface IAcUserToCompanyDbSetBase where TUserToCompany : class, IAcUserToCompanyBase
+{
+ DbSet UserToCompanies { get; set; }
+}
\ No newline at end of file
diff --git a/AyCode.Entities/ServiceProviders/AcCompany.cs b/AyCode.Entities/ServiceProviders/AcCompany.cs
index c5c7323..2b2dc57 100644
--- a/AyCode.Entities/ServiceProviders/AcCompany.cs
+++ b/AyCode.Entities/ServiceProviders/AcCompany.cs
@@ -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))
diff --git a/AyCode.Interfaces/Profiles/IAcProfile.cs b/AyCode.Interfaces/Profiles/IAcProfile.cs
index 5e0440c..142433d 100644
--- a/AyCode.Interfaces/Profiles/IAcProfile.cs
+++ b/AyCode.Interfaces/Profiles/IAcProfile.cs
@@ -8,4 +8,5 @@ namespace AyCode.Interfaces.Profiles;
public interface IAcProfile : IAcProfileDtoBase, ITimeStampInfo where TAddress : IAcAddressDtoBase
{
public string? GetFullName(string lang = "ENG");
+ public void SetAddress(TAddress address);
}
\ No newline at end of file
diff --git a/AyCode.Interfaces/ServiceProviders/IAcCompanyBase.cs b/AyCode.Interfaces/ServiceProviders/IAcCompanyBase.cs
index f10524b..47e4bbf 100644
--- a/AyCode.Interfaces/ServiceProviders/IAcCompanyBase.cs
+++ b/AyCode.Interfaces/ServiceProviders/IAcCompanyBase.cs
@@ -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);
+
}
\ No newline at end of file