예제 #1
0
        public ActionResult Edit(CreateOrUpdateBookVM book)
        {
            if (this.ModelState.IsValid)
            {
                var bookToUpdate = _repo.AllIncluding(_ => _.Category, _ => _.Authors, _ => _.Readings)
                    .Single(_ => _.Id == book.Id);
                                
                bookToUpdate.State = State.Modified;
                bookToUpdate.CategoryId = book.CategoryId;
                bookToUpdate.FirstPublished = new DateTime(book.YearFirstPublished, 1, 1);
                bookToUpdate.Published = new DateTime(book.YearPublished, 1, 1);
                bookToUpdate.Isbn = book.Isbn;
                bookToUpdate.Pages = book.Pages;
                bookToUpdate.Tags = book.Tags;
                bookToUpdate.Title = book.Title;                
                bookToUpdate.Owned = book.Owned;

                bookToUpdate.Authors = _authorRepo.All.Where(_ => book.AuthorIds.Contains(_.Id)).ToList();
                
                _repo.InsertOrUpdate(bookToUpdate);
                _unitOfWork.Save();

                return RedirectToAction("Details", new { id = bookToUpdate.Id });
            }

            ViewData["Categories"] = new SelectList(_categoryRepository.All.ToList(), "Id", "Name", 1);
            return View(book);
        }
예제 #2
0
        public ActionResult Create(CreateOrUpdateBookVM book)
        {
            if (this.ModelState.IsValid)
            {
                var bookToAdd = new Book()
                {
                    State = State.Added,
                    CategoryId = book.CategoryId,
                    FirstPublished = new DateTime(book.YearFirstPublished, 1, 1),
                    Published = new DateTime(book.YearPublished, 1, 1),
                    Isbn = book.Isbn,
                    Pages = book.Pages,
                    Tags = book.Tags,
                    Title = book.Title,
                    Authors = book.AuthorIds.Select(_ => new Author() { Id = _ }).ToList(),
                    Owned = book.Owned
                };

                _repo.InsertOrUpdateGraph(bookToAdd);
                _unitOfWork.Save();
               
                return RedirectToAction("Details", new { id = bookToAdd.Id });
            }

            ViewData["Categories"] = new SelectList(_categoryRepository.All.ToList(), "Id", "Name", 1);
            return View(book);
        }