Ejemplo n.º 1
0
        public async Task <Result <LikeViewModel> > CreateLikePostAsync(int?postId, bool isLike)
        {
            if (_userContext.UserId == -1 || !await _userQueryRepository.IsExistUserAsync(_userContext.UserId))
            {
                return(Result.Fail <LikeViewModel>(EC.UserNotFound, ET.UserNotFound));
            }

            if (!postId.HasValue || !await _postQueryRepository.IsExistPostAsync(postId.Value))
            {
                return(Result.Fail <LikeViewModel>(EC.PostNotFound, ET.PostNotFound));
            }

            var like = new Like
            {
                IsLike = isLike,
                UserId = _userContext.UserId,
                PostId = postId
            };

            await _likeRepository.PostAsync(like);

            await _unitOfWorks.CommitAsync();

            var model = new LikeViewModel
            {
                Id     = like.Id,
                IsLike = isLike,
                PostId = postId,
                UserId = _userContext.UserId
            };

            return(Result.OK(model));
        }