예제 #1
0
        public static string ImportBooks(BooveyDbContext context, string inputJson)
        {
            var booksDto      = JsonConvert.DeserializeObject <IEnumerable <BookImportModel> >(inputJson);
            var publishersIds = context.Publishers
                                .Select(x => x.Id)
                                .ToList();
            var authorsIds = context.Authors
                             .Select(x => x.Id)
                             .ToList();

            var categoriesIds = context.Genres
                                .Select(x => x.Id)
                                .ToList();

            foreach (var currentBook in booksDto.Where(x => publishersIds.Contains(x.Publisher)))
            {
                var book = new Book
                {
                    CoverUrl        = currentBook.CoverUrl,
                    Title           = currentBook.Title,
                    Pages           = currentBook.Pages,
                    PublicationDate = DateTime.ParseExact(currentBook.PublicationDate, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                    ASIN            = currentBook.ASIN,
                    ISBN            = currentBook.ISBN,
                    Description     = currentBook.Description,
                    PublisherId     = currentBook.Publisher,
                };

                foreach (var authorId in currentBook.AuthorsId.Where(x => authorsIds.Contains(x)))
                {
                    book.Authors.Add(new AuthorBook
                    {
                        AuthorId = authorId
                    });
                }

                foreach (var categoryId in currentBook.CategoriesIds.Where(x => categoriesIds.Contains(x)))
                {
                    book.BookGenres.Add(new BookGenre
                    {
                        CategoryId = categoryId
                    });
                }

                context.Books.Add(book);
            }
            context.SaveChanges();

            return($"Successfully imported {context.Books.Count()} books.");
        }
예제 #2
0
        public static string ImportCountries(BooveyDbContext context, string inputJson)
        {
            var countriesDto = JsonConvert.DeserializeObject <IEnumerable <CountryImportModel> >(inputJson);

            foreach (var currentCountry in countriesDto)
            {
                var country = new Country
                {
                    Name = currentCountry.Name
                };

                context.Countries.Add(country);
            }
            context.SaveChanges();
            return($"Successfully imported {context.Countries.Count()} countries.");
        }
예제 #3
0
        public static string ImportPublishers(BooveyDbContext context, string inputJson)
        {
            var publishersDto = JsonConvert.DeserializeObject <IEnumerable <PublisherImportModel> >(inputJson);

            foreach (var currentPubliher in publishersDto)
            {
                var publisher = new Publisher
                {
                    Name = currentPubliher.Name
                };

                context.Publishers.Add(publisher);
            }

            context.SaveChanges();
            return($"Successfully imported {context.Publishers.Count()} publishers.");
        }
예제 #4
0
        public static string ImportAuthors(BooveyDbContext context, string inputJson)
        {
            var authorsDto = JsonConvert.DeserializeObject <IEnumerable <AuthorImportModel> >(inputJson);


            foreach (var currentAuthor in authorsDto)
            {
                var author = new Author
                {
                    FullName = currentAuthor.FullName,
                    Summary  = currentAuthor.Summary,
                };

                context.Authors.Add(author);
            }
            context.SaveChanges();
            return($"Successfully imported {context.Authors.Count()} authors.");
        }
예제 #5
0
        public static string ImportUsers(BooveyDbContext context, string inputJson)
        {
            var countriesIds = context.Countries.Select(x => x.Id).ToList();
            var usersDto     = JsonConvert.DeserializeObject <IEnumerable <UserImportModel> >(inputJson).Where(x => countriesIds.Contains(x.CountryId));
            var booksIds     = context.Books.Select(x => x.Id).ToList();

            foreach (var currentUser in usersDto)
            {
                var user = new User
                {
                    FirstName = currentUser.FirstName,
                    LastName  = currentUser.LastName,
                    Age       = currentUser.Age,
                    Email     = currentUser.Email,
                    CountryId = currentUser.CountryId
                };

                foreach (var favoriteBook in currentUser.FavoriteBooksIds.Where(x => booksIds.Contains(x)))
                {
                    var bookFavoriteByUser = new BookFavoriteByUser
                    {
                        BookId = favoriteBook
                    };

                    user.FavoriteBooks.Add(bookFavoriteByUser);
                }

                foreach (var likedBook in currentUser.LikedBooksIds.Where(x => booksIds.Contains(x)))
                {
                    var bookLikedByUser = new BookLikedByUser
                    {
                        BookId = likedBook
                    };

                    user.LikedBooks.Add(bookLikedByUser);
                }

                context.Users.Add(user);
            }

            context.SaveChanges();

            return($"Successfully imported {context.Users.Count()} users.");
        }
예제 #6
0
        public static string ImportBooksReviews(BooveyDbContext context, string inputJson)
        {
            var booksReviewsDto = JsonConvert.DeserializeObject <IEnumerable <BookReviewImportModel> >(inputJson);
            var usersIds        = context.Users.Select(x => x.Id).ToList();
            var booksIds        = context.Books.Select(x => x.Id).ToList();

            var booksReviews = booksReviewsDto.Where(x => booksIds.Contains(x.BookId) && usersIds.Contains(x.UserId))
                               .Select(x => new Review
            {
                Rating      = x.Rating,
                Comment     = x.Comment,
                PublishedOn = DateTime.ParseExact(x.PublishedOn, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                //var isValidDate = DateTime.TryParseExact(currentGame.ReleaseDate, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var releaseDate);
                UserId = x.UserId,
                BookId = x.BookId
            })
                               .ToList();

            context.BooksReviews.AddRange(booksReviews);
            context.SaveChanges();
            return($"Successfully imported {context.BooksReviews.Count()} book reviews.");
        }
예제 #7
0
        public static string ImportUserFollowers(BooveyDbContext context, string inputJson)
        {
            var usersIds         = context.Users.Select(x => x.Id).ToList();
            var userFollowersDto = JsonConvert.DeserializeObject <IEnumerable <UserFollower> >(inputJson)
                                   .Where(x => usersIds.Contains(x.FollowerId) && usersIds.Contains(x.UserId));

            foreach (var currentUser in userFollowersDto)
            {
                var userFollower = new UserFollower
                {
                    UserId     = currentUser.UserId,
                    FollowerId = currentUser.FollowerId
                };


                context.UsersFollowers.Add(userFollower);
            }

            context.SaveChanges();

            return($"Successfully imported {context.UsersFollowers.Select(x => x.UserId).Distinct().Count()} users with {context.UsersFollowers.Select(x => x.FollowerId).Count()} followers.");
        }