public async Task <UserModel> CreateAsync(UserModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var user = MapToUserEntity(model); using (var transaction = _dbContext.Database.BeginTransaction()) { try { _dbContext.Users.Add(user); var roles = _dbContext.Roles.AsEnumerable().Where(r => model.Roles.Select(mr => mr.Id).Contains(r.Id)).ToList(); if (roles.Any()) { var userRoles = roles.Select(role => new UserRole { RoleId = role.Id, UserId = user.Id }); _dbContext.UserRoles.AddRange(userRoles); } await _dbContext.SaveChangesAsync(); transaction.Commit(); return(ToUserModel(user)); } catch (Exception) { transaction?.Rollback(); throw; } } }
public async Task AddRedirectUri(string uri, int clientDbId) { var client = await _dbContext.Clients.FirstOrDefaultAsync(c => c.Id == clientDbId); if (client == null) { throw new ArgumentException($"No Client with id: {clientDbId}"); } var clientRedirectUri = new ClientRedirectUri { Uri = uri.ToLower(), Client = client }; _dbContext.ClientRedirectUri.Add(clientRedirectUri); await _dbContext.SaveChangesAsync(); }