Пример #1
0
        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);
            }
        }
Пример #2
0
        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);
                }
            });
        }