Ejemplo n.º 1
0
        public async Task <MemeDetailsVM> GetMemeDetailsById(int id, System.Security.Claims.ClaimsPrincipal user)
        {
            var entity = await _repository.FindAsync(id);

            if (entity == null)
            {
                throw new MemeSiteException(HttpStatusCode.NotFound, "Not Found");
            }
            var memeVM = new MemeDetailsVM()
            {
                MemeId       = entity.MemeId,
                Title        = entity.Title,
                Txt          = entity.Txt,
                UserId       = entity.UserID,
                UserName     = _userManager.FindByIdAsync(entity.UserID).Result.UserName,
                ByteHead     = entity.ByteHead,
                ByteImg      = entity.ImageByte,
                Category     = await _categoryService.GetCategoryVM(entity.CategoryId),
                CreationDate = entity.CreationDate.ToString("dd/MM/yyyy"),
                Rate         = await _voteService.GetMemeRate(entity.MemeId),
                CommentCount = await _commentService.CountAsync(m => m.MemeRefId == entity.MemeId),
                IsAccepted   = entity.IsAccepted,
                IsArchived   = entity.IsArchived,
                IsVoted      = false,
                IsFavourite  = false,
                VoteValue    = null,
            };

            if (user != null && user.Identity.IsAuthenticated == true)
            {
                string userId = user.Claims.First(c => c.Type == "UserID").Value;
                memeVM.IsVoted = await _voteService.IsExistAsync(m => m.MemeRefId == id && m.UserId == userId);

                memeVM.VoteValue = await _voteService.GetValueIfExist(entity.MemeId, userId);

                memeVM.IsFavourite = await _favouriteService.IsExistAsync(m => m.MemeRefId == id && m.UserId == userId);
            }
            return(memeVM);
        }