public SuccessPostViewModel PostLike(PostLikeInputViewModel likeInputViewModel) { SuccessPostViewModel result = new SuccessPostViewModel(); try { string token = GetRequestHeader(); User user = UnitOfWork.UserRepository.GetByToken(token); if (user != null) { if (!LikeAndDislike(likeInputViewModel, user.Id)) { result.Result = null; result.Status = status.ReturnStatus(100, Resources.Messages.InvalidContent, false); } result.Result = Resources.Messages.SuccessPost; result.Status = status.ReturnStatus(0, Resources.Messages.SuccessPost, true); } else { result.Result = null; result.Status = status.ReturnStatus(100, Resources.Messages.InvalidUser, false); } } catch (Exception) { result.Result = null; result.Status = status.ReturnStatus(100, Resources.Messages.CatchError, false); } return(result); }
public bool LikeAndDislike(PostLikeInputViewModel likeInputViewModel, Guid userId) { Guid contentId = new Guid(likeInputViewModel.ContentId); Content content = UnitOfWork.ContentRepository.GetById(contentId); if (content == null) { return(false); } ContentLike contentLike = UnitOfWork.ContentLikeRepository .Get(current => current.ContentId == contentId && current.UserId == userId).FirstOrDefault(); if (contentLike != null) { if (contentLike.IsLike != likeInputViewModel.IsLike) { contentLike.IsLike = likeInputViewModel.IsLike; UnitOfWork.ContentLikeRepository.Update(contentLike); UnitOfWork.Save(); } } else { ContentLike oContentLike = new ContentLike() { UserId = userId, ContentId = contentId, IsLike = likeInputViewModel.IsLike, IsActive = true }; UnitOfWork.ContentLikeRepository.Insert(oContentLike); UnitOfWork.Save(); } return(true); }