Пример #1
0
 public BookVM(Book book)
 {
     Title = book.Title;
     Published = book.FirstPublished ?? book.Published;
     TimesRead = book.Readings == null ? 0 : book.Readings.Count();
     Category = book.Category.Name;
     Author = string.Format(book.Authors.Count() == 1 ? "{0}" : "{0}, et al.", book.Authors.First().Name);
     Pages = book.Pages;
     Id = book.Id;
     LastRead = book.Readings != null && book.Readings.Any() ? book.Readings.First().Date : default(Nullable<DateTime>);
     Owned = book.Owned;
 }
Пример #2
0
 public CreateOrUpdateBookVM(Book book)
 {
     Title = book.Title;
     YearFirstPublished = book.FirstPublished.HasValue? book.FirstPublished.Value.Year : 0;
     YearPublished = book.Published.HasValue ? book.Published.Value.Year : 0;
     Pages = book.Pages;
     AuthorIds = book.Authors.Select(_ => _.Id).ToArray();
     CategoryId = book.CategoryId;
     Tags = book.Tags;
     Isbn = book.Isbn;
     Owned = book.Owned.HasValue ? book.Owned.Value : false;
     Id = book.Id;
 }
Пример #3
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);
        }