public async Task <IActionResult> FollowUser(int id, int recipientId) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var follow = await _repo.GetFollow(id, recipientId); if (follow != null) { return(BadRequest("You are already following this user.")); } if (await _repo.GetUser(recipientId) == null) { return(NotFound()); } follow = new Follow { FollowerId = id, FollowedId = recipientId }; _repo.Add <Follow>(follow); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to follow 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 user")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn)); } throw new Exception("Message save failed."); }