public async Task <IActionResult> PostLike(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("already liked the user")); } if (_repo.GetUser(recipientId) == null) { return(NotFound()); } like = new Like() { LikeeId = recipientId, LikerId = id }; _repo.Add <Like>(like); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("failed to like to user")); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO messageForCreationDTO) { //this line of code is added for the message photo display 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("Colud not Found the 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. "); }
public async Task <IActionResult> LikeUser(int id, int recepientId) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var like = await _datingRepo.GetLike(id, recepientId); if (like != null) { return(BadRequest("You already liked this user.")); } if (await _datingRepo.GetUser(recepientId) == null) { return(NotFound()); } like = new Like { LikerId = id, LikeeId = recepientId }; _datingRepo.Add <Like>(like); // this is not async as this is not adding to db at this point,. if (await _datingRepo.SaveAll()) { return(Ok()); } return(BadRequest("Unable to like user")); }
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, true); if (recipient == null) { return(BadRequest("Could not found user...")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); // TODO: CreatedAtRoute() return(CreatedAtRoute("GetMessage", new { controller = "Messages", userId, id = message.Id }, messageToReturn)); } throw new Exception("Creating the message failed on saved"); }
public async Task <IActionResult> LikeUser(int id, int recipientId) { //Checking user is authorized or not 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 Models.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 _datingRepo.GetUser(userId); // some random shit ? video 172 from course for blocking some automapper shit if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _datingRepo.GetUser(messageForCreationDto.RecipientId); if (recipient == null) { BadRequest("Could not find user"); } var message = _mapper.Map <Message>(messageForCreationDto); _datingRepo.Add(message); if (await _datingRepo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn)); } return(BadRequest("Couldnt create message")); }
public async Task <IActionResult> CreateMessage(int userId, MessageCreateDto messageCreateDto) { var sender = await _repo.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageCreateDto.SenderId = userId; var receipient = await _repo.GetUser(messageCreateDto.RecipientId); if (receipient == null) { return(BadRequest("receipient not found")); } var message = _mapper.Map <Message>(messageCreateDto); _repo.Add(message); var messageToReturn = _mapper.Map <MessageReturnDto>(message); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new Exception("fail to creat message"); }
public async Task <IActionResult> LikeUser(int id, int likeuserid) { try { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var likes = await _datingrepo.GetLikes(id, likeuserid); if (likes != null) { return(BadRequest("You have already liked the user!")); } if (await _datingrepo.GetUser(likeuserid) == null) { return(NotFound()); } var like = new Likes { LikeByUserID = id, LikedUserID = likeuserid }; _datingrepo.Add(like); if (await _datingrepo.SaveAll()) { return(Ok()); } throw new Exception($"Error adding user likeuserid !"); } catch (Exception ex) { throw new Exception($"Error adding like user likeuserid !"); } }
public async Task <IActionResult> CreateMessage(int userid, MessageCreateDto messageParams) { try { var sender = await _datingrepo.GetUser(userid); if (sender.UserID != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageParams.SenderID = userid; var receiver = await _datingrepo.GetUser(messageParams.ReceiverID); if (receiver == null) { return(BadRequest("Receiving user does not exists")); } var message = _mapper.Map <Messages>(messageParams); _datingrepo.Add(message); if (await _datingrepo.SaveAll()) { var returnMessage = _mapper.Map <MessageReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userid = userid, messageid = message.MessageID }, returnMessage)); } throw new Exception("Error while creating message"); } catch (Exception ex) { throw new Exception("Error while creating message"); } }