diff --git a/DigitalOps.Authority/Validators/HasAccessToClientValidator.cs b/DigitalOps.Authority/Validators/HasAccessToClientValidator.cs index e665a87..1c09617 100644 --- a/DigitalOps.Authority/Validators/HasAccessToClientValidator.cs +++ b/DigitalOps.Authority/Validators/HasAccessToClientValidator.cs @@ -24,15 +24,15 @@ public class HasAccessToClientValidator : AbstractValidator .SetValidator(new DbEntityExistValidator(dbContext)) .CustomAsync(async (clientId, validationContext, cancellationToken) => { - var organizationClient = await _dbContext.OrganizationClients + var client = await _dbContext.Clients .AsNoTracking() - .FirstOrDefaultAsync(organizationClient => organizationClient.ClientId == clientId, cancellationToken); + .FirstOrDefaultAsync(client => client.Id == clientId, cancellationToken); - if (organizationClient is null) + if (client is null) return; var validation = new HasAccessToOrganizationValidator(_role, _dbContext, _userIdentityService); - var validationResult = validation.Validate(organizationClient.OrganizationId); + var validationResult = validation.Validate(client.OrganizationId); if (!validationResult.IsValid) foreach (var error in validationResult.Errors) diff --git a/DigitalOps.CQRS/Queries/Client/ClientQuery.cs b/DigitalOps.CQRS/Queries/Client/ClientQuery.cs index 1244be8..1336467 100644 --- a/DigitalOps.CQRS/Queries/Client/ClientQuery.cs +++ b/DigitalOps.CQRS/Queries/Client/ClientQuery.cs @@ -13,7 +13,6 @@ public class ClientParams public class ClientItem { public long Id { get; set; } - public IEnumerable OrganizationIds { get; set; } public required string Name { get; set; } public DateTime CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } @@ -39,18 +38,14 @@ public class ClientQueryableProvider(MainDbContext dbContext, UserIdentityServic var user = await userIdentityService.GetUserOrDefaultAsync(cancellationToken); - var queryable = dbContext.Clients - .AsNoTracking(); - var result = dbContext.Clients .AsNoTracking() - .Where(client => client.OrganizationClients.Any(organizationClient => organizationClient.OrganizationId == organizationId)) + .Where(client => client.OrganizationId == organizationId) .Where(client => - client.OrganizationClients.Any(organizationClient => organizationClient.Organization.OrganizationUsers.Any(organizationUser => organizationUser.UserId == user!.Id))) + client.Organization.OrganizationUsers.Any(organizationClient => organizationClient.Organization.OrganizationUsers.Any(organizationUser => organizationUser.UserId == user!.Id))) .Select(client => new ClientItem { Id = client.Id, - OrganizationIds = client.OrganizationClients.Select(organizationClient => organizationClient.OrganizationId), Name = client.Name, CreatedAt = client.CreatedAt, UpdatedAt = client.UpdatedAt diff --git a/DigitalOps.CQRS/Queries/Project/ProjectQuery.cs b/DigitalOps.CQRS/Queries/Project/ProjectQuery.cs index c046232..4ec254e 100644 --- a/DigitalOps.CQRS/Queries/Project/ProjectQuery.cs +++ b/DigitalOps.CQRS/Queries/Project/ProjectQuery.cs @@ -55,7 +55,8 @@ public class ProjectQueryableProvider(MainDbContext dbContext, UserIdentityServi } queryable = queryable - .Where(project => project.Client.OrganizationClients.Any(organizationClient => + .Where(project => project.Client.OrganizationId == organizationId) + .Where(project => project.Client.Organization.OrganizationUsers.Any(organizationClient => organizationClient.OrganizationId == organizationUser!.OrganizationId)); var result = queryable.Select(project => new ProjectItem diff --git a/DigitalOps.CQRS/Services/Client/ClientService.cs b/DigitalOps.CQRS/Services/Client/ClientService.cs index 7a118a8..b028710 100644 --- a/DigitalOps.CQRS/Services/Client/ClientService.cs +++ b/DigitalOps.CQRS/Services/Client/ClientService.cs @@ -21,16 +21,11 @@ public class ClientService(MainDbContext dbContext, UserIdentityService userIden var client = new Dal.DbEntity.Client { - Name = options.Name - }; - - var organizationClient = new OrganizationClient - { - Organization = organization, - Client = client + Name = options.Name, + Organization = organization }; - organization.OrganizationClients.Add(organizationClient); + organization.Clients.Add(client); await dbContext.SaveChangesAsync(cancellationToken); } } \ No newline at end of file diff --git a/DigitalOps.Dal/DbEntity/Client.cs b/DigitalOps.Dal/DbEntity/Client.cs index a2b2bfd..55f7742 100644 --- a/DigitalOps.Dal/DbEntity/Client.cs +++ b/DigitalOps.Dal/DbEntity/Client.cs @@ -7,13 +7,15 @@ public partial class Client { public long Id { get; set; } + public long OrganizationId { get; set; } + public string Name { get; set; } = null!; public DateTime CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } - public virtual ICollection OrganizationClients { get; set; } = new List(); + public virtual Organization Organization { get; set; } = null!; public virtual ICollection Projects { get; set; } = new List(); } diff --git a/DigitalOps.Dal/DbEntity/Organization.cs b/DigitalOps.Dal/DbEntity/Organization.cs index 07f7d24..2c2c619 100644 --- a/DigitalOps.Dal/DbEntity/Organization.cs +++ b/DigitalOps.Dal/DbEntity/Organization.cs @@ -13,9 +13,9 @@ public partial class Organization public DateTime? UpdatedAt { get; set; } - public virtual ICollection OidcProviders { get; set; } = new List(); + public virtual ICollection Clients { get; set; } = new List(); - public virtual ICollection OrganizationClients { get; set; } = new List(); + public virtual ICollection OidcProviders { get; set; } = new List(); public virtual ICollection OrganizationUsers { get; set; } = new List(); } diff --git a/DigitalOps.Dal/DbEntity/OrganizationClient.cs b/DigitalOps.Dal/DbEntity/OrganizationClient.cs deleted file mode 100644 index b493e44..0000000 --- a/DigitalOps.Dal/DbEntity/OrganizationClient.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace DigitalOps.Dal.DbEntity; - -public partial class OrganizationClient -{ - public long Id { get; set; } - - public long OrganizationId { get; set; } - - public long ClientId { get; set; } - - public DateTime CreatedAt { get; set; } - - public DateTime? UpdatedAt { get; set; } - - public virtual Client Client { get; set; } = null!; - - public virtual Organization Organization { get; set; } = null!; -} diff --git a/DigitalOps.Dal/MainDbScaffoldedContext.cs b/DigitalOps.Dal/MainDbScaffoldedContext.cs index daba6ba..4081ee9 100644 --- a/DigitalOps.Dal/MainDbScaffoldedContext.cs +++ b/DigitalOps.Dal/MainDbScaffoldedContext.cs @@ -1,4 +1,6 @@ -using DigitalOps.Dal.DbEntity; +using System; +using System.Collections.Generic; +using DigitalOps.Dal.DbEntity; using Microsoft.EntityFrameworkCore; namespace DigitalOps.Dal; @@ -20,8 +22,6 @@ public partial class MainDbScaffoldedContext : DbContext public virtual DbSet Organizations { get; set; } - public virtual DbSet OrganizationClients { get; set; } - public virtual DbSet OrganizationUsers { get; set; } public virtual DbSet Projects { get; set; } @@ -52,7 +52,13 @@ public partial class MainDbScaffoldedContext : DbContext entity.Property(e => e.Name) .HasMaxLength(255) .HasColumnName("name"); + entity.Property(e => e.OrganizationId).HasColumnName("organization_id"); entity.Property(e => e.UpdatedAt).HasColumnName("updated_at"); + + entity.HasOne(d => d.Organization).WithMany(p => p.Clients) + .HasForeignKey(d => d.OrganizationId) + .OnDelete(DeleteBehavior.ClientSetNull) + .HasConstraintName("client_organization_id_fkey"); }); modelBuilder.Entity(entity => @@ -96,31 +102,6 @@ public partial class MainDbScaffoldedContext : DbContext entity.Property(e => e.UpdatedAt).HasColumnName("updated_at"); }); - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("organization_client_pkey"); - - entity.ToTable("organization_client"); - - entity.Property(e => e.Id).HasColumnName("id"); - entity.Property(e => e.ClientId).HasColumnName("client_id"); - entity.Property(e => e.CreatedAt) - .HasDefaultValueSql("(CURRENT_TIMESTAMP AT TIME ZONE 'UTC'::text)") - .HasColumnName("created_at"); - entity.Property(e => e.OrganizationId).HasColumnName("organization_id"); - entity.Property(e => e.UpdatedAt).HasColumnName("updated_at"); - - entity.HasOne(d => d.Client).WithMany(p => p.OrganizationClients) - .HasForeignKey(d => d.ClientId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("organization_client_client_id_fkey"); - - entity.HasOne(d => d.Organization).WithMany(p => p.OrganizationClients) - .HasForeignKey(d => d.OrganizationId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("organization_client_organization_id_fkey"); - }); - modelBuilder.Entity(entity => { entity.HasKey(e => e.Id).HasName("organization_user_pkey");