Beispiel #1
0
        public async Task <IActionResult> BookDetails([FromRoute] string id)
        {
            var currentUser = this.User.Identity.Name;
            var book        = await this.bookRepository.GetByIdAsync(id);

            var comments = await this.bookRepository.GetOneToManyAsync(b => b.BookId == id,
                                                                       bg => bg.Comments);

            await this.dataService.MatchCommentToUser(comments);

            var genres = await this.bookRepository.GetManyToManyAsync(b => b.BookId == id,
                                                                      bg => bg.BookGenres,
                                                                      g => g.Genre);

            var tags = await this.bookRepository.GetManyToManyAsync(b => b.BookId == id,
                                                                    bg => bg.BookTags,
                                                                    g => g.Tag);

            var bookUser = await this.bookUserRepository.GetByExpressionAsync(bu => bu.BookId == id);

            var bookViewModel = this.objectMapper.Map <Book, BookViewModel>(book);

            var commentObjects = this.objectMapper.Map <IEnumerable <Comment>, IEnumerable <CommentResponseModel> >(comments);

            await this.dataService.MatchUserToPicture(commentObjects);

            if (this.User.Identity.IsAuthenticated)
            {
                await this.dataService.CheckUserCommentRights(commentObjects, currentUser);
            }

            var commentsViewModel = this.objectMapper.Map <IEnumerable <CommentResponseModel>, IEnumerable <CommentViewModel> >(commentObjects);

            var propertiesViewModel = new BookPropertiesViewModel
            {
                Comments = commentsViewModel,
                Tags     = tags.ToList().Select(t => t.Value),
                Genres   = genres.ToList().Select(g => g.Name)
            };

            bool isRated    = bookUser == null || bookUser.HasRatedBook == false ? false : true;
            int  userRating = bookUser == null || bookUser.HasRatedBook == false ? 0 : bookUser.Rate;

            var singleBookViewModel = new SingleBookViewModel
            {
                Book       = bookViewModel,
                Properties = propertiesViewModel,
                IsRated    = isRated,
                UserRating = userRating
            };

            return(View(singleBookViewModel));
        }
Beispiel #2
0
        public async Task <IActionResult> BookDetails([FromRoute] string id)
        {
            var book = await this.bookRepository.GetByIdAsync(id);

            var comments = await this.bookRepository.GetOneToManyAsync(b => b.BookId == id,
                                                                       bg => bg.Comments);

            foreach (var comment in comments)
            {
                var user = await this._userManager.FindByIdAsync(comment.UserId);

                comment.User = user;
            }

            var genres = await this.bookRepository.GetManyToManyAsync(b => b.BookId == id,
                                                                      bg => bg.BookGenres,
                                                                      g => g.Genre);

            var tags = await this.bookRepository.GetManyToManyAsync(b => b.BookId == id,
                                                                    bg => bg.BookTags,
                                                                    g => g.Tag);

            var bookUser = await this.bookUserRepository.GetByExpressionAsync(bu => bu.BookId == id);

            var bookViewModel     = this.objectMapper.Map <Book, BookViewModel>(book);
            var commentsViewModel = this.objectMapper.Map <IEnumerable <Comment>, IEnumerable <CommentViewModel> >(comments);

            foreach (var comment in commentsViewModel)
            {
                var user = await this._userManager.FindByNameAsync(comment.Author);

                comment.AuthorPicUrl = user.ProfilePictureUrl;
            }


            if (this.User.Identity.IsAuthenticated)
            {
                foreach (var comment in commentsViewModel)
                {
                    var isAdmin = this.User.IsInRole("Admin");

                    var commentCreatorId = comment.UserId;
                    var currentUser      = await this.applicationUserRepository.GetByExpressionAsync(u => u.UserName == this.User.Identity.Name);

                    var isCreator = commentCreatorId == currentUser.Id;

                    comment.CanEdit = isAdmin || isCreator;
                }
            }

            var propertiesViewModel = new BookPropertiesViewModel
            {
                Comments = commentsViewModel,
                Tags     = tags.ToList().Select(t => t.Value),
                Genres   = genres.ToList().Select(g => g.Name)
            };

            bool isRated    = bookUser == null || bookUser.HasRatedBook == false ? false : true;
            int  userRating = bookUser == null || bookUser.HasRatedBook == false ? 0 : bookUser.Rate;

            var singleBookViewModel = new SingleBookViewModel
            {
                Book       = bookViewModel,
                Properties = propertiesViewModel,
                IsRated    = isRated,
                UserRating = userRating
            };

            return(View(singleBookViewModel));
        }