示例#1
0
        public GetBookViewModel GetAll()
        {
            var bookViews      = new List <GetBookViewItem>();
            var bookPublishers = _bookPublisherRepository.GetAll().GroupBy(x => x.Book);
            var bookAuthors    = _bookAuthorRepository.GetAll().GroupBy(x => x.Book);

            foreach (var bookPublisher in bookPublishers)
            {
                GetBookViewItem bookViewModel = Mapper.Map <GetBookViewItem>(bookPublisher.Key);

                var publishers          = bookPublisher.Select(a => a.Publisher);
                var publishersViewModel = Mapper.Map <List <GetPublisherViewItem> >(publishers);

                bookViewModel.Publishers.AddRange(publishersViewModel);
                bookViews.Add(bookViewModel);
            }

            foreach (var bookAuthor in bookAuthors)
            {
                var bookViewModel = bookViews.Where(x => x.Id == bookAuthor.Key.Id).SingleOrDefault();

                var authors          = bookAuthor.Select(a => a.Author);
                var authorsViewModel = Mapper.Map <List <GetAuthorViewItem> >(authors);

                bookViewModel.Authors.AddRange(authorsViewModel);
            }

            var result = new GetBookViewModel();

            result.Books = bookViews;

            return(result);
        }
示例#2
0
        public GetBookViewModel Get(long id)
        {
            var book = _bookRepository.Get(id);

            if (book == null)
            {
                throw new BusinessLogicException("Book not found");
            }

            var result = new GetBookViewModel()
            {
                Id                = book.Id,
                Name              = book.Name,
                AuthorId          = book.AuthorId,
                YearOfPublication = book.YearOfPublication
            };

            var author = _authorRepository.Get(result.AuthorId.Value);

            result.Author = new GetAuthorViewModel()
            {
                Id   = author.Id,
                Name = author.Name
            };
            return(result);
        }
示例#3
0
        public IActionResult GetDataBook(int idBook, int page)
        {
            GetBookViewModel vm = new GetBookViewModel()
            {
                thisPage  = (int)page,
                IdBook    = (int)idBook,
                CountPage = context.ChapterBook.Where(i => i.IdBook == idBook).Count(),
                content   = $"{context.ChapterBook.FirstOrDefault(i => i.IdBook == idBook && i.NumberChapter == page).ChapterContent}"
            };

            return(PartialView("GetDataBook", vm));
        }
示例#4
0
        public async Task <ViewResult> GetBook(int Id)
        {
            var book = await _bookRepository.GetBook(Id);

            var books = await _bookRepository.GetAllBooks();

            GetBookViewModel _bookViewModel = new GetBookViewModel();

            _bookViewModel.Book         = book;
            _bookViewModel.SimilarBooks = books;
            return(View(_bookViewModel));
        }
示例#5
0
        public GetBookViewModel GetBook(int bookId)
        {
            var dbResult           = this.bookReposetory.GetBook(bookId);
            GetBookViewModel model = new GetBookViewModel()
            {
                Book = new Book()
                {
                    Id            = Convert.ToInt32(dbResult.OutElements.ElementAt(0)),
                    Name          = dbResult.OutElements.ElementAt(1).ToString(),
                    NumberOfPages = dbResult.OutElements.ElementAt(2).ToString(),
                    Price         = Convert.ToInt32(dbResult.OutElements.ElementAt(3)),
                    BookCount     = Convert.ToInt32(dbResult.OutElements.ElementAt(4)),
                    Author        = new Author()
                    {
                        SurName    = dbResult.OutElements.ElementAt(5).ToString(),
                        Name       = dbResult.OutElements.ElementAt(6).ToString(),
                        MiddleName = dbResult.OutElements.ElementAt(7).ToString(),
                    },
                    PublishingHouse = new PublishingHouse()
                    {
                        Name = dbResult.OutElements.ElementAt(8).ToString()
                    },
                    Genere = new Genere()
                    {
                        Name = dbResult.OutElements.ElementAt(9).ToString()
                    }
                },
                Comments = new List <Comment>()
            };
            var commentDbResult = this.bookReposetory.GetBookComments(bookId);

            if (commentDbResult.Result == DbResult.Faild)
            {
                return(null);
            }
            for (int i = 0; i < commentDbResult.OutElements.Count; i += 4)
            {
                Comment comment = new Comment()
                {
                    Id       = Convert.ToInt32(commentDbResult.OutElements.ElementAt(i)),
                    Context  = commentDbResult.OutElements.ElementAt(i + 1).ToString(),
                    Customer = new User()
                    {
                        UserId = Convert.ToInt32(commentDbResult.OutElements.ElementAt(i + 2))
                    },
                };
                model.Comments.Add(comment);
            }
            return(model);
        }
示例#6
0
        public IHttpActionResult PutBook(int id, [FromUri] PutBookBindingModel bookModel)
        {
            var book = context.Books.Find(id);

            if (book == null)
            {
                return(this.BadRequest("Book doesn't exist"));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var author = context.Authors.FirstOrDefault(a => a.Id == bookModel.AuthorId);

            if (author == null)
            {
                return(this.BadRequest("Author doesn't exist in the provided book."));
            }

            book.Title       = bookModel.Title;
            book.Description = bookModel.Description;
            book.Price       = bookModel.Price;
            book.Copies      = bookModel.Copies;
            book.Edition     = bookModel.Edition;
            book.AuthorId    = bookModel.AuthorId;

            context.Books.AddOrUpdate(book);
            context.SaveChanges();

            var viewBook = new GetBookViewModel()
            {
                Title       = book.Title,
                Description = book.Description,
                Price       = book.Price,
                Copies      = book.Copies,
                AuthorId    = book.AuthorId,
                AuthorName  = book.Author.FirstName,
                Edition     = book.Edition,
                Categories  = book.Categories.Select(c => c.Name)
            };

            return(this.Ok(viewBook));
        }
示例#7
0
        public GetBookViewModel Delete(long id)
        {
            var book = _bookRepository.Get(id);

            if (book == null)
            {
                throw new BusinessLogicException("Book not found");
            }

            _publicationHouseInBookRepository.DeleteByBookId(id);

            _bookRepository.Delete(id);

            var result = new GetBookViewModel()
            {
                Id                = book.Id,
                Name              = book.Name,
                AuthorId          = book.AuthorId,
                YearOfPublication = book.YearOfPublication
            };

            return(result);
        }
示例#8
0
        public IHttpActionResult GetBook(int id)
        {
            var book = context.Books.Find(id);

            if (book == null)
            {
                return(this.BadRequest("Book doesn't exist."));
            }

            var viewBook = new GetBookViewModel()
            {
                Title       = book.Title,
                Description = book.Description,
                Price       = book.Price,
                Copies      = book.Copies,
                AuthorId    = book.AuthorId,
                AuthorName  = book.Author.FirstName,
                Edition     = book.Edition,
                Categories  = book.Categories.Select(c => c.Name)
            };

            return(this.Ok(viewBook));
        }
示例#9
0
        public IHttpActionResult GetBook(int id)
        {
            var book = context.Books.Find(id);

            if (book == null)
            {
                return this.BadRequest("Book doesn't exist.");
            }

            var viewBook = new GetBookViewModel()
            {
                Title = book.Title,
                Description = book.Description,
                Price = book.Price,
                Copies = book.Copies,
                AuthorId = book.AuthorId,
                AuthorName = book.Author.FirstName,
                Edition = book.Edition,
                Categories = book.Categories.Select(c => c.Name)
            };
            
            return this.Ok(viewBook);
        }
示例#10
0
        public IActionResult Get()
        {
            GetBookViewModel listBooks = _bookService.GetAll();

            return(Ok(listBooks));
        }
示例#11
0
        public IHttpActionResult PutBook(int id, [FromUri]PutBookBindingModel bookModel)
        {
            var book = context.Books.Find(id);

            if (book == null)
            {
                return this.BadRequest("Book doesn't exist");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var author = context.Authors.FirstOrDefault(a => a.Id == bookModel.AuthorId);

            if (author == null)
            {
                return this.BadRequest("Author doesn't exist in the provided book.");
            }

            book.Title = bookModel.Title;
            book.Description = bookModel.Description;
            book.Price = bookModel.Price;
            book.Copies = bookModel.Copies;
            book.Edition = bookModel.Edition;
            book.AuthorId = bookModel.AuthorId;

            context.Books.AddOrUpdate(book);
            context.SaveChanges();

            var viewBook = new GetBookViewModel()
            {
                Title = book.Title,
                Description = book.Description,
                Price = book.Price,
                Copies = book.Copies,
                AuthorId = book.AuthorId,
                AuthorName = book.Author.FirstName,
                Edition = book.Edition,
                Categories = book.Categories.Select(c => c.Name)
            };

            return this.Ok(viewBook);
        }