public void Run() {
     var docs = getRdfFiles()
         .Where(f => f.Id.NotIn(0, 999999) && f.Title != null)
         // Don't include any that have no epubs (eg sound files)
         .Where(f => f.EpubUrlImages != null || f.EpubUrlNoImages != null);
     using (var db = new EbooksContext()) {
         int count = 0;
         var newBooks = new List<GutBook>();
         var newAuthors = new List<GutAuthor>();
         var newLangNames = new List<LanguageName>();
         var newLangCodes = new List<LanguageCode>();
         foreach (var doc in docs) {
             loadBookData(db, doc, newBooks, newAuthors, newLangNames, newLangCodes);
             if (++count >= 1000) {
                 db.LanguageNames.AddRange(newLangNames);
                 newLangNames.Clear();
                 db.LanguageCodes.AddRange(newLangCodes);
                 newLangCodes.Clear();
                 db.GutAuthors.AddRange(newAuthors);
                 newAuthors.Clear();
                 db.GutBooks.AddRange(newBooks);
                 newBooks.Clear();
                 db.SaveChanges();
                 count = 0;
             }
         }
         db.LanguageNames.AddRange(newLangNames);
         db.LanguageCodes.AddRange(newLangCodes);
         db.GutAuthors.AddRange(newAuthors);
         db.GutBooks.AddRange(newBooks);
         db.SaveChanges();
     }
 }
 public ActionResult Download(int gutBookId, bool images)
 {
     using (var db = new EbooksContext()) {
         var gutBook = db.GutBooks.Single(b => b.GutBookId == gutBookId);
         // Check whether the book has already been downloaded
         var ef = db.EpubFiles.SingleOrDefault(e => e.GutBookId == gutBook.GutBookId && e.GutBookWithImages == images);
         Epub ep;
         if (ef != null) {
             ep = new Epub(ef.Contents);
         } else {
             ep = new Epub(images ? gutBook.GetEpubImages() : gutBook.GetEpubNoImages());
         }
         try {
             var user = db.GetEbooksUser(User);
             var book = Book.Load(db, ep, user.UserId);
             if (book.Publisher == null) book.Publisher = Publisher.Get(db, "Project Gutenberg");
             if (ef == null) {
                 book.EpubFile.GutBookId = gutBook.GutBookId;
                 book.EpubFile.GutBookWithImages = images;
             }
             db.SaveChanges();
         } finally {
             ep.Dispose();
         }
     }
     return RedirectToAction("Index", "Ebooks");
 }
 public ActionResult ShowGutThumbnail(int bookId)
 {
     using (var db = new EbooksContext()) {
         var thumbnail = db.GutBooks.Single(b => b.GutBookId == bookId).GetThumbnailData() ?? Cover.EmptyThumbnail;
         db.SaveChanges();   // May have read and cached the thumbnail data
         return new FileStreamResult(new MemoryStream(thumbnail), "image/jpeg");
     }
 }
 public ActionResult Delete(int bookId)
 {
     using (var db = new EbooksContext()) {
         var user = db.GetEbooksUser(User);
         var book = db.Books.SingleOrDefault(b => b.BookId == bookId && b.UserId == user.UserId);
         if (book != null) {
             db.Books.Remove(book);
             db.SaveChanges();
         }
     }
     return RedirectToAction("Index");
 }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // Create a User in the Ebooks database
                    using (var ebdb = new EbookObjects.Models.EbooksContext()) {
                        ebdb.Users.Add(new EbookObjects.Models.User { Identity = user.Id });
                        ebdb.SaveChanges();
                    }
                    
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult Edit(BookEditedViewModel model)
        {
            using (var db = new EbooksContext()) {
                var book = db.Books.Single(b => b.BookId == model.BookId);
                book.Title = model.Title;
                if (model.Author.StartsWith("~")) {
                    book.Author = Author.Get(db, model.Author.Substring(1));
                } else {
                    book.AuthorId = model.AuthorId != 0 ? model.AuthorId : null;
                }
                if (model.Publisher.StartsWith("~")) {
                    book.Publisher = Publisher.Get(db, model.Publisher.Substring(1));
                } else {
                    book.PublisherId = model.PublisherId != 0 ? model.PublisherId : null;
                }
                book.SetTags(db, model.Tags);
                book.SetIdentifiers(db, model.BookIdents);

                book.Description = model.Description;
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }
 public ActionResult UploadFiles()
 {
     using (var db = new EbooksContext()) {
         var user = db.GetEbooksUser(User);
         foreach (string f in Request.Files) {
             HttpPostedFileBase hpf = Request.Files[f] as HttpPostedFileBase;
             if (hpf.ContentLength == 0) continue;
             using (var m = new MemoryStream(hpf.ContentLength)) {
                 hpf.InputStream.CopyTo(m);
                 var epub = new Epub(m);
                 Book.Load(db, epub, user.UserId);
                 db.SaveChanges();
             }
         }
     }
     return Redirect("~/Ebooks");
 }