public async Task<ActionResult> EditEBook(Guid ebookId) { using (var db = new EbookManagerDbContext()) { var catalogRepository = new CatalogRepository(db); var ebook = await catalogRepository.GetEbookAsync(ebookId); if (ebook == null) throw new HttpException(404, "not found"); var model = new EditEbookModel(); model.Id = ebook.Id; model.Summary = ebook.Summary; model.Base64Thumbnail = Convert.ToBase64String(ebook.Thumbnail); model.Title = ebook.Title; model.PartsCount = ebook.Parts.Count; foreach (var part in ebook.Parts) { model.ExistingParts.Add(EbookPartViewModel.FromEbookPart(part)); } return View(model); } }
public async Task<ActionResult> Details(Guid ebookId) { using (var db = new EbookManagerDbContext()) { var catalogRepository = new CatalogRepository(db); var ebook = await catalogRepository.GetEbookAsync(ebookId); var model = EbookViewModel.FromEbook(ebook); var userId = ClaimsPrincipal.Current.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; ViewBag.UserOwnsBook = await catalogRepository.UserOwnsBookAsync(userId, ebookId); return View(model); } }
public async Task<ActionResult> DeleteBook(Guid ebookId) { using (var db = new EbookManagerDbContext()) { var catalogRepository = new CatalogRepository(db); var ebook = await catalogRepository.GetEbookAsync(ebookId); if (ebook == null) throw new HttpException(404, "not found"); var model = EbookViewModel.FromEbook(ebook); return View(model); } }
public async Task<ActionResult> EditEBook(EditEbookModel model) { if (!ModelState.IsValid) { return View(model); } using (var db = new EbookManagerDbContext()) { var catalogRepository = new CatalogRepository(db); var ebook = await catalogRepository.GetEbookAsync(model.Id); if (ebook == null) throw new HttpException(404, "not found"); ebook.Title = model.Title; ebook.Summary = model.Summary; if (model.Thumbnail != null) { ebook.Thumbnail = new byte[model.Thumbnail.ContentLength]; model.Thumbnail.InputStream.Read(ebook.Thumbnail, 0, ebook.Thumbnail.Length); } if (model.Parts != null) { foreach (var part in model.Parts) { if (part == null) continue; var ebookPart = new EbookPart(); ebookPart.EbookId = ebook.Id; ebookPart.PartContent = new byte[part.ContentLength]; part.InputStream.Read(ebookPart.PartContent, 0, ebookPart.PartContent.Length); ebookPart.Position = ebook.Parts.Count; ebookPart.ContentType = part.ContentType; ebookPart.FileName = System.IO.Path.GetFileName(part.FileName); ebook.Parts.Add(ebookPart); } } await catalogRepository.UpdateBookAsync(ebook); TempData["Success"] = "L'eBook a été sauvegardé"; return RedirectToAction("Index"); } }