public async Task <IActionResult> Update(UpdateBookViewModel model) { if (!ModelState.IsValid) { model.Authors = await _authorService.GetAllInDictionaryAsync(); model.Categories = await _categoryService.GetAllInDictionaryAsync(); return(View()); } string imagePath = model.PrevImagePath; if (model.Image != null) { _fileService.DeleteFile(imagePath); imagePath = await _fileService.AddFileAsync(model.Image); } string pdfPath = model.PrevPdfPath; if (model.PdfFile != null) { _fileService.DeleteFile(pdfPath); pdfPath = await _fileService.AddFileAsync(model.PdfFile); } await _bookService.UpdateAsync(model, imagePath, pdfPath); return(RedirectToAction("GetBooks")); }
public ActionResult Edit(int id) { var book = UnitOfWork.BookRepository.Find(x => x.Id == id).Include(y => y.User).Include(z => z.Category).FirstOrDefault(); if (book == null) { return(NotFound()); } if (book.User.Id != GetUserId()) { return(Forbid()); } var viewModel = new UpdateBookViewModel() { BookId = book.Id, Name = book.Name, Description = book.Description, Draft = book.Draft, Author = book.Author, ReleaseYear = book.ReleaseYear, CategoryId = book.Category.Id, UserId = book.User.Id, CategoriesList = UnitOfWork.CategoryRepository.GetAll().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList() }; return(View(viewModel)); }
public Book UpdateBook(UpdateBookViewModel book) { var oldBook = _books.FindAsync(b => b.Id == book.Id).Result.FirstOrDefault(); var new_Book = _mapper.Map <UpdateBookViewModel, Book>(book, oldBook); _books.ReplaceOneAsync(b => b.Id == oldBook.Id, new_Book); return(new_Book); }
internal static Book ToModel(this UpdateBookViewModel viewModel, string id) { return(new Book { Id = id, Name = viewModel.Name, Description = viewModel.Description, ISBN = viewModel.ISBN }); }
public IHttpActionResult UpdateBook(int id, [FromUri] UpdateBookViewModel model) { var getBookId = _context.Books.Find(id); var getAuthorId = _context.Authors.Find(model.AuthorId); if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } if (getBookId == null) { return(BadRequest("There is no such a book id.")); } if (model.Price < 0) { return(BadRequest("The price cannot be negative.")); } if (model.Copies < 0) { return(BadRequest("The copies cannot be negative.")); } if (!Enum.IsDefined(typeof(Edition), model.Edition)) { return(BadRequest("There is no such an edition.")); } if (!Enum.IsDefined(typeof(AgeRestriction), model.AgeRestriction)) { return(BadRequest("There is no such an age restriction.")); } if (getAuthorId == null) { return(BadRequest("There is no such an author id.")); } var updateBook = _context.Books.First(b => b.Id == id); updateBook.Title = model.Title; updateBook.Description = model.Description; updateBook.Price = model.Price; updateBook.Copies = model.Copies; updateBook.Edition = model.Edition; updateBook.AgeRestriction = model.AgeRestriction; updateBook.ReleaseDate = model.ReleaseDate; updateBook.Author.Id = model.AuthorId; _context.SaveChanges(); return(Ok("The book is update it.")); }
public async Task Update(UpdateBookViewModel item) { var updatedItem = Mapper.Map <UpdateBookViewModel, Book>(item); await _bookRepository.Update(updatedItem); var authors = Mapper.Map <ICollection <AuthorUpdateBookViewModelItem>, List <Author> >(item.Authors); await _authorInBookRepository.UpdateAuthorsByBookId(item.Id, authors); var publicHouses = Mapper.Map <ICollection <PublicHouseUpdateBookViewModelItem>, List <PublicHouse> >(item.PublicHouses); await _publicHouseInBookRepository.UpdatePublicHousesByBookId(item.Id, publicHouses); }
public async Task <IActionResult> UpdateBook(UpdateBookViewModel bookViewModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var command = _mapper.Map <UpdateBookViewModel, UpdateBookCommand>(bookViewModel); var result = await _mediator.Send(command); return(Ok(result)); }
public IActionResult UpdateBook(UpdateBookViewModel book) { BookMaster bookToUpdate = _bookMasterRepository.GetBook(book.BookMasterId); int updateSuccessFlag = _bookMasterRepository.DeleteBook(bookToUpdate); if (updateSuccessFlag > 0) { return(RedirectToAction("Index", "Book")); } ModelState.AddModelError("", "Operation not successfull"); return(RedirectToAction("Update", "Book")); }
public async Task <IActionResult> UpdateBook(int id, UpdateBookViewModel updateBook, IFormFile image) { var book = _dbContext.BooksList.Find(updateBook.Id); if (book == null) { ModelState.AddModelError(string.Empty, "Không có sách trong kho"); } else if (book.Code == updateBook.Code) { string uniqueFileName = null; if (updateBook.Image != null) { string uploadsFolder = Path.Combine(_webHostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + updateBook.Image.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); updateBook.Image.CopyTo(new FileStream(filePath, FileMode.Create)); } book.NameOfBook = updateBook.NameOfBook; book.Author = updateBook.Author; book.YearOfPrint = updateBook.YearOfPrint; book.CategoryId = updateBook.CategoryId; book.Quantity = updateBook.Quantity; book.ImageName = uniqueFileName; } else { book.Code = updateBook.Code; if (_dbContext.BooksList.Any(x => x.Code == updateBook.Code)) { ModelState.AddModelError(string.Empty, "Mã cuốn sách đã tồn tại trong kho"); } } if (!ModelState.IsValid) { ViewBag.Categories = _dbContext.CategoriesList.Select(x => new SelectListItem { Text = x.CategoryName, Value = $"{x.Id}", Selected = book.CategoryId == x.Id }).ToList(); return(View(updateBook)); } await _dbContext.SaveChangesAsync(); return(RedirectToAction("Books")); }
/// <summary> /// UpdateAsync /// </summary> /// <param name="model"></param> /// <param name="imagePath"></param> /// <param name="pdfPath"></param> /// <returns></returns> public async Task UpdateAsync(UpdateBookViewModel model, string imagePath, string pdfPath) { var book = await _context.Books.FindAsync(model.Id); if (book == null) { return; } book.Available = model.Available; book.CategoryId = model.CategoryId; book.Description = model.Description; book.Language = model.Language; book.Percent = model.Percent; book.Price = model.Price; book.Title = model.Title; book.AvailableCount = model.AvailableCount; book.HasPdf = pdfPath != null; book.PdfPath = pdfPath; book.ISBN = model.ISBN; book.ImagePath = imagePath; book.PagesNumber = model.PagesNumber; book.PublishYear = model.PublishYear; var authorsId = await _context.BookAuthors.Where(x => x.BookId == book.Id).Select(x => x.AuthorId).ToListAsync(); var prevAuthorsId = new List <int>(); var newBookAuthors = new List <BookAuthor>(); foreach (var id in authorsId) { if (!model.AuthorsId.Contains(id)) { prevAuthorsId.Add(id); } } foreach (var authorId in model.AuthorsId) { if (!authorsId.Contains(authorId)) { newBookAuthors.Add(new BookAuthor { BookId = book.Id, AuthorId = authorId }); } } var prevBookAuthors = await _context.BookAuthors.Where(x => x.BookId == book.Id && prevAuthorsId.Contains(x.AuthorId)).ToListAsync(); _context.BookAuthors.RemoveRange(prevBookAuthors); await _context.BookAuthors.AddRangeAsync(newBookAuthors); await _context.SaveChangesAsync(); }
public IActionResult Update() { var updateBookView = new UpdateBookViewModel { InfoFetched = false, Title = "", Author = "", BookMasterId = 0, ShortDescription = "", LongDescription = "" }; return(View(updateBookView)); }
public async Task <IActionResult> Update([FromBody] UpdateBookViewModel itemVM) { if (itemVM == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await _service.Update(itemVM); return(Ok(itemVM)); }
public async Task <ActionResult <BookViewModel> > Update(string id, UpdateBookViewModel viewModel) { try { var book = await _bookRepository.UpdateBookAsync(viewModel.ToModel(id)); return(book.ToViewModel()); } catch (BookNotFoundException) { return(NotFound()); } catch (InvalidBookException) { return(BadRequest()); } }
public IActionResult Update([FromBody] UpdateBookViewModel book) { try { _bookService.Update(book); return(Ok(book)); } catch (BusinessLogicException exception) { return(BadRequest(exception.Message)); } catch (Exception exception) { _logger.LogInformation(exception.Message); return(StatusCode((int)HttpStatusCode.InternalServerError)); } }
public async Task <IActionResult> Edit(UpdateBookViewModel bookViewModel) { if (ModelState.IsValid) { var model = _mapper.Map <UpdateBookViewModel, UpdateBookModel>(bookViewModel); var result = await _booksClient.UpdateBook(model); if (result.ResponseMessage.StatusCode != HttpStatusCode.OK) { ModelState.AddModelError(string.Empty, result.ResponseMessage.Content.ToString()); } else { return(RedirectToAction("Search")); } } return(View(bookViewModel)); }
public IActionResult Update(UpdateBookViewModel book) { BookMaster bookToUpdate = _bookMasterRepository.GetBook(book.BookMasterId); if (bookToUpdate != null) { var updateBookViewModel = new UpdateBookViewModel { InfoFetched = true, Title = bookToUpdate.Title, Author = bookToUpdate.Author, BookMasterId = bookToUpdate.BookMasterId, ShortDescription = bookToUpdate.ShortDescription, LongDescription = bookToUpdate.LongDescription }; ModelState.AddModelError("", "Sorry!Feature not implemented yet."); return(View(updateBookViewModel)); } ModelState.AddModelError("", "Operation unsuccessful. Book id is not correct."); return(View(book)); }
public ActionResult Edit(UpdateBookViewModel viewModel) { try { var book = UnitOfWork.BookRepository.Find(x => x.Id == viewModel.BookId).Include(y => y.User).Include(v => v.Category).FirstOrDefault(); if (book == null) { return(NotFound()); } if (book.User.Id != GetUserId()) { return(Forbid()); } var category = UnitOfWork.CategoryRepository.Get(x => x.Id == viewModel.CategoryId); if (!ModelState.IsValid) { return(View(viewModel)); } book.Name = viewModel.Name; book.Description = viewModel.Description; book.Author = viewModel.Author; book.ReleaseYear = viewModel.ReleaseYear; book.Draft = viewModel.Draft; book.Category = category; UnitOfWork.BookRepository.Update(book); if (UnitOfWork.Complete() > 0) { return(RedirectToAction(nameof(Details), new { id = book.Id })); } return(View(viewModel)); } catch { return(View(viewModel)); } }
public void Update(UpdateBookViewModel bookViewModel) { if (_bookRepository.Get(bookViewModel.Id) == null) { throw new BusinessLogicException("Book not found"); } bookViewModel.AuthorId = bookViewModel.Author.Id; var book = new Book() { Id = bookViewModel.Id, Name = bookViewModel.Name, AuthorId = bookViewModel.Author.Id, YearOfPublication = bookViewModel.YearOfPublication }; bookViewModel.AuthorId = bookViewModel.Author.Id; _bookRepository.Update(book); UpdatePublicationHouses(bookViewModel.Id, bookViewModel.PublicationHouses.Select(x => x.Id).ToList()); }
public void Update(UpdateBookViewModel model) { if (!string.IsNullOrEmpty(model.Town)) { var dbBook = bookRepo.GetClassicById(model.Id); dbBook.NumberOfPages = model.NumberOfPages; dbBook.Title = model.Title; dbBook.Author = model.Author; dbBook.Price = model.Price; dbBook.Town = model.Town; dbBook.Amount = model.Amount; bookRepo.UpdateClassicBook(dbBook); } else if (!string.IsNullOrEmpty(model.Planet)) { var dbBook = bookRepo.GetFantasyById(model.Id); dbBook.NumberOfPages = model.NumberOfPages; dbBook.Title = model.Title; dbBook.Author = model.Author; dbBook.Price = model.Price; dbBook.Planet = model.Planet; dbBook.Amount = model.Amount; bookRepo.UpdateFantasyBook(dbBook); } else { var dbBook = bookRepo.GetHistoryById(model.Id); dbBook.NumberOfPages = model.NumberOfPages; dbBook.Title = model.Title; dbBook.Author = model.Author; dbBook.Price = model.Price; dbBook.Year = model.Year; dbBook.Amount = model.Amount; bookRepo.UpdateHistoryBook(dbBook); } }
public void UpdateBook([FromBody] UpdateBookViewModel book) { _bookService.UpdateBook(book); }
public void UpdateBook(UpdateBookViewModel updateBookViewModel) { Book book = Mapper.Map <UpdateBookViewModel, Book>(updateBookViewModel); _bookRepository.Update(book); }
public IActionResult UpdateBook(UpdateBookViewModel book) { return(Ok(SuccessHelper.Wrap(_bookServices.UpdateBook(book)))); }
public BookResponse UpdateBook(UpdateBookViewModel newBook) { return(_mapper.Map <Book, BookResponse>(_bookRepository.UpdateBook(newBook))); }
private BookViewModel FillingBookFromRequest(bool addition = false) { BookViewModel book; if (addition) { book = new AddBookViewModel(); } else { book = new UpdateBookViewModel(); } if (!string.IsNullOrEmpty(Request.ContentType) && Request.ContentType.IndexOf("multipart/", StringComparison.OrdinalIgnoreCase) >= 0) { StringValues idValue, nameValue, authorValue, isbnValue, priceValue, quantityValue, contentValue, languageidValue; if (Request.Form.TryGetValue("id", out idValue)) { var id = idValue.FirstOrDefault(); int result; if (int.TryParse(id, out result) && !addition) { (book as UpdateBookViewModel).Id = result; } } if (Request.Form.TryGetValue("name", out nameValue)) { book.Name = nameValue.FirstOrDefault(); } if (Request.Form.TryGetValue("author", out authorValue)) { book.Author = authorValue.FirstOrDefault(); } if (Request.Form.TryGetValue("isbn", out isbnValue)) { book.Isbn = isbnValue.FirstOrDefault(); } if (Request.Form.TryGetValue("price", out priceValue)) { var price = priceValue.FirstOrDefault(); decimal result; if (decimal.TryParse(price, out result)) { book.Price = result; } } if (Request.Form.TryGetValue("quantity", out quantityValue)) { var quantity = quantityValue.FirstOrDefault(); int result; if (int.TryParse(quantity, out result)) { book.Quantity = result; } } if (Request.Form.TryGetValue("content", out contentValue)) { book.Content = contentValue.FirstOrDefault(); } if (Request.Form.TryGetValue("languageid", out languageidValue)) { var languageid = languageidValue.FirstOrDefault(); int result; if (int.TryParse(languageid, out result)) { book.LanguageId = result; } } var cover = new BookCoverViewModel(); var count = Request.Form.Files.Count; if (count > 0) { var file = Request.Form.Files[0]; string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).Name.Trim().ToString(); string fileType = file.ContentType; cover.NameOfFile = file.Name; cover.LengthOfFile = (int)file.Length; cover.TypeOfFile = fileType; byte[] fileBytes = new byte[56000]; using (var stream = new MemoryStream(fileBytes)) { file.CopyTo(stream); cover.BodyOfFile = stream.ToArray(); } } book.imageOfCover = cover; } return(book); }
public IActionResult Update(UpdateBookViewModel model) { bookService.Update(model); return(Ok()); }