public ActionResult Save(Book book) { if (!ModelState.IsValid) { var viewModel = new BookFormModel(); return(View("BookForm", viewModel)); } if (book.Id == 0) { book.DateAdded = DateTime.Now; _context.Books.Add(book); } else { var bookInDb = _context.Books.Single(b => b.Id == book.Id); bookInDb.Title = book.Title; bookInDb.Author = book.Author; bookInDb.Genre = book.Genre; bookInDb.NumberInStock = book.NumberInStock; bookInDb.ReleaseDate = book.ReleaseDate; } _context.SaveChanges(); return(RedirectToAction("Index", "Books")); }
public DataObjectOperationResult CommitChanges(BookFormModel postedData) { Mapper.CreateMap<BookFormModel, Book>(); var book = new Book(Db); Mapper.Map<BookFormModel, Book>(postedData, book); var results = book.Save(); postedData.Id = book.Id; return results; }
public ActionResult New() { var viewModel = new BookFormModel { Book = new Book() }; return(View("BookForm", viewModel)); }
public async Task <int> AddAsync(BookFormModel bookModel) { var book = this.mapper.Map <Book>(bookModel); await this.dbContext.AddAsync(book); await this.dbContext.SaveChangesAsync(); return(book.Id); }
public BookFormModel GenerateForm() { var vModel = new BookFormModel() { Authors = new List<IAuthor>(), Characters = new List<ICharacter>() }; BuildFormDdLs(vModel); return vModel; }
public void BuildFormDdLs(BookFormModel vModel) { var authorChoices = Author.GetAll(Db).ToList().Except(vModel.Authors); vModel.AuthorAssoiationOptions = ListToSelectList.ConvertToSelectList( authorChoices, "Id", "LastName", true, "Select Author To Add"); var characterChoices = Character.GetAll(Db).ToList().Except(vModel.Characters); vModel.CharacterAssoiationOptions = ListToSelectList.ConvertToSelectList( characterChoices, "Id", "Name", true, "Select Character To Add"); }
public async Task <IActionResult> Create(BookFormModel model) { if (await this.books.UniqueCheckAsync(model.BookTitle, model.AuthorName)) { this.TempData.AddWarningMessage(string.Format(TempDataAlreadyExistsText, ModelName, model.BookTitle)); return(this.RedirectToAction(nameof(this.Create))); } model.BookDescription = this.html.Sanitize(model.BookDescription); string savePath; if (model.ImageUrl != null) { var path = Path.Combine(ImageFolderName, BooksImageFolderName, $"{Guid.NewGuid()}.jpg"); if (!ImageDownloaderExtensions.Download(model.ImageUrl, path)) { this.TempData.AddWarningMessage(string.Format(TempDataWrongUrlText, ModelName)); return(this.RedirectToAction(nameof(this.Create))); } path = path.Replace("\\", "/"); savePath = string.Format(SavePath, path); } else { savePath = DefaultImagePath; } await this.books.CreateAsync( model.AuthorName, model.BookTitle, model.BookDescription, model.CityIssued, model.Press, model.Department, model.PublishDate, DateTime.UtcNow, model.Pages, model.Genre, savePath, model.Language); this.TempData.AddSuccessMessage(string.Format(TempDataCreateCommentText, ModelName, EndingLetterA)); return(this.RedirectToAction(nameof(this.Books))); }
//-------------EDIT------------ public ActionResult Edit(int id) { var book = _context.Books.SingleOrDefault(b => b.Id == id); if (book == null) { return(HttpNotFound()); } var viewModel = new BookFormModel { Book = book }; return(View("BookForm", viewModel)); }
public async Task <ActionResult> Update([FromBody] BookFormModel model) { if (!Roles.IsLibrarian(User.Identity.Name) && !Roles.IsAdmin(User.Identity.Name)) { return(Forbid()); } var entity = Mapper.Map <Book>(model); foreach (BookCopieModel copie in model.BookCopies) { var library = Libraries.GetLibrary(copie.Library.LibraryId); if (library == null) { return(NotFound()); } if (copie.BookId != 0) { Book book = Books.GetBook(copie.BookId); book.Title = entity.Title; book.AuthorName = entity.AuthorName; book.AuthorSurname = entity.AuthorSurname; book.Description = entity.Description; Books.Update(book); } else { Book book = new Book() { AuthorName = entity.AuthorName, AuthorSurname = entity.AuthorSurname, Description = entity.Description, Library = library, Title = entity.Title }; Books.Create(book); } } var result = Mapper.Map <BookFormModel>(entity); var copies = Books.GetBookCopies(entity.AuthorFullName, entity.Title); result.BookCopies = Mapper.Map <IEnumerable <BookCopieModel> >(copies) .ForEach(x => x.Library = Mapper.Map <LibraryListItemModel>(copies.First(c => c.BookId == x.BookId).Library)).ToList(); return(CreatedAtAction(nameof(Fetch), new { authorFullName = entity.AuthorFullName, bookTitle = entity.Title }, result)); }
public ActionResult AddUpatde(BookFormModel postedData) { bool isNew = postedData.Id == null; var results = BWorker.CommitChanges(postedData); if (results.Success) { if(isNew) { return RedirectToAction("Edit", new {bId = postedData.Id}); } return View("InputSuccess", results); } postedData.PopupTitle = "Edit Book - ERROR"; results.PassErrorsToMvcModelState(ModelState); return View("Form", postedData); }
public IActionResult ParameterTampering(BookFormModel model) { if (!this.ModelState.IsValid) { return(this.View(model)); } var book = new Book { Title = model.Title, Description = model.Description }; this.dbContext.Add(book); this.dbContext.SaveChanges(); return(this.RedirectToAction(nameof(ParameterTampering))); }
public async Task <IActionResult> Create([FromBody] BookFormModel bookForm) { var book = new Book { Id = bookForm.Id, Title = bookForm.Title, Description = bookForm.Description, DifficultyId = bookForm.Difficulty, Categories = bookForm.Categories .Select(guid => _context.Categories.Find(guid)) .ToList() }; await _context.AddAsync(book); await _context.SaveChangesAsync(); return(Ok(BookViewModel.Create(book))); }
public async Task <IActionResult> Edit(int id, BookFormModel model) { string savePath = null; if (model.ImageUrl != null) { var path = Path.Combine(ImageFolderName, BooksImageFolderName, $"{Guid.NewGuid()}.jpg"); if (!ImageDownloaderExtensions.Download(model.ImageUrl, path)) { this.TempData.AddWarningMessage(string.Format(TempDataWrongUrlText, ModelName)); return(this.RedirectToAction(nameof(this.Create))); } path = path.Replace("\\", "/"); savePath = $"/{path}"; } model.BookDescription = this.html.Sanitize(model.BookDescription); await this.books.EditAsync( id, model.AuthorName, model.BookTitle, model.BookDescription, model.CityIssued, model.Press, model.Department, model.PublishDate, model.Pages, model.Genre, savePath, model.Language); this.TempData.AddSuccessMessage(string.Format(TempDataEditCommentText, ModelName, EndingLetterA)); return(this.RedirectToAction(nameof(this.Books))); }
public async Task <bool> EditAsync(BookFormModel bookModel) { var book = await this.dbContext.Books.FindAsync(bookModel.Id); if (book == null) { return(false); } book.Title = bookModel.Title; book.Author = bookModel.Author; book.Publisher = bookModel.Publisher; book.Descritpion = bookModel.Descritpion; if (bookModel.PictureUrl != null) { book.PictureUrl = bookModel.PictureUrl; } await this.dbContext.SaveChangesAsync(); return(true); }