public async Task <ActionResult <User> > Register(CreateUserResource userResource) { if (await UnitOfWork.Users.SingleOrDefault(u => u.Username == userResource.Username) != null) { throw new RestError(HttpStatusCode.BadRequest, new { Username = "******" }); } var user = await UnitOfWork.Users.SingleOrDefault(u => u.Email == userResource.Email); if (user != null) { throw new RestError(HttpStatusCode.BadRequest, new { Email = "Email already exists" }); } user = Mapper.Map <CreateUserResource, User>(userResource); user.Password = Hasher <User> .Hash(user, user.Password); user.CreatedAt = DateTime.Now; UnitOfWork.Users.Add(user); await UnitOfWork.CompleteAsync(); var createdUser = await UnitOfWork.Users.Get(user.Id); var response = Mapper.Map <User, ReadUserResource>(createdUser); return(Ok(response)); }
public async Task <IActionResult> Create([FromBody] CreateUserResource user) { if (string.IsNullOrWhiteSpace(user.Name)) { return(BadRequest(nameof(user.Name))); } if (string.IsNullOrWhiteSpace(user.Mail)) { return(BadRequest(nameof(user.Mail))); } if (string.IsNullOrWhiteSpace(user.Password)) { return(BadRequest(nameof(user.Password))); } var newUser = new User { Name = user.Name, Email = user.Mail, NormalizedEmail = user.Mail.ToUpper(), UserName = user.Mail, NormalizedUserName = user.Mail.ToUpper(), Type = user.Type }; var result = await _userManager.CreateAsync(newUser, user.Password); await _context.SaveChangesAsync(); if (result.Succeeded) { return(Ok(new { Id = newUser.Id })); } return(BadRequest(result.Errors.Select(e => e.Code))); }
public async Task <IActionResult> CreateUser(CreateUserResource userIn) { if (!ModelState.IsValid) { StringBuilder sb = new StringBuilder(); foreach (var modelState in ModelState.Values) { foreach (ModelError error in modelState.Errors) { sb.Append(error.ErrorMessage); } } return(BadRequest(sb.ToString())); } var user = mapper.Map <CreateUserResource, User>(userIn); user.StatusId = 1; try { await userRepository.AddUserAsync(user); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(CreatedAtRoute("GetUser", new { id = user.Id })); }
public async Task <IActionResult> CreateUser([FromBody] CreateUserResource model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userIdentity = _mapper.Map <CreateUserResource, ApplicationUser>(model); var result = await _userManager.CreateAsync(userIdentity, model.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(userIdentity, "member"); // Send token trough email string confirmationToken = _userManager.GenerateEmailConfirmationTokenAsync(userIdentity).Result; return(Ok(result)); } return(BadRequest(Errors.AddErrorToModelState("register_failure", result.ToString(), ModelState))); } catch (Exception ex) { return(BadRequest(ex.ToString())); } }
public async Task <IActionResult> PostAsync([FromBody] CreateUserResource resource) { //if request body does not contain required fields return error if (!ModelState.IsValid) { var errorString = JsonSerializer.Serialize(ModelState.GetErrorMessages()); return(BadRequest(errorString)); } var TestUser = _mapper.Map <CreateUserResource, User>(resource); //test if email is taken var result = await _userService.FindEmailAsync(TestUser); if (!result.Success) { var errorString = JsonSerializer.Serialize(result.Message); return(BadRequest(errorString)); } //test if username is taken result = await _userService.FindNameAsync(TestUser); if (!result.Success) { var errorString = JsonSerializer.Serialize(result.Message); return(BadRequest(errorString)); } //test password validation result = await _userService.PasswordValidation(TestUser); if (!result.Success) { var errorString = JsonSerializer.Serialize(result.Message); return(BadRequest(errorString)); } //hash the password var user = await _userService.HashPassword(TestUser); //save user to database var response = await _userService.CreateUserAsync(user); if (!response.Success) { var errorString = JsonSerializer.Serialize(result.Message); return(BadRequest(errorString)); } //return success with user email/name var userResource = _mapper.Map <User, UserResource>(response.User); var userString = JsonSerializer.Serialize(userResource); return(Ok(userString)); }
public async Task <IActionResult> Create([FromBody] CreateUserResource resource) { var user = _mapper.Map <CreateUserResource, IdentityUser>(resource); var result = await _usersService.CreateAsync(user, resource.Password); if (!result.Success) { return(BadRequest(new ErrorResource(result.Messages))); } var userResource = _mapper.Map <IdentityUser, UserResource>(result.Resource); return(Ok(userResource)); }
public async Task <IActionResult> PostAsync([FromBody] CreateUserResource createUserResource) { var user = mapper.Map <CreateUserResource, User>(createUserResource); var result = await userService.CreateAsync(user); if (result == null) { return(BadRequest()); } var resource = mapper.Map <User, UserResource>(user); return(Created($"/api/v1/users/{resource.Id}", resource)); }
public async Task <IActionResult> CreateUser([FromBody] CreateUserResource userResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = mapper.Map <CreateUserResource, User>(userResource); context.Users.Add(user); await context.SaveChangesAsync(); var result = mapper.Map <User, UserResource>(user); return(Ok(result)); }
public async Task <UserResource> CreateUser(CreateUserResource newUser) { var validator = new CreateUserResourceValidator(); var validationResult = await validator.ValidateAsync(newUser); if (!validationResult.IsValid) { throw new ValidationException(validationResult.Errors); } var user = _mapper.Map <CreateUserResource, User>(newUser); IdentityResult result = await _userManager.CreateAsync(user, newUser.Password); if (!result.Succeeded) { throw ExceptionBuilder.Create("Eror when creating user - " + result.ToString(), HttpStatusCode.BadRequest); } var userModel = _mapper.Map <UserResource>(user); return(userModel); }
public async Task CreateUser(CreateUserResource model) { await(await unitOfWork.GetRepository <UserRepository, User, Guid>()).Create(mapper.Map <User>(model)); await unitOfWork.Commit(); }