public async Task <ActionResult> Create([FromBody] UserCreationRequestDTO userCreationRequestDTO) { _logger.LogInformation($"New user request: {userCreationRequestDTO}"); var validator = new UserCreationValidator(); var validationResult = validator.Validate(userCreationRequestDTO); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors)); } try { var result = await _userService.CreateUser(userCreationRequestDTO); return(StatusCode(201, result)); } catch (ObjectAlreadyExistsException e) { return(BadRequest(new ServiceErrorDTO { Message = e.Message })); } }
// // Summary: // Creates a new user. If the room name is already taken, then it raises an exception. public async Task <UserResponseDTO> CreateUser(UserCreationRequestDTO userCreationRequestDTO) { _logger.LogInformation("New request for user creation: {userCreationRequestDTO}", userCreationRequestDTO); var userWithSameName = await _db.Users.Where(user => user.Name == userCreationRequestDTO.Name).FirstOrDefaultAsync(); if (userWithSameName != null) { throw new ObjectAlreadyExistsException("A user with this name already exists!"); } var newUser = _mapper.Map <UserCreationRequestDTO, User>(userCreationRequestDTO); await _db.Users.AddAsync(newUser); await _db.SaveChangesAsync(); return(_mapper.Map <User, UserResponseDTO>(newUser)); }