dotnet-digital-ops/DigitalOps.CQRS/Services/Client/ClientService.cs

31 lines
978 B
C#

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,
Organization = organization
};
organization.Clients.Add(client);
await dbContext.SaveChangesAsync(cancellationToken);
}
}