public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { var sender = await _repo.GetUser(userId, false); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _repo.GetUser(messageForCreationDto.RecipientId, false); if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); // Using .Reverse method on mapper profile if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new AutoMapperConfigurationException("Crteating the message failed on save"); }
public async Task <IActionResult> LikeUser(int id, int recipientId) { // Check if user is authorized if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } // Get a "like" from the repo var like = await _repo.GetLike(id, recipientId); if (like != null) { return(BadRequest("You already like this user")); } if (await _repo.GetUser(recipientId, false) == null) { return(NotFound()); } // Create a new "like" using method arguments like = new Like { LikerId = id, LikeeId = recipientId }; // Add the "like" to the repo _repo.Add <Like>(like); // Save back to the repo if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to like user")); }