Example #1
0
        public async Task <CommentLikeStatusDTOout> SetVoteAsync(CommentVoteDTOin commentVote, string userId)
        {
            Attitude        newAttitude = commentVote.Like ? Attitude.Like : Attitude.Dislike;
            CommentAttitude voteFd      = await this.atttitudeRepo.All().FirstOrDefaultAsync(x => x.ShitGiverId == userId && x.CommentId == commentVote.Id);

            if (voteFd != null && voteFd.Attitude == newAttitude)
            {
                throw new ArgumentException("Same attitude can not be reasigned!");
            }
            if (voteFd is null)
            {
                await this.atttitudeRepo.AddAssync(new CommentAttitude
                {
                    ShitGiverId = userId,
                    CommentId   = commentVote.Id,
                    Attitude    = newAttitude
                });
            }
            else
            {
                voteFd.IsDeleted = false;
                voteFd.Attitude  = newAttitude;
            }
            await atttitudeRepo.SaveChangesAsync();

            return(await this.commentRepo.All().Where(x => x.Id == commentVote.Id && !x.IsDeleted).To <CommentLikeStatusDTOout>().FirstOrDefaultAsync());
        }
        public async Task <ActionResult <CommentLikeStatusDTOout> > ChangeVote(CommentVoteDTOin commentVote)
        {
            try
            {
                CommentLikeStatusDTOout newStatus = await this.commentService.SetVoteAsync(commentVote, UserId);

                return(newStatus);
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(new { reason = ex.Message }));
            }
        }