public ActionResult Update(HttpPostedFileBase file, HttpPostedFileBase image, int id, string title, string genre, int rating, string language, string description = "", string author_name = "", string country = "", string publisher_name = "", float price = 0) { if (file == null || image == null || author_name == "" || publisher_name == "" || genre == "" || language == "" || title == "") { return(RedirectToAction("Update", new { id = id })); } if (!file.FileName.Contains(".docx") && !file.FileName.Contains(".doc") && !file.FileName.Contains(".pdf") && !file.FileName.Contains(".mobi") && !file.FileName.Contains(".rtf")) { ViewBag.Alert = "Wrong file format (.pdf, .doc, .docx, .rtf, .mobi)"; return(RedirectToAction("Update", new { id = id })); } BookRepository br = new BookRepository(); Book model = new Book() { Id = id, Title = title, Rating = rating, Genre = genre, Price = price, Description = description, Language = language, Author_Name_fk = author_name, Publisher_Name_fk = publisher_name }; if (file != null) { string uniqueName = Guid.NewGuid().ToString() + file.FileName; string fileName = HttpContext.Server.MapPath("~/Static/Files/") + uniqueName; file.SaveAs(fileName); model.fileUrl = uniqueName; } string converted_path = ""; string imgName = ""; if (image != null) { DrawRepository draw = new DrawRepository(); string uniqueName = Guid.NewGuid().ToString() + image.FileName; imgName = HttpContext.Server.MapPath("~/Static/Images/") + uniqueName; image.SaveAs(imgName); uniqueName = uniqueName.Replace(".jpg", ".png"); System.Drawing.Image converted = System.Drawing.Image.FromFile(imgName); converted_path = imgName; converted_path = converted_path.Replace(".jpg", ".png"); converted.Save(converted_path, System.Drawing.Imaging.ImageFormat.Png); draw.Resize(converted_path, 1400, 2046); model.ImgUrl = uniqueName.Replace(".png", "_" + 1400 + "x" + 2046 + ".png"); } br.UpdateBook(model, model.Id); return(RedirectToAction("Book", new { id = model.Id })); }
public ActionResult Edit(HttpPostedFileBase image, Author author, string country) { author.Country = country; AuthorRepository ar = new AuthorRepository(); if (!User.Identity.IsAuthenticated || author == null || country == null) { return(RedirectToAction("Info", new { author = author })); } string converted_path = ""; string imgName = ""; if (image != null) { DrawRepository draw = new DrawRepository(); string uniqueName = Guid.NewGuid().ToString() + image.FileName; imgName = HttpContext.Server.MapPath("~/Static/Images/Authors/") + uniqueName; image.SaveAs(imgName); uniqueName = uniqueName.Replace(".jpg", ".png"); System.Drawing.Image converted = System.Drawing.Image.FromFile(imgName); converted_path = imgName; converted_path = converted_path.Replace(".jpg", ".png"); converted.Save(converted_path, System.Drawing.Imaging.ImageFormat.Png); draw.Resize(converted_path, 1400, 2046); author.ImgUrl = uniqueName.Replace(".png", "_" + 1400 + "x" + 2046 + ".png"); } ar.UpdateAuthor(author); return(RedirectToAction("Info", new { author = author.Author_Name })); }
public object Add(HttpPostedFileBase file, HttpPostedFileBase image, string title, string genre, string language, int rating, string description, string author_name, string country, string publisher_name, float price = 0) { if (!ModelState.IsValid || file == null) { ViewBag.Alert = "Not valid"; return(RedirectToAction("Add")); } if (!file.FileName.Contains(".docx") && !file.FileName.Contains(".doc") && !file.FileName.Contains(".pdf") && !file.FileName.Contains(".mobi") && !file.FileName.Contains(".rtf")) { ViewBag.Alert = "Wrong file format (.pdf, .doc, .docx, .rtf, .mobi)"; return(RedirectToAction("Add")); } description = description.Trim(); Author author = new Author { Author_Name = author_name, Country = country }; Publisher publisher = new Publisher { Publisher_Name = publisher_name }; Book book = new Book { Title = title, Rating = rating, Genre = genre, Price = price, Description = description, Language = language, Author = author, Author_Name_fk = author_name, Publisher_Name_fk = publisher_name, Publisher = publisher }; if (file != null) { string uniqueName = Guid.NewGuid().ToString() + file.FileName; string fileName = HttpContext.Server.MapPath("~/Static/Files/") + uniqueName; file.SaveAs(fileName); book.fileUrl = uniqueName; } string converted_path = ""; string imgName = ""; if (image != null) { DrawRepository draw = new DrawRepository(); string uniqueName = Guid.NewGuid().ToString() + image.FileName; imgName = HttpContext.Server.MapPath("~/Static/Images/") + uniqueName; image.SaveAs(imgName); uniqueName = uniqueName.Replace(".jpg", ".png"); System.Drawing.Image converted = System.Drawing.Image.FromFile(imgName); converted_path = imgName; converted_path = converted_path.Replace(".jpg", ".png"); converted.Save(converted_path, System.Drawing.Imaging.ImageFormat.Png); draw.Resize(converted_path, 1400, 2046); book.ImgUrl = uniqueName.Replace(".png", "_" + 1400 + "x" + 2046 + ".png"); } BookRepository bookRepository = new BookRepository(); try { string result = bookRepository.AddBook(book); if (result == "Added") { return(RedirectToAction("Products", "Home")); } else { return("Book exists already"); } } catch (Exception) { return(RedirectToAction("Add")); } }