public async Task <ActionResult> AddLike(string username) { var liker = await _likesRepository.GetUserWithLikes(User.GetUserId()); var likee = await _userRepository.GetUserByUsernameAsync(username); var erroneousActionResult = await ValidateLikeAsync(liker, likee); if (erroneousActionResult is not null) { return(erroneousActionResult); } var userLike = new UserLike { LikerId = liker.Id, LikeeId = likee.Id }; liker.LikedUsers.Add(userLike); // TODO: figure out a better way to save changes, as we now have two repos, each of them could potentially save the changes if (!await _userRepository.SaveAllAsync()) { return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } return(Ok()); }
public async Task <ActionResult> AddLike(string username) { var sourceUserId = User.GetUserId(); var likedUser = await _userRepository.GetUserByUsernameAsync(username); var SourceUser = await _likesRepository.GetUserWithLikes(sourceUserId); if (likedUser == null) { return(NotFound()); } if (SourceUser.UserName == username) { return(BadRequest("You cannot like yourself")); } var userLike = await _likesRepository.GetUserLike(sourceUserId, likedUser.Id); if (userLike != null) { return(BadRequest("You already liked this user")); } userLike = new UserLike { SourceUserId = sourceUserId, LikedUserID = likedUser.Id }; SourceUser.LikedUsers.Add(userLike); if (await _userRepository.SaveAllAsync()) { return(Ok()); } return(BadRequest("Failed to like user")); }
public async Task <ActionResult> AddLike(string username) { var sourceUserId = User.GetUserId(); // get User we are liking var likedUser = await _userRepository.GetUserByUserNameAsync(username); // get SourceUser with associated Likes var sourceUser = await _likesRepository.GetUserWithLikes(sourceUserId); // check if liked user exists if (likedUser == null) { return(NotFound()); } // prevent user from liking themselves if (sourceUser.Id == likedUser.Id) { return(BadRequest("You cannot like yourself.")); } // check if UserLike already exists var userLike = await _likesRepository.GetUserLike(sourceUserId, likedUser.Id); if (userLike != null) { return(BadRequest("You have already liked this user.")); } // if UserLike does not already exist then create a new UserLike userLike = new UserLike { SourceUserId = sourceUserId, // set SourceUserId LikedUserId = likedUser.Id // set LikedUserId // other fields will be null }; // add Like to SourceUser sourceUser.LikesSent.Add(userLike); // save changes if (await _userRepository.SaveAllAsync()) { return(Ok()); } // if unsucessfull return(BadRequest("Failed to Like user.")); }
public async Task <ActionResult> AddLike(string username) { // User logged in var sourceUserId = User.GetUserId(); // Input user to be liked var likedUser = await _userRepository.GetUserByUserNameAsync(username); // User doint the like var sourceUser = await _likesRepository.GetUserWithLikes(sourceUserId); if (likedUser == null) { return(NotFound()); } if (sourceUser.UserName == username) { return(BadRequest("You cannot like yourself in this App")); } var userLike = await _likesRepository.GetUserLike(sourceUserId, likedUser.Id); if (userLike != null) { return(BadRequest("You already like this person.")); } userLike = new UserLike { SourceUserId = sourceUserId, LikedUserId = likedUser.Id }; sourceUser.LikedUsers.Add(userLike); if (await _userRepository.SaveAllAsync()) { return(Ok()); } return(BadRequest("Failed to like user.")); }
public async Task <ActionResult> AddLike(string username) { var sourceUserId = User.GetUserId(); var likedUser = await _userRepository.GetByUsernameAsync(username); var sourceUser = await _likesRepository.GetUserWithLikes(sourceUserId); if (likedUser == null) { return(NotFound()); } if (sourceUser.UserName == username) { return(BadRequest("You're likeing yourself!?'")); } var userLike = await _likesRepository.GetUserLike(sourceUserId, likedUser.Id); // TODO: Make it possible to remove a like if (userLike != null) { return(BadRequest("You already liked this user.")); } userLike = new UserLike { SourceUserId = sourceUserId, LikedUserId = likedUser.Id }; sourceUser.LikedUsers.Add(userLike); // TODO: Unit of work pattern here... if (await _userRepository.SaveAllAsync()) { return(Ok()); } return(BadRequest("Failed to like user")); }
public async Task <ActionResult> AddLike(string username) { var sourceUserId = User.GetUserId(); var likedUser = await userRepository.GetUserByUsernameAsync(username); var sourceUser = await likesRepository.GetUserWithLikes(sourceUserId); if (likedUser == null) { return(NotFound()); } if (sourceUser.Username == username) { return(BadRequest("Not possible to like yourself")); } var userLike = await likesRepository.GetUserLike(sourceUserId, likedUser.Id); if (userLike != null) { return(BadRequest("Voce ja curtiu")); } //se nao tiver um userLike, cria um userLike = new UserLike { SourceUserId = sourceUserId, LikedUserId = likedUser.Id }; sourceUser.LikedUser.Add(userLike); if (await userRepository.SaveAllAsync()) { return(Ok()); } return(BadRequest("Failed to Like user")); }
public async Task <ActionResult> AddLike(string username) { var sourceUserId = await userRepository.GetUserByUsernameAsync(User.FindFirstValue(ClaimTypes.NameIdentifier)); var likedUser = await userRepository.GetUserByUsernameAsync(username); var sourceUser = await likesRepository.GetUserWithLikes(sourceUserId.Id); if (likedUser == null) { return(NotFound()); } if (sourceUser.UserName == username) { return(BadRequest("You cannot like yourself")); } var userLike = await likesRepository.GetUserLike(sourceUserId.Id, likedUser.Id); if (userLike != null) { return(BadRequest("You already like this user")); } userLike = new UserLike { SourceUserId = sourceUserId.Id, LikedUserId = likedUser.Id }; sourceUser.LikedUsers.Add(userLike); if (await userRepository.SaveAllAsync()) { return(Ok()); } return(BadRequest("Failed to like user")); }
public async Task <ActionResult> AddLike(string userName) { var likerId = User.GetUserId(); var likee = await _userRepository.GetUserByUsername(userName); var liker = await _likesRepository.GetUserWithLikes(likerId); if (likee == null) { return(NotFound()); } if (liker.UserName == userName) { return(BadRequest("You cannot like yourself")); } var userLike = await _likesRepository.GetUserLike(likerId, likee.Id); if (userLike != null) { return(BadRequest("You already like this user")); } userLike = new UserLike() { LikerId = likerId, LikeeId = likee.Id, }; liker.Likees.Add(userLike); if (await _userRepository.SaveAll()) { return(Ok()); } return(BadRequest("Failed to like user")); }
public async Task <ActionResult> AddLike(string username) { var sourceUserId = User.GetUserId(); var likedUser = await userRepo.GetUserByUsernameAsync(username); var sourceUser = await likesRepo.GetUserWithLikes(sourceUserId); if (likedUser == null) { return(NotFound()); } if (sourceUser.UserName == username) { return(BadRequest("You cannot like your own profile.")); } var userLike = await likesRepo.GetUserLike(sourceUserId, likedUser.Id); if (userLike != null) { return(BadRequest("You have already liked this user.")); } userLike = new UserLike { SourceUserId = sourceUserId, LikedUserId = likedUser.Id }; sourceUser.LikedUsers.Add(userLike); if (await userRepo.SaveAllAsync()) { return(Ok()); } return(BadRequest("Unable to like user.")); }
public async Task <ActionResult> AddLike(string username) { var sourceUserId = User.GetUserId(); var likedUser = await _userRepository.GetUserByUsernameAsync(username); var sourceUser = await _likesRepository.GetUserWithLikes(sourceUserId); if (likedUser == null) { return(NotFound()); } if (sourceUser.UserName == username) { return(BadRequest("non puoi masturbarti")); } var userLike = await _likesRepository.GetUserLike(sourceUserId, likedUser.Id); if (userLike != null) { return(BadRequest("ancora! cazzo insisti")); } userLike = new UserLike { SourceUserId = sourceUserId, LikedUserId = likedUser.Id }; sourceUser.LikedUsers.Add(userLike); if (await _userRepository.SaveAllAsync()) { ; } return(Ok()); }
[HttpPost("{username}")] // We need to extract the username from the route parameters public async Task <ActionResult> AddLike(string username) { var sourceUserId = User.GetUserId(); var likedUser = await _userRepository.GetUserByUsernameAsync(username); var sourceUser = await _likesRepository.GetUserWithLikes(sourceUserId); if (likedUser == null) { return(NotFound()); } if (sourceUser.UserName == username) { return(BadRequest("you cannot like yourself")); } var userLike = await _likesRepository.GetUserLike(sourceUserId, likedUser.Id); // if the userlike is not found here then that means the user hasn't already been liked by the logged user if (userLike != null) { return(BadRequest("You already liked this user")); } userLike = new UserLike { SourceUserId = sourceUserId, // We only need to add this two properties to our UserLike entity as this is the only two columns we have LikedUserId = likedUser.Id }; sourceUser.LikedUsers.Add(userLike); if (await _userRepository.SaveAllAsync()) { return(Ok()); } return(BadRequest("Failed to like user")); }
public async Task <ActionResult> AddLike(string userName) { var sourceUserId = User.GetUserId(); var likedUser = await _usersRepository.GetUserByUserNameAsync(userName); var sourceUser = await _likesRepository.GetUserWithLikes(sourceUserId); if (likedUser == null) { return(NotFound()); } if (sourceUser.UserName == userName) { return(BadRequest("Easy there Narcissus!")); } var userLike = await _likesRepository.GetUserLike(sourceUserId, likedUser.Id); if (userLike != null) { return(BadRequest("You already like this user")); } userLike = new UserLike { LikedUserId = likedUser.Id, SourceUserId = sourceUserId }; sourceUser.LikedUsers.Add(userLike); if (await _usersRepository.SaveAllChangesAsync()) { return(Ok()); } return(BadRequest("Failed to like user")); }
public async Task <IActionResult> AddLike(string username) { var sourceUserId = User.GetUserId(); var likedUser = await _userRepository.GetUserByUsernameAsync(username); //Get User duoc Like var sourceUser = await _likeRepository.GetUserWithLikes(sourceUserId); // Get minh ra tu bang like if (likedUser == null) { return(NotFound()); } if (sourceUser.UserName == username) { return(BadRequest("Không thể từ thích bản thân")); } var userLike = await _likeRepository.GetUserLike(sourceUserId, likedUser.Id); if (userLike != null) { return(BadRequest("Bạn đã thích người này")); } userLike = new UserLike { SourceUserId = sourceUserId, LikedUserId = likedUser.Id }; sourceUser.LikedUsers.Add(userLike); if (await _userRepository.SaveAllAsync()) { return(Ok()); } return(BadRequest("Fail to like user")); }
public async Task <ActionResult> AddLike(string username) { var userIdSource = User.GetUserId(); var sourceUser = await _likesRepository.GetUserWithLikes(userIdSource); var likedUser = await _userRepository.GetUserByUsernameAsync(username); if (sourceUser.UserName == username) { return(BadRequest("You can't like yourself")); } bool hasPreviousLike = (await _likesRepository.GetUserLike(sourceUser.Id, likedUser.Id) != null) ? true : false; if (hasPreviousLike) { return(BadRequest("You already like this user")); } var userLike = new UserLike() { SourceUserId = sourceUser.Id, SourceUser = sourceUser, LikedUserId = likedUser.Id, LikedUser = likedUser }; sourceUser.LikedUsers.Add(userLike); if (await _userRepository.SaveAllAsync()) { return(Ok()); } return(BadRequest("A problem occurred adding the like to the user")); }