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 void ProcessRequest(HttpContext context)
        {
            Guid ebookId;
            int position;
            if (context.Request.QueryString.AllKeys.Contains("ebook")
                && Guid.TryParse(context.Request.QueryString["ebook"], out ebookId)
                && context.Request.QueryString.AllKeys.Contains("position")
                && int.TryParse(context.Request.QueryString["position"], out position))
            {
                using (var db = new EbookManagerDbContext())
                {
                    var catalogRepository = new CatalogRepository(db);
                    var ebookPart = catalogRepository.GetEbookPart(ebookId, position);
                    if (ebookPart != null)
                    {
                        context.Response.Clear();
                        context.Response.ContentType = ebookPart.ContentType;
                        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + ebookPart.FileName);
                        context.Response.BinaryWrite(ebookPart.PartContent);
                        context.Response.End();
                        return;
                    }
                }
            }

            context.Response.StatusCode = 404;
            context.Response.StatusDescription = "Not Found";
            context.Response.End();
        }
        public async Task<ActionResult> Add(AddEbookModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            using (var db = new EbookManagerDbContext())
            {
                var catalogRepository = new CatalogRepository(db);
                var ebook = new Ebook();
                ebook.Id = Guid.NewGuid();
                ebook.Summary = model.Summary;
                ebook.Title = model.Title;

                ebook.Thumbnail = new byte[model.Thumbnail.ContentLength];
                model.Thumbnail.InputStream.Read(ebook.Thumbnail, 0, ebook.Thumbnail.Length);

                try
                {
                    await catalogRepository.AddEbookAsync(ebook);
                    return RedirectToRoute("editEbook", new { ebookId = ebook.Id });
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("InsertError", e);
                    return View(model);
                }
            }
        }
        public async Task<HttpResponseMessage> GetEbooks()
        {
            using (var db = new EbookManagerDbContext())
            {
                var userName = "******";
                var catalogRepository = new CatalogRepository(db);
                var userEbooks = await catalogRepository.LoadUserCatalog(userName);

                return Request.CreateResponse(HttpStatusCode.OK, userEbooks);
            }
        }
        public async Task<HttpResponseMessage> GetMyEbooks()
        {
            using (var db = new EbookManagerDbContext())
            {
                var userName = User.Identity.Name;

                var catalogRepository = new CatalogRepository(db);
                var ebooks = await catalogRepository.LoadUserCatalogWithPartCount(userName);
                return Request.CreateResponse(ebooks.Select(e => new { Id = e.Item1.Id, Summary = e.Item1.Summary, Title = e.Item1.Title, Thumbnail = e.Item1.Thumbnail, PartCount = e.Item2 }));
            }
        }
        public async Task<ActionResult> Index()
        {
            using (var db = new EbookManagerDbContext())
            {
                var catalogRepository = new CatalogRepository(db);
                var ebooks = await catalogRepository.LoadCatalogAsync();

                var model = new AdminCatalogModel();
                model.Ebooks = ebooks.Select(e => EbookViewModel.FromEbook(e)).ToList();

                return View(model);
            }
        }
        public async Task<ActionResult> Index()
        {
            using (var db = new EbookManagerDbContext())
            {
                var catalogRepository = new CatalogRepository(db);
                var ebooks = await catalogRepository.LoadCatalogWithoutPartsAsync();

                var model = new CatalogViewModel();
                model.Ebooks.AddRange(ebooks.Select(e => EbookViewModel.FromEbook(e)));

                return View(model);
            }
        }
 public async Task<HttpResponseMessage> GetEbookPart(Guid ebookId, int index)
 {
     using (var db = new EbookManagerDbContext())
     {
         var catalogRepository = new CatalogRepository(db);
         var part = catalogRepository.GetEbookPart(ebookId, index);
         var response = new HttpResponseMessage(HttpStatusCode.OK);
         response.Content = new ByteArrayContent(part.PartContent);
         response.Content.Headers.ContentLength = part.PartContent.Length;
         response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(part.ContentType);
         return response;
     }
 }
        public async Task<ActionResult> Buy(Guid ebookId)
        {
            using (var db = new EbookManagerDbContext())
            {
                var catalogRepository = new CatalogRepository(db);

                var userId = ClaimsPrincipal.Current.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
                await catalogRepository.BuyEbookAsync(userId, ebookId);

                TempData["Success"] = "Votre achat a été pris en compte !";

                return RedirectToAction("Index", "MySpace");
            }
        }
        public async Task<ActionResult> Index()
        {
            using (var db = new EbookManagerDbContext())
            {
                var userName = User.Identity.Name;

                var catalogRepository = new CatalogRepository(db);
                var ebooks = await catalogRepository.LoadUserCatalog(userName);

                var model = new CatalogViewModel();
                model.Ebooks.AddRange(ebooks.Select(e => EbookViewModel.FromEbook(e)));

                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> ConfirmDelete(Guid ebookId)
        {
            using (var db = new EbookManagerDbContext())
            {
                var catalogRepository = new CatalogRepository(db);
                await catalogRepository.DeleteEbookAsync(ebookId);

                TempData["Success"] = "L'eBook a été supprimé";
                return RedirectToAction("Index");
            }
        }
        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");
            }
        }