public async Task <IActionResult> LikeUser(int id, int recipientId) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var like = await _repo.GetLike(id, recipientId); if (like != null) { return(BadRequest("You already liked this user!")); } if (await _repo.GetUser(recipientId) == null) { return(NotFound("This user does not exist")); } like = new Like { LikerId = id, LikeeId = recipientId }; _repo.Add <Like>(like); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to like user")); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { var sender = await _repo.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _repo.GetUser(messageForCreationDto.RecipientId); if (recipient == null) { return(BadRequest("Could not find that user")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); // var messageToReturn = _mapper.Map<MessageToReturn>(message); // --> or using (as I am ) .reverseMap() but need the following to narrow down the response if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userId, message.Id }, messageToReturn)); } throw new Exception("Creating the message failed on save"); }