public void Correct() { var corrections = BookCommonData.Corrections; OnNotify("Starting correction..."); foreach (var book in db.Books) { book.Title = book.Title.Replace("’", "'").Replace("–", "-").Replace("&", "&").Replace("&", "&"); if (!string.IsNullOrEmpty(book.Category)) { var _category = book.Category; foreach (var correction in corrections) { if (correction.Key.Trim().StartsWith("*")) { continue; } var key = correction.Key.Trim().Replace("*", ""); var value = correction.Value.Trim(); _category = _category.Replace(key, value); } if (_category.Contains(";")) { _category = _category.Split(';')[0]; } book.SetCategory(_category); //no approving book.Authors = book.Authors.Trim(); } book.Summary = book.Summary?.Replace("…", "..."); } db.SaveChanges(); LastAction = BookActions.Correct; OnNotify("Correction finished."); }
public void ClearDbFile() { db.SaveChanges(); db.Dispose(); db = null; File.Delete(@"..\..\books.db"); LastAction = BookActions.Clear; }
public ActionResult Create([Bind(Include = "GenreId,GenreName")] Genre genre) { if (ModelState.IsValid) { int result = BookActions.GenreSave(genre.GenreName); return(RedirectToAction("Index")); } return(View(genre)); }
public BookWindow(Book model, MainWindow main, BookActions action = BookActions.Add) { InitializeComponent(); this.Owner = main; this.model = model; DataContext = this.model; db = main.Model.Db; this.action = action; if (this.action == BookActions.Add) { btnRemove.Visibility = Visibility.Hidden; } }
// GET: Genres/Delete/5 public ActionResult Delete(int id) { if (BookActions.BookGenreCheck(id)) { Genre genre = db.Genres.Find(id); db.Genres.Remove(genre); db.SaveChanges(); } else { TempData["bookExists"] = "true"; } return(RedirectToAction("Index")); }
public ActionResult Search(string search, FormCollection formCollection) { bool chkSwitch = false; if (formCollection["chkSwitch"] == "on") { chkSwitch = true; } List <CompleteBook> books = BookActions.Search(search, chkSwitch); TempData["books"] = books; //i'm passing model to a different controller so I have to use TempData //TempData = short lived session used for redirects return(RedirectToAction("Index", "Home")); }
public ActionResult SaveUpdateBook(int hiddenId, string Title, string AuthorName, string AuthorSurname, int GenreId, HttpPostedFileBase ImageUrl) { if (ModelState.IsValid) { //New Book modal has data-id=0 //Update Book modal has data-id=BookId if (hiddenId == 0) { BookActions.BookSave(Title, AuthorName, AuthorSurname, GenreId, ImageUrl); } else { int BookId = hiddenId; BookActions.BookUpdate(BookId, Title, AuthorName, AuthorSurname, GenreId, ImageUrl); } } return(RedirectToAction("Index", "Home")); }
public async Task <int> UpdateEpubsFromWeb() { OnNotify("Updating epubs..."); var web = new HtmlWeb(); HtmlDocument detailPage = null; var list = db.Books.Where(b => b.Extension == "epub").ToList(); var counter = 0; foreach (var book in list) { OnNotify($"Updating epubs {++counter}/{list.Count}..."); detailPage = await web.LoadFromWebAsync(book.Url); var downloadLinks = detailPage.DocumentNode.SelectNodes("//span[@class='download-links']/a"); foreach (var node in downloadLinks) { var href = node.Attributes["href"].Value; if (href.Contains(".pdf")) { book.DownloadUrl = href; book.Extension = "pdf"; break; } else if (href.Contains(".epub")) { book.DownloadUrl = href; book.Extension = "epub"; } else if (href.Contains("file.html")) { book.DownloadUrl = href; book.Extension = "rar"; } } } db.SaveChanges(); LastAction = BookActions.Update; OnNotify("Updating completed..."); return(counter); }
// POST: BookActions/Delete/5 //[HttpPost] public ActionResult Delete(int id, string image) { try { string path; // delete book from db and image file from server if (image != "noimage.jpg") { path = Request.MapPath("~/Content/Images/" + image); } else { path = ""; } BookActions.BookDelete(id, path); return(RedirectToAction("Index", "Home")); } catch { return(RedirectToAction("Index", "Home")); } }
public async Task <int> UpdateDbFromWeb_ORG() { var corrections = BookCommonData.Corrections; try { OnNotify("Updating all from web (ORG) ..."); HtmlDocument pageHtml = null; var web = new HtmlWeb(); var postIDs = db.Books.Select(b => b.PostId).Distinct(); var URL = "http://www.allitebooks.org/page"; pageHtml = await web.LoadFromWebAsync("http://www.allitebooks.org"); var pagination = pageHtml.DocumentNode.SelectNodes("//div[@class='pagination clearfix']/a"); PAGES_NUM = int.Parse(pagination.Last().InnerText); int MAX_THERE = 10; var alreadyThereCounter = 0; int booksAdded = 0; for (var page = 0; page <= PAGES_NUM; page++) { var pageList = new List <Book>(); var pageErrUrls = new List <string>(); pageHtml = await web.LoadFromWebAsync($"{URL}/{page}"); var pageBookNodes = pageHtml.DocumentNode.SelectNodes("//article"); //цикл по нодам страницы foreach (var bookElement in pageBookNodes) { var book = new Book(); try { book.PostId = int.Parse(bookElement.Attributes["id"].Value.Substring(5)); if (postIDs.Contains(book.PostId)) { alreadyThereCounter++; if (alreadyThereCounter <= MAX_THERE) // продолжаем цикл { continue; } else { break; //убрали goto } } //Adding book //Primary properties book.Title = bookElement.SelectSingleNode("div/header/h2[@class='entry-title']/a")?.InnerText; book.Title = book.Title?.Trim().Replace("’", "'").Replace("–", "-").Replace("&", "&").Replace("&", "&"); book.Url = bookElement.SelectSingleNode("div/header/h2[@class='entry-title']/a")?.Attributes["href"]?.Value?.Trim(); book.Summary = bookElement.SelectSingleNode("div/div[@class='entry-summary']/p")?.InnerText; book.Sync = 0; //Secondary properties book = await UpdateBookFromWeb_ORG(book); OnNotify($"Loading page {page}..."); pageList.Add(book); } catch (Exception e) { pageErrUrls.Add(book.Url); } } //сюда уходит goto foreach (var b in pageList) { if (!db.Books.Any(_b => _b.PostId == b.PostId)) { db.Books.Add(b); booksAdded++; } } await db.SaveChangesAsync(); File.AppendAllLines("log.txt", pageErrUrls.ToArray()); OnNotify($"Page {page} saved."); if (alreadyThereCounter > MAX_THERE) { break; } Thread.Sleep(DELAY); } OnNotify($"Updating all finished. Added {booksAdded} new books."); LastAction = BookActions.Update; return(booksAdded); } catch (Exception e) { OnNotify($"Error while updating from web: {e.Message}"); return(0); } }
public void RemoveBook(Book model) { db.Books.Remove(model); db.SaveChanges(); LastAction = BookActions.Remove; }
public void AddBook(Book model) { db.Books.Add(model); db.SaveChanges(); LastAction = BookActions.Add; }
public void Save() { db.SaveChanges(); LastAction = BookActions.Save; }
// GET: Genres public ActionResult Index() { return(View(BookActions.GetGenres())); }
public ActionResult CreateGenre(string genre) { int genreId = BookActions.GenreSave(genre); return(Json(new { genreId = genreId })); }
public async Task <int> UpdateDbFromWeb_IN() { var corrections = BookCommonData.Corrections; try { OnNotify("Updating all from web (IN) ..."); HtmlDocument pageHtml = null; var web = new HtmlWeb(); var maxPostID = db.Books.Select(b => b.PostId).Max(); var bookTitles = db.Books.Select(b => b.Title); pageHtml = await web.LoadFromWebAsync(Settings.Default.WebUrl); var pagination = pageHtml.DocumentNode.SelectNodes("//span[@class='pages']"); var pagText = pagination.First().InnerText; var lastnum = pagText.Substring(pagText.LastIndexOf(' ') + 1); PAGES_NUM = int.Parse(lastnum); int MAX_THERE = 10; var alreadyThereCounter = 0; int booksAdded = 0; for (var page = 1; page <= PAGES_NUM; page++) { var newBooks = new List <Book>(); var pageErrUrls = new List <string>(); pageHtml = await web.LoadFromWebAsync($"{Settings.Default.WebUrl}/page/{page}"); var pageBookNodes = pageHtml.DocumentNode.SelectNodes("//div[@class='meta-info-container']"); //h3[@class='entry-title td-module-title']/a"); //цикл по нодам страницы foreach (var bookNode in pageBookNodes) { var book = new Book(); try { var h3 = bookNode.SelectSingleNode("div/div/h3/a"); var title = h3.Attributes["title"].Value.Trim().Replace("’", "'").Replace("–", "-").Replace("&", "&").Replace("&", "&"); if (bookTitles.Contains(title)) { alreadyThereCounter++; if (alreadyThereCounter <= MAX_THERE) // продолжаем цикл { continue; } else { break; //убрали goto } } //Adding book //Primary properties -> goto Details book.Title = title; book.Url = bookNode.SelectSingleNode("div/div[@class='td-read-more']/a").Attributes["href"].Value; //Secondary properties book = await UpdateBookFromWeb_IN(book); OnNotify($"Loading page {page}..."); newBooks.Add(book); } catch (Exception e) { pageErrUrls.Add(book.Url); } } //сюда уходит goto foreach (var b in newBooks) { if (!db.Books.Any(_b => _b.PostId == b.PostId)) { db.Books.Add(b); booksAdded++; } } await db.SaveChangesAsync(); File.AppendAllLines("log.txt", pageErrUrls.ToArray()); OnNotify($"Page {page} saved."); if (alreadyThereCounter > MAX_THERE) { break; } Thread.Sleep(DELAY); } OnNotify($"Updating all finished. Added {booksAdded} new books."); LastAction = BookActions.Update; return(booksAdded); } catch (Exception e) { OnNotify($"Error while updating from web: {e.Message}"); return(0); } }
public void WhenGetTheTitle() { _actualTitle = BookActions.GetTitle(api.GetBook(_code)); }
public ActionResult SideBar() { //notice that a PartialView is being returned return(PartialView(BookActions.GetGenres())); }