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 like this user")); } if (await _repo.GetUser(recipientId) == null) { return(NotFound()); } 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 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("Creating the message failed on save"); }
public async Task <IActionResult> GetLike(int id, int recipientId) { if (await _repo.GetUser(id) == null) { return(Unauthorized()); } if (await _repo.GetUser(recipientId) == null) { return(NotFound()); } var like = await _repo.GetLike(id, recipientId); if (like != null) { return(BadRequest("You Already liked this user")); } like = new Like { LikeeId = recipientId, LikerId = id }; _repo.Add <Like>(like); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to like User")); }
public async Task <IActionResult> CreateMessage(int userId, MessageDTO messageDTO) { var sender = await _repo.GetUser(userId); if (sender == null) { return(BadRequest("Can not find the user")); } if (sender.id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageDTO.senderId = userId; var recipient = await _repo.GetUser(messageDTO.recipientId); if (recipient == null) { return(BadRequest("Can not find the user")); } var _message = _mapper.Map <Message>(messageDTO); _repo.Add(_message); if (await _repo.SaveAll()) { var _messageToReturn = _mapper.Map <MessageDetailDTO>(_message); return(CreatedAtRoute("GetMessage", new { id = _message.id }, _messageToReturn )); } return(BadRequest("Unable to create message")); }
public IActionResult AddUser(User user) { _repo.Add(user); return(Ok()); }