public async Task <IActionResult> CreateBook(AssetCreateBookViewModel model) { if (ModelState.IsValid) { string uniqueFileName = ProcessUploadedAssetFile(model); var book = _mapper.Map <Book>(model); book.Status = _context.Statuses.FirstOrDefault(x => x.Name == "Available"); book.ImageUrl = "/images/" + uniqueFileName; book.Location = _branch.GetBranchByName(model.LibraryBranchName); //Prevent exceptions while searching when the author of the book is unknown if (book.Author == null) { book.Author = "-"; } await _assetsService.AddAsync(book); return(RedirectToAction("Create", "Catalog")); } return(View(model)); }
public IActionResult CreateBook(AssetCreateBookViewModel model) { if (ModelState.IsValid) { string uniqueFileName = ProcessUploadedBookFile(model); var book = new Book { Title = model.Title, Author = model.Author, ISBN = model.ISBN, Year = model.Year, Status = _context.Statuses.FirstOrDefault(x => x.Name == "Available"), Cost = model.Cost, ImageUrl = "/images/" + uniqueFileName, NumberOfCopies = model.NumberOfCopies, Location = _branch.GetBranchByName(model.LibraryBranchName) }; //Prevent exceptions while searching when the author of the book is unknown if (book.Author == null) { book.Author = "-"; } _assetsService.Add(book); return(RedirectToAction("Create", "Catalog")); } return(View(model)); }
private string ProcessUploadedBookFile(AssetCreateBookViewModel model) { string uniqueFileName = null; if (model.Photo != null) { string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.Photo.CopyTo(fileStream); } } return(uniqueFileName); }