public async Task <string> CreateAsync(AddOrEditAccountDto dto) { if (await CheckDuplicationClientIdAsync(dto.UserName) != null) { throw new BadRequestException($"This user already exists."); } var client = await _context.Clients.SingleOrDefaultAsync(x => x.ClientId.Equals(dto.ClientId)); if (client == null) { throw new BadRequestException("Invalid client"); } var entity = dto.ToEntity(); entity.PasswordHash = _passwordHasher.HashPassword(entity, dto.Password); _context.Users.Add(entity); _context.UserClients.Add(new UserClient { UserId = entity.Id, ClientId = client.Id }); await _context.SaveChangesAsync(); return(entity.Id); }
public async Task <string> CreateOrEditAsync(AddOrEditAccountDto dto) { var client = await _context.Clients.SingleOrDefaultAsync(x => x.ClientId.Equals(dto.ClientId)); if (client == null) { throw new BadRequestException("Invalid client"); } var entity = await CheckDuplicationClientIdAsync(dto.UserName); if (entity == null) { entity = dto.ToEntity(); if (!string.IsNullOrEmpty(dto.Password)) { entity.PasswordHash = _passwordHasher.HashPassword(entity, dto.Password); } _context.Users.Add(entity); _context.UserClients.Add(new UserClient { UserId = entity.Id, ClientId = client.Id }); } else { var userClients = await _context.UserClients .SingleOrDefaultAsync(x => x.UserId.Equals(entity.Id) && x.ClientId.Equals(client.Id)); if (userClients == null) { _context.UserClients.Add(new UserClient { UserId = entity.Id, ClientId = client.Id }); } } await _context.SaveChangesAsync(); return(entity.Id); }