public ActionResult AddBook() { BookBs bs = new BookBs(); AuthorBs authorBs = new AuthorBs(); BookEditModel model = new BookEditModel(); model.Book = new BookModel(); model.Authors = authorBs.GetList().Select(c => (AuthorModel)c).ToList(); LibraryBs libraryBs = new LibraryBs(); model.Libraries = libraryBs.GetList().Select(c => (LibraryModel)c).ToList(); DepartmentBs departmentBs = new DepartmentBs(); var lib = model.Libraries.FirstOrDefault(); if (lib != null) { model.SelectedLib = lib.Id; var departments = departmentBs.GetList().Where(c => c.LibraryId == model.SelectedLib).ToList(); if (departments != null) { model.Departments = departments.Select(c => (DepartmentModel)c).ToList(); } } return(View(model)); }
public async Task <IActionResult> Create([FromBody] BookEditModel book) { if (ModelState.IsValid) { BookResultModel response = await this.bookService.UpdateBook(book.Id, book.Name, book.Description, book.ImageUrl, book.Author, book.ReleaseDate, book.Categories); if (!response.Success) { FailedResponseModel badResponse = new FailedResponseModel() { Errors = response.Errors }; return(BadRequest(badResponse)); } BookSuccessResponseModel successResponse = new BookSuccessResponseModel() { Name = response.Name }; return(Ok(successResponse)); } return(BadRequest(new FailedResponseModel { Errors = ModelState.Values.SelectMany(x => x.Errors.Select(y => y.ErrorMessage)) })); }
// Demo 4 public ActionResult EditorTemplate() { var books = new Book[] { new Book { Title = "Curious George", Author = "H.A. Rey", DatePublished = DateTime.Parse("1973/2/23") }, new Book { Title = "Code Complete", Author = "H.A. Rey", DatePublished = DateTime.Parse("2004/6/9") }, new Book { Title = "The Two Towers", Author = "H.A. Rey", DatePublished = DateTime.Parse("2005/6/1") }, new Book { Title = "Homeland", Author = "RA Salvatore", DatePublished = DateTime.Parse("1990/9/19") }, new Book { Title = "Moby Dick", Author = "Herman Melville", DatePublished = DateTime.Parse("2008/1/1") } }; var model = new BookEditModel { Books = books.ToList() }; return(View(model)); }
public ActionResult EditBook(BookEditModel model) { if (ModelState.IsValid) { if (model.Book.DepartmentId == 0) { model.Book.DepartmentId = null; } BookBs bs = new BookBs(); var result = bs.Update((BookDTO)model); if (result.Code == BusinessLayer.OperationStatusEnum.Success) { TempData["OperationStatus"] = true; TempData["OpearionMessage"] = "Данные успешно обновлены"; return(RedirectToAction("Books", "ProviderPage")); } else { TempData["OperationStatus"] = false; TempData["OpearionMessage"] = result.Message; } } model.Authors = providerContext.Authors().Select(c => (AuthorModel)c).ToList(); DepartmentBs dbs = new DepartmentBs(); model.Departments = dbs.GetList().Where(c => c.LibraryId == model.SelectedLib).Select(c => (DepartmentModel)c).ToList(); return(View(model)); }
public string EditBook2(BookEditModel book) { try { BookService bookService = new BookService(); Book dbBook = bookService.GetBookByID(book.ID); if (dbBook == null) { return($"Could not find a book with the given ID: {book.ID}"); } dbBook.Author = book.Author; dbBook.Description = book.Description; dbBook.Genre = book.Genre; dbBook.Quantity = book.Quantity; dbBook.Title = book.Title; dbBook.CreatedDate = DateTime.Now; bookService.EditBook(dbBook); return("Book is saved successfully"); } catch (Exception ex) { return($"Failed to save the book. Error: {ex.GetBaseException().Message}"); } }
public ActionResult Edit(int id) { var model = new BookEditModel(); if (id > 0) { var book = db.Books.Where(m => m.Id == id) .FirstOrDefault(); if (book != null) { model.Id = book.Id; model.Title = book.Title; model.Description = book.Description; model.Year = book.Year; model.CoverUrl = book.CoverUrl; model.AuthorId = book.AuthorId; model.CategoryId = book.CategoryId; } } model.Authors = db.Authors .Select(m => new SelectListItem { Value = m.Id.ToString(), Text = m.Name, Selected = m.Id == model.AuthorId }) .ToList(); model.Categories = db.Categories .Select(m => new SelectListItem { Value = m.Id.ToString(), Text = m.Title, Selected = m.Id == model.CategoryId }) .ToList(); return(View(model)); }
public async Task <IActionResult> Edit(int id, BookEditModel model) { if (ModelState.IsValid) { Book book = new Book { Id = id, Caption = model.Caption, PublishedDate = model.PublishedDate, WriterBooks = new List <WriterBook>() }; foreach (var wid in model.WriterIds) { book.WriterBooks.Add(new WriterBook { WriterId = wid, BookId = id }); } _bookRepository.Edit(book); return(RedirectToAction("Index")); } foreach (var w in _writerRepository.Get()) { model.Writers.Add(new SelectListItem { Value = w.Id.ToString(), Text = $"{w.LastName} {w.FirstName}" }); } return(View(model)); }
private void CopyParams(BookEditModel model) { Title = model.Title; Author = model.Author; Year = model.Year; Count = model.Count; Genre = model.Genre; }
public static void EditBookToDomain(this Book book, BookEditModel model) { book.Title = model.Title; book.PublisherId = model.PublisherId; book.Description = string.IsNullOrWhiteSpace(model.Description) ? null : model.Description; book.PublishingDate = model.PublishingDate; book.Rating = model.Rating; book.CoverUrl = model.CoverUrl; }
public IActionResult Add() { var model = new BookEditModel { Publishers = _uow.PublisherRepository.List().ToList() }; return(View(model)); }
public static ServiceBook ToServiceBook(this BookEditModel model) { return(new ServiceBook() { ID = model.ID, AgeCategory = model.AgeCategory, FirstPublication = model.PublishDate, Name = model.Name }); }
public IHttpActionResult Put(BookEditModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _bookStoreManager.Update(model); return(Ok()); }
public IActionResult Add(BookEditModel model) { if (!ModelState.IsValid) { model.Publishers = _uow.PublisherRepository.List().ToList(); return(View(model)); } _uow.BookRepository.Add(model.AddBookToDomain()); _uow.Commit(); return(RedirectToAction("List")); }
public async Task <ActionResult> Update(int id, [FromBody] BookEditModel model) { Book book = await booksService.Find(id); if (book == null) { return(NotFound()); } book.Update(model); return(Ok(await booksService.Update(book))); }
public void Update(BookEditModel model) { _bookRepository.Update(new DbBook { Id = model.Id, Pages = model.Pages, PublishDate = model.PublishDate, Publisher = model.Publisher, Title = model.Title, ISBN = model.ISBN }); }
public static Book AddBookToDomain(this BookEditModel model) { return(new Book { Id = Guid.NewGuid(), Title = model.Title, PublisherId = model.PublisherId, Description = string.IsNullOrWhiteSpace(model.Description) ? null : model.Description, PublishingDate = model.PublishingDate, Rating = model.Rating, CoverUrl = model.CoverUrl }); }
public async Task <IActionResult> Edit(Guid id, [Bind("Title, Description, Image")] BookEditModel bookEditModel) { var bookToBeEdited = _repository.GetBookById(id); if (bookToBeEdited == null) { return(NotFound()); } if (!ModelState.IsValid) { return(View(bookEditModel)); } bookToBeEdited.Title = bookEditModel.Title; bookToBeEdited.Description = bookEditModel.Description; try { if (bookEditModel.Image != null) { var searchedPath = bookToBeEdited.Folder.Replace("~", ""); var path = _env.WebRootPath + searchedPath; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } using (var fileStream = new FileStream(Path.Combine(path, bookToBeEdited.ImageName), FileMode.Create)) { await bookEditModel.Image.CopyToAsync(fileStream); } } _repository.EditBook(bookToBeEdited); } catch (DbUpdateConcurrencyException) { if (!BookExists(_repository.GetBookById(id).Id)) { return(NotFound()); } throw; } return(RedirectToAction(nameof(Index))); }
public IActionResult Books(string isbn) { BookEditModel model = new BookEditModel() { Books = _lendingInfoDbContext.Books.Include(b => b.Keeper).AsNoTracking().Where(b => b.ISBN == isbn), BookDetails = _lendingInfoDbContext.BooksDetail.AsNoTracking().FirstOrDefault(b => b.ISBN == isbn) }; if (model.BookDetails == null) { TempData["message"] = "未找到目标书籍"; return(RedirectToAction("BookDetails")); } return(View(model)); }
public async Task EditBookAsync(string id, BookEditModel book) { var current = await context.Books.SingleOrDefaultAsync(b => b.Id == id); if (current == null) { throw new BllException { ErrorCode = ErrorCode.BadRequest }; } context.Entry <Book>(current).CurrentValues.SetValues(book); await context.SaveChangesAsync(); }
public IActionResult Create(BookEditModel bookEditModel) { if (ModelState.IsValid) { var newBook = new BookRepo(); newBook.Name = bookEditModel.Name; newBook.Type = bookEditModel.Type; newBook = _bookData.AddBook(newBook); return(RedirectToAction(nameof(Info), new { id = newBook.BookId })); } else { return(View()); } }
public static void UpdateBook(BookEditModel model) { manager.bookService.UpdateBook(model.ToServiceBook()); var fbook = manager.bookService.GetFullBookInfo(model.ID); var book = fbook.BookData; var authors = manager.authorService.GetAllAuthors().ToList(); //Adding authors foreach (var author in model.Authors?.Select(authorID => authors.FirstOrDefault(e => e.ID == authorID)).Where(e => e != null) ?? new List <ServiceAuthor>()) { manager.authorService.AddAuthorBook(author, book); } model.Authors = model.Authors ?? new int[0]; fbook.Authors = fbook.Authors.ToList(); foreach (var author in fbook.Authors.Where(author => model.Authors.All(e => e != author.ID))) { manager.authorService.RemoveAuthorBook(author, book); } var genres = manager.listService.GetAllGenres().ToList(); //Adding genres foreach (var genre in model.Genres?.Select(genreID => genres.FirstOrDefault(e => e.ID == genreID)).Where(genre => genre != null) ?? new List <ServiceGenre>()) { manager.listService.AddBookGenre(book, genre); } model.Genres = model.Genres ?? new int[0]; fbook.Genres = fbook.Genres.ToList(); foreach (var genre in fbook.Genres.Where(genre => model.Genres.All(e => e != genre.ID))) { manager.listService.RemoveBookGenre(book, genre); } var tags = manager.listService.GetAllTags().ToList(); //Adding tags foreach (var tag in model.Tags?.Select(tagID => tags.FirstOrDefault(e => e.ID == tagID)).Where(tag => tag != null) ?? new List <ServiceTag>()) { manager.listService.AddBookTag(book, tag); } model.Tags = model.Tags ?? new int[0]; fbook.Tags = fbook.Tags.ToList(); foreach (var tag in fbook.Tags.Where(tag => model.Tags.All(e => e != tag.ID))) { manager.listService.RemoveBookTag(book, tag); } }
public ActionResult Edit(BookEditModel model) { if (ModelState.IsValid) { var book = db.Books.Where(m => m.Id == model.Id) .FirstOrDefault(); if (book != null) { book.Title = model.Title; book.Description = model.Description; book.Year = model.Year; book.CoverUrl = model.CoverUrl; book.AuthorId = model.AuthorId; book.CategoryId = model.CategoryId; } else { book = new Book { Title = model.Title, Description = model.Description, Year = model.Year, CoverUrl = model.CoverUrl, AuthorId = model.AuthorId, CategoryId = model.CategoryId }; db.Books.Add(book); } db.SaveChanges(); return(RedirectToAction("IndexAuthor", new { id = model.AuthorId })); } model.Authors = db.Authors .Select(m => new SelectListItem { Value = m.Id.ToString(), Text = m.Name, Selected = m.Id == model.AuthorId }) .ToList(); model.Categories = db.Categories .Select(m => new SelectListItem { Value = m.Id.ToString(), Text = m.Title, Selected = m.Id == model.CategoryId }) .ToList(); return(View(model)); }
public ActionResult EditBook(int id) { BookBs bs = new BookBs(); AuthorBs authorBs = new AuthorBs(); BookEditModel model = new BookEditModel(); model.Book = (BookModel)bs.GetById(id); model.Authors = authorBs.GetList().Select(c => (AuthorModel)c).ToList(); LibraryBs libraryBs = new LibraryBs(); model.Libraries = libraryBs.GetList().Select(c => (LibraryModel)c).ToList(); if (model.Book.DepartmentId.HasValue && model.Book.DepartmentId.Value > 0) { DepartmentBs departmentBs = new DepartmentBs(); if (model.SelectedLib == 0) { model.SelectedLib = departmentBs.GetById(model.Book.DepartmentId.Value).LibraryId; } var departments = departmentBs.GetList().Where(c => c.LibraryId == model.SelectedLib).ToList(); if (departments != null) { model.Departments = departments.Select(c => (DepartmentModel)c).ToList(); } } else { DepartmentBs departmentBs = new DepartmentBs(); var lib = model.Libraries.FirstOrDefault(); if (lib != null) { model.SelectedLib = lib.Id; var departments = departmentBs.GetList().Where(c => c.LibraryId == model.SelectedLib).ToList(); if (departments != null) { model.Departments = departments.Select(c => (DepartmentModel)c).ToList(); } } } return(View(model)); }
public ActionResult BookEdit(int id) { var q = db.Tbl_Book.Where(a => a.Book_ID == id).SingleOrDefault(); Rep_Book rep = new Rep_Book(); BookEditModel model = new BookEditModel(); model.DetailsNav = rep.Get_BookDetailsListByBookType(q.Tbl_BookType.BookType_ID); model.ID = q.Book_ID; model.Details = q.Tbl_BookDetails; model.Book = q; return(View(model)); }
public async Task <IActionResult> Edit(BookEditModel collection) { if (ModelState.IsValid) { await m_bookService.Update(collection); return(RedirectToAction(nameof(Index))); } else { ViewBag.PublishersList = await GetPublishersSelectList(); ViewBag.AuthorsList = await GetAuthorsSelectList(); return(View(collection)); } }
public IActionResult Create(BookEditModel model) { if (ModelState.IsValid) { var newBook = new Books(); newBook.BookName = model.BookName; newBook.Type = model.Type; newBook.BookAuthor = model.BookAuthor; newBook = _bookDetails.Add(newBook); return(RedirectToAction(nameof(Info), new { id = newBook.BookId })); } else { return(View()); } }
public IActionResult Detail(string isbn) { BookDetails bookDetails = _lendingInfoDbContext.BooksDetail.AsNoTracking().FirstOrDefault(b => b.ISBN == isbn); if (bookDetails == null) { TempData["message"] = $"找不到 ISBN 为{isbn}的书籍"; return(View("Index")); } BookEditModel model = new BookEditModel() { BookDetails = bookDetails, Books = _lendingInfoDbContext.Books.Include(b => b.Appointments).AsNoTracking().Where(b => b.ISBN == isbn) }; return(View(model)); }
public async Task <IActionResult> Put(BookEditModel book) { var bookEntity = _mapper.Map <Book>(book); if (!_bookValidationService.IsBookExists(bookEntity)) { return(NotFound("The book by id {book.Id} was not found.")); } if (!_bookValidationService.IsBookBelongsToCategory(bookEntity)) { return(BadRequest($"Can not save. Category by id {book.Id} was not found.")); } await _bookService.Update(bookEntity); return(Ok()); }
public IActionResult Edit(BookEditModel model) { if (!ModelState.IsValid) { model.Publishers = _uow.PublisherRepository.List().ToList(); return(View(model)); } var book = _uow.BookRepository.Find(model.Id); if (book == null) { return(NotFound()); } book.EditBookToDomain(model); _uow.Commit(); return(RedirectToAction("List")); }
public BookViewModel SaveBookEditModelToDb(BookEditModel bookEditModel) { Book _bookDbModel; if (bookEditModel.Id != 0) { _bookDbModel = _dataManager.Books.GetBookById(bookEditModel.Id); } else { _bookDbModel = new Book(); } _bookDbModel.Name = bookEditModel.Name; _bookDbModel.Author = _dataManager.Authors.GetAuthorById(bookEditModel.AuthorId); _bookDbModel.Price = bookEditModel.Price; _dataManager.Books.SaveBook(_bookDbModel); return(BookDBToViewModelById(_bookDbModel.Id)); }