public async Task <ActionResult <PagedList <LikeDto> > > GetUserLikes([FromQuery] LikesParams likesParams) { likesParams.UserId = User.getUserId(); var users = await _likesRepository.GetUserLikes(likesParams); Response.AddPaginationHeader(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages); return(Ok(users)); }
public async Task <ActionResult <PagedList <LikeDto> > > GetUserLikes([FromQuery] LikeParams likeParams) { // sử dụng 2 tham số nen tao 2 property trong likeParam la predicate , UserId likeParams.UserId = User.getUserId(); // tra ve 1 page list cho user var users = await this._unitOfWork.LikeRepository.GetUserLikes(likeParams); // add vài Header là một Extension để client lấy ra Response.AddPaginationHeader(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages); return(Ok(users)); }
public async Task <ActionResult> AddLike(string username) { // user login var sourceUserId = User.getUserId(); // lay user khi user khi login var likeUser = await this._unitOfWork.UserRepository.GetUserByUserNameAsync(username); // tra ve User co like var sourceUser = await this._unitOfWork.LikeRepository.GetUserWithLikes(sourceUserId); if (likeUser == null) { return(NotFound()); // khong tim thay user ma ho muôn like } if (sourceUser.UserName == username) { return(BadRequest("you can't like your self")); } // nếu user like người mình đã like========================================== var userLike = await this._unitOfWork.LikeRepository.GetUserLike(sourceUserId, likeUser.Id); if (userLike != null) { return(BadRequest("you already like this user")); } // ========================================================================== // neu khong co thi tao moi userLike = new UserLike { SourceUserId = sourceUserId, LikedUserId = likeUser.Id }; sourceUser.LikedUsers.Add(userLike); if (await this._unitOfWork.Complete()) { return(Ok()); } return(BadRequest("Fail 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("You cannot like yourself")); } var userLike = await _likesRepository.GetUserLike(sourceUserId, likedUser.Id); if (userLike != null) { return(BadRequest("User already liked")); } userLike = new UserLike { SourceUserId = sourceUserId, LikedUserId = likedUser.Id }; sourceUser.LikedUsers.Add(userLike); if (await _userRepository.SaveAllAsync()) { return(Ok("User Liked")); } return(BadRequest("Failed to like User")); }