public static EbookPartViewModel FromEbookPart(EbookPart part)
 {
     return(new EbookPartViewModel()
     {
         ContentType = part.ContentType,
         EbookId = part.EbookId,
         Position = part.Position,
         FileName = part.FileName
     });
 }
        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"));
            }
        }