public IActionResult Detail([FromRoute] int id)
        {
            if (id == 0)
            {
                var vm = new BookEditDetailViewModel
                {
                    CreationDate = DateTime.Now
                };

                return(View("Detail", vm));
            }
            else
            {
                var book = _bookService.GetBookById(id);
                if (book == null)
                {
                    return(NotFound());
                }

                var vm = ConvertBookToEditDetailViewModel(book);
                vm.Genres = _bookService.GetAllGenres().Select(x => new SelectListItem
                {
                    Text  = x.Name,
                    Value = x.Id.ToString(),
                }
                                                               ).ToList();
                return(View(vm));
            }
        }
Esempio n. 2
0
        public BookEditDetailViewModel ConvertBookToEditDetailViewModel(Book book)
        {
            var vm = new BookEditDetailViewModel
            {
                Id           = book.Id,
                Title        = book.Title,
                CreationDate = book.CreationDate,
                Genre        = book.Genre?.Name,
                GenreId      = book.Genre?.Id
            };

            return(vm);
        }
Esempio n. 3
0
        public IActionResult Persist([FromForm] BookEditDetailViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var book = vm.Id == 0 ? new Book() : _bookService.GetBookById(vm.Id);
                book.Title        = vm.Title;
                book.Genre        = vm.GenreId.HasValue ? _bookService.GetGenreById(vm.GenreId.Value) : null;
                book.CreationDate = vm.CreationDate;
                book.ISBN         = vm.ISBN;
                _bookService.Persist(book);

                return(Redirect("/books"));
            }
            return(View("Detail", vm));
        }