public async Task <ActionResult <ServerDto> > PostAsync(CreateServerDto dto) { using (var transaction = await dataContext.Database.BeginTransactionAsync()) { var user = new User(); mapper.Map(dto, user); var identityResult = await userManager.CreateAsync(user, dto.Password); if (!identityResult.Succeeded) { return(BadRequest(identityResult.Errors)); } await userManager.AddToRoleAsync(user, UserRoles.Server); var server = new Server { User = user }; mapper.Map(dto, server); dataContext.Add(server); await dataContext.SaveChangesAsync(); var result = mapper.Map <ServerDto>(dto); result.Id = server.Id; transaction.Commit(); return(result); } }
/// <summary> /// Creates a new server. /// </summary> /// <param name="createServerDto">The creation request data.</param> /// <param name="owner">The user creating the server.</param> /// <returns>The result of the creation.</returns> public async Task <CreateServerResult> CreateServerAsync(CreateServerDto createServerDto, ApplicationUser owner) { var validationResult = AnnotationValidator.TryValidate(createServerDto); if (validationResult.Failed) { return(CreateServerResult.Fail(validationResult.Error.ToErrorList())); } var server = new Server(createServerDto.Name, createServerDto.PrivacyLevel, createServerDto.Description); var category = Category.CreateDefaultWelcomeCategory(server); server.Categories.Add(category); server.Members.Add(owner); var adminRole = ServerRole.CreateDefaultAdminRole(server); await _serverRepository.InsertAsync(server); await _serverRoleRepository.InsertAsync(adminRole); await _userRoleRepository.InsertAsync(new IdentityUserRole <Guid> { UserId = owner.Id, RoleId = adminRole.Id }); await _serverRepository.CommitAsync(); return(CreateServerResult.Ok(server)); }
public async Task <ServerDto> SaveServer([FromBody] CreateServerDto serverDto) { var user = await _userManager.FindByEmailAsync(HttpContext.User.Identity.Name); var server = _mapper.Map <Server>(serverDto); server.CreatedAt = DateTime.Now; server.OwnedById = user.Id; await _unitOfWork.Servers.Insert(server); // so that there is always a default configuration for notification thresholds await SetDefaultServerNotifications(server); return(_mapper.Map <ServerDto>(server)); }