Esempio n. 1
0
        public BaseResponse <LikeOutputDto> ChangeLikeStatus(LikeInputDto likeInputDto)
        {
            var like      = Mapper.Map <Like>(likeInputDto);
            var likeFound = FirstOrDefault(x => x.BookId == like.BookId && x.UserId == like.UserId);

            if (likeFound == null)
            {
                likeFound = Create(like, out var isSaved);
                if (!isSaved)
                {
                    throw new InternalServerErrorException($"Không thể like book id: {likeInputDto.BookId}");
                }
            }
            else
            {
                likeFound.EntityStatus = likeFound.IsActivated() ? EntityStatus.Deleted : EntityStatus.Activated;
                var isSaved = Update(likeFound);
                if (!isSaved)
                {
                    throw new InternalServerErrorException("Something went wrong, try again later");
                }
            }

            var book = _bookService.Find(Guid.Parse(likeInputDto.BookId));

            book.LikedCount += likeFound.IsActivated() ? 1 : -1;
            _bookService.Update(book);

            return(new SuccessResponse <LikeOutputDto>(Mapper.Map <LikeOutputDto>(likeFound)));
        }
Esempio n. 2
0
 public BaseResponse <LikeOutputDto> ChangeLikeStatus([FromBody] LikeInputDto likeInputDto)
 {
     return(_likeService.ChangeLikeStatus(likeInputDto));
 }