public static string ImportBooks(BookShopContext context, string xmlString)
        {
            var books = XmlConverter.Deserializer <BooksXmlImportDTO>(xmlString, "Books");

            var bookList = new List <Book>();

            var sb = new StringBuilder();

            foreach (var book in books)
            {
                if (!IsValid(book))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var bookToAdd = new Book
                {
                    Name        = book.Name,
                    Genre       = (Genre)Enum.Parse(typeof(Genre), book.Genre),
                    Price       = book.Price,
                    Pages       = book.Pages,
                    PublishedOn = DateTime.ParseExact(book.PublishedOn, "MM/dd/yyyy", CultureInfo.InvariantCulture)
                };
                sb.AppendLine($"Successfully imported book {book.Name} for {book.Price:f2}.");

                bookList.Add(bookToAdd);
            }

            context.AddRange(bookList);
            context.SaveChanges();


            return(sb.ToString().TrimEnd());
        }
Exemplo n.º 2
0
        public static string ImportBooks(BookShopContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            List <Book> books = new List <Book>();

            ImportBookDto[] bookDtos = XmlConverter.Deserializer <ImportBookDto>(xmlString, "Books");

            foreach (var bookDto in bookDtos)
            {
                if (!IsValid(bookDto))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                Book book = new Book
                {
                    Name        = bookDto.Name,
                    Genre       = (Genre)bookDto.Genre,
                    Price       = bookDto.Price,
                    Pages       = bookDto.Pages,
                    PublishedOn = DateTime.ParseExact(bookDto.PublishedOn, "MM/dd/yyyy", CultureInfo.InvariantCulture)
                };

                sb.AppendLine(string.Format(SuccessfullyImportedBook, book.Name, book.Price));

                books.Add(book);
            }

            context.Books.AddRange(books);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Exemplo n.º 3
0
        public static string ImportBooks(BookShopContext context, string xmlString)
        {
            var sb = new StringBuilder();

            List <Book> booksToAdd = new List <Book>();

            var books = XmlConverter.Deserializer <BookXmlImportModel>(xmlString, "Books");

            foreach (var book in books)
            {
                if (!IsValid(book))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                DateTime publishedOn;
                bool     isValidDate = DateTime.TryParseExact(
                    book.PublishedOn,
                    "MM/dd/yyyy",
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None,
                    out publishedOn);

                if (!isValidDate)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                Book bookToAdd = new Book
                {
                    Name        = book.Name,
                    Pages       = book.Pages,
                    Price       = book.Price,
                    PublishedOn = publishedOn,
                    Genre       = (Genre)book.Genre
                };

                booksToAdd.Add(bookToAdd);
                sb.AppendLine(string.Format(SuccessfullyImportedBook, book.Name, book.Price));
            }

            context.Books.AddRange(booksToAdd);

            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Exemplo n.º 4
0
        public static string ImportBooks(BookShopContext context, string xmlString)
        {
            var outputResult = new StringBuilder();
            var booksResult  = XmlConverter.Deserializer <ImportBooksDTO>(xmlString, "Books");
            var booksToAdd   = new List <Book>();

            foreach (var bookDto in booksResult)
            {
                if (!IsValid(bookDto))
                {
                    outputResult.AppendLine(ErrorMessage);
                    continue;
                }
                DateTime publishedOn;

                bool isDateValid = DateTime.TryParseExact(
                    bookDto.PublishedOn,
                    "MM/dd/yyyy",
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None,
                    out publishedOn);
                if (!isDateValid)
                {
                    outputResult.AppendLine(ErrorMessage);
                    continue;
                }

                Book newBook = new Book
                {
                    Name        = bookDto.Name,
                    Genre       = (Genre)bookDto.Genre,
                    Price       = bookDto.Price,
                    Pages       = bookDto.Pages,
                    PublishedOn = publishedOn,
                };

                booksToAdd.Add(newBook);
                outputResult.AppendLine(String.Format(SuccessfullyImportedBook, newBook.Name, newBook.Price));
            }

            context.Books.AddRange(booksToAdd);
            context.SaveChanges();
            return(outputResult.ToString().TrimEnd());
        }
        public static string ImportBooks(BookShopContext context, string xmlString)
        {
            var bookDTOs = XmlConverter.Deserializer<BookImportModel>(xmlString, "Books");

            var result = new StringBuilder();
            var books = new List<Book>();

            foreach (var bookDTO in bookDTOs)
            {
                if (!IsValid(bookDTO))
                {
                    result.AppendLine(ErrorMessage);
                    continue;
                }

                DateTime date;
                bool isDateValid = DateTime.TryParseExact(bookDTO.PublishedOn, "MM/dd/yyyy",
                    CultureInfo.InvariantCulture, DateTimeStyles.None, out date);

                if (!isDateValid)
                {
                    result.AppendLine(ErrorMessage);
                    continue;
                }

                var newBook = new Book()
                {
                    Name = bookDTO.Name,
                    Genre = Enum.Parse<Genre>(bookDTO.Genre),
                    Price = bookDTO.Price,
                    Pages = bookDTO.Pages,
                    PublishedOn = date
                };

                books.Add(newBook);
                result.AppendLine(String.Format(SuccessfullyImportedBook,
                    newBook.Name, newBook.Price));
            }
            context.Books.AddRange(books);
            context.SaveChanges();
            return result.ToString().Trim();
        }
        public static string ImportBooks(BookShopContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            var books = XmlConverter.Deserializer <BookInputModel>(xmlString, "Books");

            foreach (var currentBook in books)
            {
                var isValidPublishDate = DateTime.TryParseExact(
                    currentBook.PublishedOn,
                    "MM/dd/yyyy",
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None,
                    out DateTime currentPublishDate);

                bool isValidGenre = Enum.TryParse(currentBook.Genre, out Genre currentGenre);

                if (!IsValid(currentBook) ||
                    !isValidPublishDate ||
                    !isValidGenre)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var bookToImport = new Book
                {
                    Name        = currentBook.Name,
                    Genre       = currentGenre,
                    Pages       = currentBook.Pages,
                    Price       = currentBook.Price,
                    PublishedOn = currentPublishDate
                };

                context.Books.Add(bookToImport);
                sb.AppendLine(string.Format(SuccessfullyImportedBook, bookToImport.Name, bookToImport.Price));
                context.SaveChanges();
            }
            return(sb.ToString().TrimEnd());
        }
Exemplo n.º 7
0
        public static string ImportBooks(BookShopContext context, string xmlString)
        {
            var root = "Books";
            var data = XmlConverter.Deserializer <ImportBookDTO>(xmlString, root);

            List <Book>   books = new List <Book>();
            StringBuilder sb    = new StringBuilder();

            foreach (var item in data)
            {
                if (IsValid(item))
                {
                    Enum.TryParse(item.Genre.ToString(), true, out Genre genre);
                    var book = new Book
                    {
                        Name        = item.Name,
                        Genre       = genre,
                        Price       = item.Price,
                        Pages       = item.Pages,
                        PublishedOn = DateTime.ParseExact(item.PublishedOn, "MM/dd/yyyy", CultureInfo.InvariantCulture)
                    };

                    books.Add(book);
                    sb.AppendLine(string.Format(SuccessfullyImportedBook, book.Name, book.Price));
                }
                else
                {
                    sb.AppendLine(ErrorMessage);
                }
            }

            context.Books.AddRange(books);
            context.SaveChanges();

            return(sb.ToString());
        }