Пример #1
0
        public ActionResult UpdateLike(int bookId, bool?like, string returnUrl)
        {
            try
            {
                int userID = (int)Profile["ID"];
                var book   = manager.bookService.GetBookById(bookId);
                var dbLike = manager.commentService.GetBookLikes(book).FirstOrDefault(e => e.UserID == userID);
                if (dbLike != null)
                {
                    if (like == null)
                    {
                        manager.commentService.RemoveLike(dbLike);
                    }
                    else
                    {
                        manager.commentService.RemoveLike(dbLike);
                        dbLike.ID   = 0;
                        dbLike.Like = (bool)like;
                        manager.commentService.AddLike(dbLike);
                    }
                }
                else if (like != null)
                {
                    manager.commentService.AddLike(new ServiceLike()
                    {
                        BookID = bookId,
                        UserID = userID,
                        Like   = (bool)like
                    });
                }

                if (Request.IsAjaxRequest())
                {
                    if (returnUrl != null && returnUrl == "details")
                    {
                        return(PartialView("_LikeProgressButtonsView", Like.GetLikeButtonsModel(bookId, userID)));
                    }
                    return(PartialView("_LikeButtonsView", Like.GetLikeButtonsModel(bookId, userID)));
                }
                else
                {
                    returnUrl = returnUrl ?? "";
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return(Redirect(returnUrl));
                    }
                    return(RedirectToAction("Details", "Books", new { id = bookId }));
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return(View("Error"));
            }
        }
Пример #2
0
        public static BookShortModel ToBookShortModel(this ServiceFullBook sfb, int userID)
        {
            var author = sfb.Authors.FirstOrDefault();

            return(new BookShortModel()
            {
                ID = sfb.BookData.ID,
                Name = sfb.BookData.Name,
                Likes = Like.GetLikeButtonsModel(sfb.BookData.ID, userID),
                Cover = sfb.Covers.FirstOrDefault()?.ImagePath,
                Author = new AuthorShortModel()
                {
                    ID = author?.ID ?? 0, Name = author?.Name, PhotoPath = author?.Photo
                }
            });
        }