using DigitalOps.Authority.Services; using DigitalOps.CQRS.Services.Client.Options; using DigitalOps.Dal; using DigitalOps.Dal.DbEntity; using Microsoft.EntityFrameworkCore; namespace DigitalOps.CQRS.Services.Client; public class ClientService(MainDbContext dbContext, UserIdentityService userIdentityService) { public async Task AddClientAsync(AddClientOptions options, CancellationToken cancellationToken = default) { var user = await userIdentityService.GetUserOrDefaultAsync(cancellationToken); if (null == user) return; var organization = await dbContext.Organizations.FirstOrDefaultAsync(organization => organization.Id == options.OrganizationId, cancellationToken); if (null == organization) return; var client = new Dal.DbEntity.Client { Name = options.Name }; var organizationClient = new OrganizationClient { Organization = organization, Client = client }; organization.OrganizationClients.Add(organizationClient); await dbContext.SaveChangesAsync(cancellationToken); } }