using CH.Authority.Services; using CH.CQRS.Service.User.Options; using CH.Dal; using Microsoft.EntityFrameworkCore; using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping; namespace CH.CQRS.Service.User; public class UserService(CHDbContext dbContext, UserIdentityService identityService) { public async Task UpdateEmailAsync(UpdateEmailCommandOptions options,CancellationToken cancellationToken) { // todo: change on oauth server first, make sure to persist changes if db save fail after keycloak api is succesful var user = await dbContext.Users.FirstOrDefaultAsync(user => user.SubjectId == identityService.SubjectId, cancellationToken); if (null != user) { if (user.Email != options.Email) { user.Email = options.Email; } dbContext.Users.Update(user); await dbContext.SaveChangesAsync(cancellationToken); } return; } public async Task UpdateNameAsync(UpdateNameCommandOptions options,CancellationToken cancellationToken) { var user = await dbContext.Users.FirstOrDefaultAsync( user => user.SubjectId == identityService.SubjectId, cancellationToken); if(null != user) { if (!String.IsNullOrWhiteSpace(options.FirstName) && user.FirstName != options.FirstName) { user.FirstName = options.FirstName; } if (!String.IsNullOrWhiteSpace(options.LastName) && user.LastName != options.LastName) { user.LastName = options.LastName; } dbContext.Users.Update(user); await dbContext.SaveChangesAsync(cancellationToken); } return; } }