public ActionResult Create(UploadBookViewModel model) { try { // TODO: Add insert logic here if (ModelState.IsValid) { model.OwnerId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); var msg = bookService.UploadBook(model); if (msg == ServiceResultMsg.BOOK_UPLOAD_SUCCESS) { HomePageTransferData m = new HomePageTransferData() { BookServiceMsg = msg }; return(RedirectToAction("Index", "Home")); } } return(RedirectToAction("Index")); } catch (Exception e) { return(View()); } }
public async Task <IActionResult> Upload(UploadBookViewModel m) { var f = m.BookFile; var fName = m.BookName; var user = await _userManager.GetUserAsync(User); var folder = Path.Combine(_env.WebRootPath, "bookRepo", user.NormalizedUserName); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } var filePath = Path.Combine(folder, f.FileName); if (f.Length > 0) { using (var fileStream = new FileStream(filePath, FileMode.Create)) { await f.CopyToAsync(fileStream); } } EpubBook epubBook = EpubReader.ReadBook(filePath); byte[] coverImageContent = epubBook.CoverImage; var coverPath = Path.Combine(_env.WebRootPath, "bookCovers", Guid.NewGuid().ToString() + ".jpg"); if (coverImageContent != null) { using (MemoryStream coverImageStream = new MemoryStream(coverImageContent)) { Image coverImage = Image.FromStream(coverImageStream); coverImage = (Image)(new Bitmap(coverImage, new Size(140, 180))); coverImage.Save(coverPath, ImageFormat.Jpeg); } } else { coverPath = null; } Book newBook = new Book { Id = Guid.NewGuid(), BookName = fName, BookCoverImagePath = coverPath, Path = filePath, UploadedDateTime = DateTime.Now, UploadedByUser = user, }; _context.BookDbSet.Add(newBook); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public ActionResult Upload() { UploadBookViewModel model = new UploadBookViewModel() { Genres = bookService.GetAllGenres() }; return(View(model)); }
public ActionResult UploadBook() { var allCategories = this.categories.GetAll().To <BookCategoryViewModel>().ToList(); var viewModel = new UploadBookViewModel() { Categories = allCategories, }; return(this.View(viewModel)); }
private void ValidateGenreAndISBN(UploadBookViewModel ubvm) { if (ubvm.Genre < 1 && ubvm.Genre > this.db.Genres.Count()) { throw new ArgumentOutOfRangeException("Invalid genreId"); } string isbnRegex = @"((?:[\dX]{13})|(?:[\d\-X]{17})|(?:[\dX]{10})|(?:[\d\-X]{13}))"; Regex rgx = new Regex(isbnRegex); if (!rgx.IsMatch(ubvm.ISBN) && !rgx.IsMatch(ubvm.ISBN)) { throw new Exception("Invalid ISBN"); } }
private void FillBookDbModels(UploadBookViewModel ubvm, Book book) { ValidateGenreAndISBN(ubvm); book.GenreId = ubvm.Genre; if (CountDigits(ubvm.ISBN) == 10) { book.ISBN10 = ubvm.ISBN; book.ISBN13 = ""; } if (CountDigits(ubvm.ISBN) == 13) { book.ISBN10 = ""; book.ISBN13 = ubvm.ISBN; } book.Title = ubvm.Title; book.Author = ubvm.Author; }
public ActionResult GetBookInformationByISBN(test model) { var cleanISBN = model.isbn.Replace("-", string.Empty); var output = BookSearch.SearchISBN(cleanISBN); var result = output.Result.VolumeInfo; UploadBookViewModel ubvm = new UploadBookViewModel() { Title = result.Title, Author = result.Authors.FirstOrDefault(), Genre = 1 }; ubvm.ISBN = cleanISBN; return(Json(ubvm)); // return View("Upload", ubvm); }
public ActionResult Upload(UploadBookViewModel uploadBook, HttpPostedFileBase file) { DepartmentManager departmentManager = new DepartmentManager(); ConditionManager conditionManager = new ConditionManager(); ViewBag.Conditions = conditionManager.GetConditions(); ViewBag.Departments = departmentManager.GetAllDepartments(); if (ModelState.IsValid) { BookManager bookManager = new BookManager(); bool saved = bookManager.SaveBook(uploadBook, file, User.Identity.GetUserId()); if (saved) { return(RedirectToAction("Uploaded", "Dashboard")); } else { return(View(uploadBook)); } } return(View(uploadBook)); }
private void FillUserBookDbModels(UploadBookViewModel ubvm, UsersBook userBook, int bookId) { userBook.BookId = bookId; userBook.OwnerId = ubvm.OwnerId; userBook.IsTaken = false; }
public ServiceResultMsg UploadBook(UploadBookViewModel ubvm) { if (ubvm.ISBN == null) { ubvm.ISBN = ""; } ubvm.ISBN = ubvm.ISBN.Replace("-", String.Empty); if (!db.Books.Any(x => (x.ISBN10 == ubvm.ISBN) || (x.ISBN13 == ubvm.ISBN))) { Book book = new Book(); this.FillBookDbModels(ubvm, book); using (db) { db.Books.Add(book); try { db.SaveChanges(); } catch (Exception e) { return(ServiceResultMsg.BOOK_UPLOAD_FAIL); } UsersBook userBook = new UsersBook(); int id = db.Books.First(x => (x.ISBN10 == ubvm.ISBN) || (x.ISBN13 == ubvm.ISBN)).Id; this.FillUserBookDbModels(ubvm, userBook, id); db.UsersBook.Add(userBook); try { db.SaveChanges(); return(ServiceResultMsg.BOOK_UPLOAD_SUCCESS); } catch (Exception e) { return(ServiceResultMsg.BOOK_UPLOAD_FAIL); } } } else { using (db) { UsersBook userBook = new UsersBook(); int id = db.Books.First(x => (x.ISBN10 == ubvm.ISBN) || (x.ISBN13 == ubvm.ISBN)).Id; this.FillUserBookDbModels(ubvm, userBook, id); db.UsersBook.Add(userBook); try { db.SaveChanges(); return(ServiceResultMsg.BOOK_UPLOAD_SUCCESS); } catch (Exception e) { return(ServiceResultMsg.BOOK_UPLOAD_FAIL); } } } }
public bool SaveBook(UploadBookViewModel uploadBook, HttpPostedFileBase file, string userId) { List <Department> depts = new List <Department>(); depts.Add(db.Departments.Single(x => x.Id == uploadBook.Department)); Category category = db.Categories.Find(uploadBook.Category); Condition condition = db.Conditions.Find(uploadBook.Condition); Photo p = new Photo(); if (file != null) { string pic = file.FileName; string path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/Content/img/books"), pic); file.SaveAs(path); p.Path = @"/Content/img/books/" + pic; } try { Book latestBook = new Book() { UserId = userId, UploadDateTime = DateTime.Now, Title = uploadBook.Title, Price = uploadBook.Price, Edition = uploadBook.Edition, Departments = depts, Category = category, CategoryId = category.Id, Approved = false, Author = uploadBook.Author, ConditionId = uploadBook.Condition, Condition = condition, AdditionalInfo = uploadBook.AdditionalInfo, }; db.Books.Add(latestBook); db.SaveChanges(); p.UserId = userId; p.Book = latestBook; p.UploadDateTime = DateTime.Now; db.Photos.Add(p); if (db.SaveChanges() > 0) { return(true); } else { return(false); } } catch (Exception e) { return(false); } }