public ActionResult Edit(int id, IFormCollection collection, IFormFile file) { try { var dto = new InsertUpdateBookDTO() { Title = collection["Book.Title"], Description = collection["Book.Description"], Pages = Int32.Parse(collection["Book.Pages"]), Price = Double.Parse(collection["Book.Price"]), CategoryId = Int32.Parse(collection["Book.CategoryId"]), AuthorId = Int32.Parse(collection["Book.AuthorId"]) }; if (file != null) { var book = _service.GetById(id); _image.Delete(book.ImageUrl); var path = _image.Upload(file); dto.ImageUrl = path; } _service.Update(dto, id); return(RedirectToAction(nameof(Index))); } catch (Exception e) { Console.WriteLine(e); return(View()); } }
public void Update(InsertUpdateBookDTO dto, int id) { var book = _unitOfWork.Book.Get(id); if (dto.Title != null) { book.Title = dto.Title; } if (dto.Description != null) { book.Description = dto.Description; } if (dto.Price > 0) { book.Price = dto.Price; } if (dto.Pages > 0) { book.Pages = dto.Pages; } if (dto.CategoryId > 0) { book.CategoryId = dto.CategoryId; } if (dto.AuthorId > 0) { book.AuthorId = dto.AuthorId; } book.ModifiedAt = DateTime.Now; if (dto.ImageUrl != null) { book.Image = dto.ImageUrl; } _unitOfWork.Save(); }
public IActionResult Post([FromForm] InsertUpdateBookDTO dto, IFormFile file) { try { var path = _image.Upload(file); dto.ImageUrl = path; _service.Insert(dto); return(Ok("Successfully inserted!")); } catch (Exception e) { Console.WriteLine(e.Message); return(BadRequest(e.Message)); } }
public int Insert(InsertUpdateBookDTO dto) { if (dto.Title == null) { throw new Exception("Title field is required!"); } if (dto.Description == null) { throw new Exception("Description field is required!"); } if (dto.Pages < 1) { throw new Exception("Pages field is required!"); } if (dto.Price < 1) { throw new Exception("Price field is required!"); } if (dto.CategoryId < 1) { throw new Exception("CategoryId field is required!"); } if (dto.AuthorId < 1) { throw new Exception("AuthorId field is required!"); } if (dto.ImageUrl == null) { throw new Exception("Image is required!"); } var book = new Book() { Title = dto.Title, Description = dto.Description, Pages = dto.Pages, Price = dto.Price, CreatedAt = DateTime.Now, CategoryId = dto.CategoryId, AuthorId = dto.AuthorId, Image = dto.ImageUrl }; _unitOfWork.Book.Add(book); _unitOfWork.Save(); return(book.Id); }
public ActionResult Edit(int id, [FromForm] InsertUpdateBookDTO dto, IFormFile file) { try { if (file != null) { var book = _service.GetById(id); _image.Delete(book.ImageUrl); var path = _image.Upload(file); dto.ImageUrl = path; } _service.Update(dto, id); return(Ok("Book successfully updated!")); } catch (Exception e) { Console.WriteLine(e); return(BadRequest(e.Message)); } }
public ActionResult Create(IFormCollection collection, IFormFile file) { try { var path = _image.Upload(file); var dto = new InsertUpdateBookDTO() { Title = collection["Book.Title"], Description = collection["Book.Description"], Pages = Int32.Parse(collection["Book.Pages"]), Price = Double.Parse(collection["Book.Price"]), CategoryId = Int32.Parse(collection["Book.CategoryId"]), AuthorId = Int32.Parse(collection["Book.AuthorId"]), ImageUrl = path }; _service.Insert(dto); return(RedirectToAction(nameof(Index))); } catch (Exception e) { Console.WriteLine(e.Message); return(View()); } }
public void Delete(InsertUpdateBookDTO entity) { throw new System.NotImplementedException(); }