public static async Task <bool> SaveNewBookFromGoogle(string isbn) { try { using (var client = new HttpClient()) { string url = $"https://www.googleapis.com/books/v1/volumes?q=isbn={isbn}"; string json = await client.GetStringAsync(url); var resource = JObject.Parse(json); var item = resource["items"][0]; var livre = GetBookFromJSON(item); if (livre.Isbn != isbn) { return(false); } using (var context = new DBcontext()) { var newAuthors = Author.GetAuthorsFromJSON(item).Select(x => { var author = context.Authors.FirstOrDefault(a => a.Name == x.Name); if (author != null) { return(author); } context.Add(x); context.SaveChanges(); return(x); }); var newCategories = Category.GetCategoriesFromJSON(item).Select(x => { var category = context.Categories.FirstOrDefault(a => a.Name == x.Name); if (category != null) { return(category); } context.Add(x); context.SaveChanges(); return(x); }); context.Add(livre); context.SaveChanges(); newAuthors.Select(a => a.Id).ToList().ForEach(x => { context.Add(new Written { BookId = livre.Id, AuthorId = x }); }); newCategories.Select(c => c.Id).ToList().ForEach(x => { context.Add(new Categorized { BookId = livre.Id, CategoryId = x }); }); context.SaveChanges(); } } return(true); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } }
public void Seed() { using (var db = new DBcontext()) { List <string> blasphemes = new List <string> { "bite", "fion", "vanus", "add", "aaerr", "scout", "leto", "bad", "ont", "fia", "parse", "ok" }.Select(x => x.ToUpper()).ToList(); try { var lien = db.Taggeds.Where(cs => blasphemes.Contains(cs.Tag.Name.ToUpper())); var assoc = lien.ToList(); assoc.ForEach(x => { db.Taggeds.Remove(x); db.SaveChanges(); }); var taggeds = db.Tags.Where(cs => blasphemes.Contains(cs.Name.ToUpper())); var listTodelete = taggeds.ToList(); listTodelete.ForEach(x => { db.Tags.Remove(x); db.SaveChanges(); }); } catch (Exception e) { Debug.WriteLine(e.Message); } } // using (var client = new HttpClient ()) { // var urls = new List<string> { // "https://www.googleapis.com/books/v1/volumes?q=Fantasy&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=Science+Fiction&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=History&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=Drama&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=Science&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=Fiction&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=Heroic&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=Vagner&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=Horor&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=games+of+thrones&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=star+wars&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=star+trek&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=dystopia&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=uchronic&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=SMITH+Dan&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=Ormston+Dean&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=Infinity+Wars&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=comics&langRestrict=en&maxResults=40", // "https://www.googleapis.com/books/v1/volumes?q=dc&langRestrict=en&maxResults=40" // }; // foreach (var url in urls) { // string json = await client.GetStringAsync (url); // var resource = JObject.Parse (json); // Insert (resource); // } // } }
public void Insert(JObject resource) { resource["items"].Select(t => t).ToList().ForEach(i => { try { using (var context = new DBcontext()) { var newAuthors = Author.GetAuthorsFromJSON(i).Select(x => { var author = context.Authors.FirstOrDefault(a => a.Name == x.Name); if (author != null) { return(author); } context.Add(x); context.SaveChanges(); return(x); }); var newCategories = Category.GetCategoriesFromJSON(i).Select(x => { var category = context.Categories.FirstOrDefault(a => a.Name == x.Name); if (category != null) { return(category); } context.Add(x); context.SaveChanges(); return(x); }); var livre = Book.GetBookFromJSON(i); context.Add(livre); context.SaveChanges(); newAuthors.Select(a => a.Id).ToList().ForEach(x => { context.Add(new Written { BookId = livre.Id, AuthorId = x }); }); newCategories.Select(c => c.Id).ToList().ForEach(x => { context.Add(new Categorized { BookId = livre.Id, CategoryId = x }); }); context.SaveChanges(); } } catch { // Console.WriteLine (e.Message); } }); }