public IActionResult Books_Category() { APIHandler webHandler = new APIHandler(); Book_Category booksCategory = webHandler.GetBooksCategory(); return(View(booksCategory)); }
public Book_Category GetBooksCategory() { //string NATIONAL_PARK_API_PATH = BASE_URL + "lists/current/hardcover-fiction.json"; string NATIONAL_PARK_API_PATH = "https://api.nytimes.com/svc/books/v3/lists/names.json?api-key=ywqLjGd3TQkfHri1fhkie9BBWJOz91de"; string booksData = ""; Book_Category booksCategory = null; httpClient.BaseAddress = new Uri(NATIONAL_PARK_API_PATH); // It can take a few requests to get back a prompt response, if the API has not received // calls in the recent past and the server has put the service on hibernation try { HttpResponseMessage response = httpClient.GetAsync(NATIONAL_PARK_API_PATH).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { booksData = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); } if (!booksData.Equals("")) { // JsonConvert is part of the NewtonSoft.Json Nuget package booksCategory = JsonConvert.DeserializeObject <Book_Category>(booksData); } } catch (Exception e) { // This is a useful place to insert a breakpoint and observe the error message Console.WriteLine(e.Message); } return(booksCategory); }
public static bool DeleteAllCategoryAndAuthor(int ID) { try { using (Book_Sale_ManagerEntities context = new Book_Sale_ManagerEntities()) { Book_Author book_author = context.Book_Author.FirstOrDefault(t => t.book_ID == ID); while (book_author != null) { context.Book_Author.Remove(book_author); context.SaveChanges(); book_author = context.Book_Author.FirstOrDefault(t => t.book_ID == ID); } Book_Category book_category = context.Book_Category.FirstOrDefault(t => t.book_ID == ID); while (book_category != null) { context.Book_Category.Remove(book_category); context.SaveChanges(); book_category = context.Book_Category.FirstOrDefault(t => t.book_ID == ID); } if (book_author == null && book_category == null) { return(true); } return(false); } } catch (Exception ex) { LogService.log("BOOK", ex.StackTrace); return(false); } }
public static bool UpDateBook(BookData b) { try { DeleteAllCategoryAndAuthor(b.ID); Console.WriteLine("ok go here"); using (Book_Sale_ManagerEntities context = new Book_Sale_ManagerEntities()) { Book book = context.Books.FirstOrDefault(bo => bo.ID == b.ID); if (book != null) { book.ISBN = b.ISBN; book.name = b.Name; book.price = b.Price; book.quantity = b.Quantity; book.status = b.Status; book.year = b.Year; book.publisher_ID = b.Publisher_ID; book.description = b.Description; for (int i = 0; i < b.Author.Count; i++) { int ID = b.Author[i].ID; Author author = context.Authors.FirstOrDefault(a => a.ID == ID); Book_Author BA = new Book_Author(); BA.Author = author; BA.Book = book; book.Book_Author.Add(BA); } for (int i = 0; i < b.Category.Count; i++) { int ID = b.Category[i].ID; Category category = context.Categories.FirstOrDefault(c => c.ID == ID); Book_Category temp = new Book_Category(); temp.Category = category; temp.Book = book; book.Book_Category.Add(temp); } context.SaveChanges(); } return(true); } } catch (Exception ex) { LogService.log("Erro at Book Service", ex.StackTrace); return(false); } }
public static bool AddBook(BookData b) { Book book = new Book(); book.ISBN = b.ISBN; book.description = b.Description; book.name = b.Name; book.price = b.Price; book.quantity = b.Quantity; book.status = b.Status; book.year = b.Year; book.publisher_ID = b.Publisher_ID; try { using (Book_Sale_ManagerEntities context = new Book_Sale_ManagerEntities()) { for (int i = 0; i < b.Author.Count; i++) { int ID = b.Author[i].ID; Author author = context.Authors.FirstOrDefault(a => a.ID == ID); Book_Author BA = new Book_Author(); BA.Author = author; BA.Book = book; book.Book_Author.Add(BA); } for (int i = 0; i < b.Category.Count; i++) { int ID = b.Category[i].ID; Category category = context.Categories.FirstOrDefault(c => c.ID == ID); Book_Category temp = new Book_Category(); temp.Category = category; temp.Book = book; book.Book_Category.Add(temp); } context.Books.Add(book); context.SaveChanges(); return(true); } } catch (Exception ex) { LogService.log("BOOK", ex.StackTrace); return(false); } }
public async Task <IActionResult> Create(BookCreateViewModel ViewModel) { if (ModelState.IsValid) { List <Author_Book> author_Books = new List <Author_Book>(); List <Book_Translator> translators = new List <Book_Translator>(); List <Book_Category> categories = new List <Book_Category>(); DateTime?PublishDate = null; if (ViewModel.IsPublish == true) { PublishDate = DateTime.Now; } Book book = new Book() { Delete = false, ISBN = ViewModel.ISBN, IsPublish = ViewModel.IsPublish, NumOfPages = ViewModel.NumOfPages, Stock = ViewModel.Stock, Price = ViewModel.Price, LanguageID = ViewModel.LanguageID, Summary = ViewModel.Summary, Title = ViewModel.Title, PublishYear = ViewModel.PublishYear, PublishDate = PublishDate, Weight = ViewModel.Weight, PublisherID = ViewModel.PublisherID }; await _context.Books.AddAsync(book); if (ViewModel.AuthorID != null) { for (int i = 0; i < ViewModel.AuthorID.Length; i++) { Author_Book author_Book = new Author_Book() { BookID = book.BookID, AuthorID = ViewModel.AuthorID[i], }; author_Books.Add(author_Book); } } await _context.Author_Books.AddRangeAsync(author_Books); if (ViewModel.TranslatorID != null) { for (int i = 0; i < ViewModel.TranslatorID.Length; i++) { Book_Translator translator = new Book_Translator() { BookID = book.BookID, TranslatorID = ViewModel.TranslatorID[i], }; translators.Add(translator); } await _context.Book_Translators.AddRangeAsync(translators); } if (ViewModel.CategoryID != null) { for (int i = 0; i < ViewModel.CategoryID.Length; i++) { Book_Category category = new Book_Category() { BookID = book.BookID, CategoryID = ViewModel.CategoryID[i], }; categories.Add(category); } await _context.Book_Categories.AddRangeAsync(categories); } await _context.SaveChangesAsync(); return(RedirectToAction("index")); } else { ViewBag.LanguageID = new SelectList(_context.Languages, "LanguageID", "LanguageName"); ViewBag.PublisherID = new SelectList(_context.Publishers, "PublisherID", "PublisherName"); ViewBag.AuthorID = new SelectList(_context.Authors.Select(t => new AuthorList { AuthorID = t.AuthorID, NameFamily = t.FirstName + " " + t.LastName }), "AuthorID", "NameFamily"); ViewBag.TranslatorID = new SelectList(_context.Translator.Select(t => new TranslatorList { TranslatorID = t.TranslatorID, NameFamily = t.Name + " " + t.Family }), "TranslatorID", "NameFamily"); ViewModel.Categories = _repository.GenerateAllTree(); return(View(ViewModel)); } }