public async Task <IActionResult> CreateMessage(int userId, [FromBody] MessageForCreationDto messageForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipent = await _repo.GetUser(messageForCreationDto.RecipientId); var sender = await _repo.GetUser(messageForCreationDto.SenderId); if (recipent == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); var messageToReturn = _mapper.Map <MessageToReturnDto>(message); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new Exception("Create message failed"); }
public async Task <IActionResult> UpdateUser(int id, [FromBody] UserForUpdateDto userForUpdateDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var userFromRepo = await _repo.GetUser(id); if (userFromRepo == null) { return(NotFound($"Could not find user with id {id}")); } if (currentUserId != userFromRepo.Id) { return(Unauthorized()); } _mapper.Map(userForUpdateDto, userFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updating user {id} faid to save"); }