コード例 #1
0
        //02. Import Products
        public static string ImportProducts(ProductShopContext context, string inputXml)
        {
            const string roodElement = "Products";

            var productDtos = XMLConverter.Deserializer <ImprotProductDto>(inputXml, roodElement);

            var products = productDtos
                           .Select(p => new Product
            {
                Name     = p.Name,
                Price    = p.Price,
                BuyerId  = p.BuyerId,
                SellerId = p.SellerId
            })
                           .ToArray();

            context.Products.AddRange(products);

            context.SaveChanges();

            return($"Successfully imported {products.Length}");
        }
コード例 #2
0
        public static string ImportCategories(ProductShopContext context, string inputXml)
        {
            const string xmlRoot = "Categories";

            var categoriesResult = XMLConverter.Deserializer <ImportCategoryDto>(inputXml, xmlRoot);

            var categories = new List <Category>();

            foreach (var importCategoryDto in categoriesResult)
            {
                var category = new Category
                {
                    Name = importCategoryDto.Name
                };
                categories.Add(category);
            }

            context.Categories.AddRange(categories);
            context.SaveChanges();

            return($"Successfully imported {context.Categories.Count()}");
        }
コード例 #3
0
        public static string ImportParts(CarDealerContext context, string inputXml)
        {
            var root = "Parts";

            var partsData = XMLConverter.Deserializer <ImportPartsDTO>(inputXml, root);

            var parts = partsData
                        .Where(p => context.Suppliers.Count() >= p.SupplierId && p.SupplierId > 0)
                        .Select(p => new Part
            {
                Name       = p.Name,
                Price      = p.Price,
                Quantity   = p.Quantity,
                SupplierId = p.SupplierId
            })
                        .ToList();

            context.Parts.AddRange(parts);
            context.SaveChanges();

            return($"Successfully imported {parts.Count}");
        }
コード例 #4
0
        //Query 3. Import Categories
        public static string ImportCategories(ProductShopContext context, string inputXml)
        {
            //var result = ImportCategories(context, categoriesXml);
            //Console.WriteLine(result);

            const string rootElement = "Categories";

            var categoryResult = XMLConverter.Deserializer <CategoryDto>(inputXml, rootElement);

            //var categories = new List<Category>();

            //foreach (var cat in categoryResult)
            //{
            //    if (cat.Name == null)
            //    {
            //        continue;
            //    }

            //    var category = new Category
            //    {
            //        Name = cat.Name,
            //    };

            //    categories.Add(category);
            //}

            var categories = categoryResult
                             .Where(c => c.Name != null)
                             .Select(c => new Category
            {
                Name = c.Name
            })
                             .ToArray();

            context.Categories.AddRange(categories);
            int categoriesCount = context.SaveChanges();

            return($"Successfully imported {categoriesCount}");
        }
コード例 #5
0
ファイル: StartUp.cs プロジェクト: GledachevY/softuni
        // problem 4
        public static string ImportCategoryProducts(ProductShopContext context, string inputXml)
        {
            const string rootElement = "CategoryProducts ";

            var categoryProductDtos = XMLConverter.Deserializer <ImportCategoriesProductsDto>(inputXml, rootElement);

            //var categories = categoryProductDtos.Where(c=>).Select(cp => new CategoryProduct
            //{
            //    CategoryId = cp.CategoryId,
            //    ProductId = cp.Productid
            //})
            //    .ToArray();

            var categories = new List <CategoryProduct>();

            foreach (var dto in categoryProductDtos)
            {
                bool doesExists = context.Products.Any(x => x.Id == dto.Productid) &&
                                  context.Categories.Any(x => x.Id == dto.CategoryId);

                if (!doesExists)
                {
                    continue;
                }

                var categoryProduct = new CategoryProduct
                {
                    CategoryId = dto.CategoryId,
                    ProductId  = dto.Productid
                };

                categories.Add(categoryProduct);
            }

            context.CategoryProducts.AddRange(categories);
            context.SaveChanges();

            return($"Successfully imported {categories.Count}");
        }
コード例 #6
0
        public static string ImportBooks(BookShopContext context, string xmlString)
        {
            var rootAttributeName = "Books";

            var booksDTO = XMLConverter.Deserializer <ImportBookDTO>(xmlString, rootAttributeName);

            var books = new List <Book>();

            var result = string.Empty;

            foreach (var bookDTO in booksDTO)
            {
                if (!IsValid(bookDTO))
                {
                    result += ErrorMessage + Environment.NewLine;
                    continue;
                }
                DateTime publishedOn;
                var      validDate = DateTime.TryParseExact(bookDTO.PublishedOn, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out publishedOn);

                books.Add(new Book
                {
                    Name        = bookDTO.Name,
                    Genre       = (Genre)bookDTO.Genre,
                    Price       = bookDTO.Price,
                    Pages       = bookDTO.Pages,
                    PublishedOn = publishedOn
                });

                result += $"Successfully imported book {bookDTO.Name} for {bookDTO.Price:F2}." + Environment.NewLine;
            }
            ;

            context.Books.AddRange(books);

            context.SaveChanges();

            return(result.Trim());
        }
コード例 #7
0
        public static string ImportCategoryProducts(ProductShopContext context, string inputXml)
        {
            const string rootElement = "CategoryProducts";

            var categoryProductsDtos = XMLConverter.Deserializer <ImportCategoryProductDTO>(inputXml, rootElement);

            var categoriesProducts = categoryProductsDtos
                                     .Where(i =>
                                            context.Categories.Any(s => s.Id == i.CategoryId) &&
                                            context.Products.Any(s => s.Id == i.ProductId))
                                     .Select(c => new CategoryProduct
            {
                CategoryId = c.CategoryId,
                ProductId  = c.ProductId,
            })
                                     .ToArray();

            context.CategoryProducts.AddRange(categoriesProducts);
            context.SaveChanges();

            return($"Successfully imported {categoriesProducts.Length}");
        }
コード例 #8
0
        //03. Import Categories
        public static string ImportCategories(ProductShopContext context, string inputXml)
        {
            const string rootElement = "Categories";

            var categoriesDtio = XMLConverter.Deserializer <ImportCategoryDto>(inputXml, rootElement);

            var categories = categoriesDtio
                             .Where(c => c.Name != null)
                             .Select(p => new Category
            {
                Name = p.Name
            })
                             .ToArray();

            /*
             * List<Category> categories = new List<Category>();
             *
             * foreach (var dto in categoriesDtio)
             * {
             *  if (dto.Name == null)
             *  {
             *      continue;
             *  }
             *
             *  var category = new Category
             *  {
             *      Name = dto.Name
             *  };
             *
             *  categories.Add(category);
             * }
             */

            context.Categories.AddRange(categories);
            context.SaveChanges();

            return($"Successfully imported {categories.Length}");
        }
コード例 #9
0
        //Problem01
        public static string ImportUsers(ProductShopContext context, string inputXml)
        {
            var usersResult = XMLConverter.Deserializer <ImportUserDto>(inputXml, "Users");

            List <User> users = new List <User>();

            foreach (var u in usersResult)
            {
                User user = new User()
                {
                    FirstName = u.FirstName,
                    LastName  = u.LastName,
                    Age       = u.Age
                };

                users.Add(user);
            }

            context.AddRange(users);
            context.SaveChanges();

            return($"Successfully imported {users.Count}");
        }
コード例 #10
0
        // >>Importing<< //

        //Problem 01
        public static string ImportUsers(ProductShopContext context, string inputXml)
        {
            const string root  = "Users";
            var          users = XMLConverter.Deserializer <ImportUserDTO>(inputXml, root);

            var usersResult = new List <User>();

            foreach (var user in users)
            {
                var currrentUser = new User
                {
                    FirstName = user.FirstName,
                    LastName  = user.LastName,
                    Age       = user.Age
                };

                usersResult.Add(currrentUser);
            }
            context.Users.AddRange(usersResult);
            context.SaveChanges();

            return($"Successfully imported {usersResult.Count}");
        }
コード例 #11
0
        //Query 9. Import Suppliers
        public static string ImportSuppliers(CarDealerContext context, string inputXml)
        {
            //var result = ImportSuppliers(context, suppliersXml);
            //Console.WriteLine(result);

            const string rootElement = "Suppliers";

            var supplierResult = XMLConverter.Deserializer <ImportSuppliersDto>(inputXml, rootElement);

            var suppliers = supplierResult
                            .Select(s => new Supplier
            {
                Name       = s.Name,
                IsImporter = s.IsImporter
            })
                            .ToArray();


            context.Suppliers.AddRange(suppliers);
            int suppliersCount = context.SaveChanges();

            return($"Successfully imported {suppliersCount}");
        }
コード例 #12
0
        public static string ImportCars(CarDealerContext context, string inputXml)
        {
            var root    = "Cars";
            var cars    = new List <Car>();
            var carData = XMLConverter.Deserializer <ImportCarsDTO>(inputXml, root);

            foreach (var carInfo in carData)
            {
                var distinctParts = carInfo.CarParts
                                    .Select(p => p.Id)
                                    .Distinct()
                                    .ToList();

                var actualParts = distinctParts
                                  .Where(id => context.Parts.Any(p => p.Id == id))
                                  .ToList();

                var car = new Car
                {
                    Make              = carInfo.Make,
                    Model             = carInfo.Model,
                    TravelledDistance = carInfo.TravelledDistance,
                    PartCars          = actualParts.Select(id => new PartCar
                    {
                        PartId = id
                    })
                                        .ToList()
                };

                cars.Add(car);
            }

            context.Cars.AddRange(cars);
            context.SaveChanges();

            return($"Successfully imported {cars.Count}");
        }
コード例 #13
0
        //Query 11. Import Cars
        public static string ImportCars(CarDealerContext context, string inputXml)
        {
            //var result = ImportCars(context, carsXml);
            //Console.WriteLine(result);

            const string rootElement = "Cars";

            var carsResult = XMLConverter.Deserializer <ImportCarsDto>(inputXml, rootElement);

            var cars = new List <Car>();

            foreach (var carDto in carsResult)
            {
                var uniqueParts = carDto.Parts.Select(p => p.Id).Distinct().ToArray();
                var realParts   = uniqueParts.Where(id => context.Parts.Any(rp => rp.Id == id));


                var car = new Car
                {
                    Make              = carDto.Make,
                    Model             = carDto.Model,
                    TravelledDistance = carDto.TraveledDistance,
                    PartCars          = realParts.Select(id => new PartCar
                    {
                        PartId = id
                    })
                                        .ToArray()
                };

                cars.Add(car);
            }

            context.Cars.AddRange(cars);
            int carsCount = context.SaveChanges();

            return($"Successfully imported {carsCount}");
        }
コード例 #14
0
        //10. Import Parts
        public static string ImportParts(CarDealerContext context, string inputXml2)
        {
            var rootAttributeName = "Parts";

            var partsDTO = XMLConverter.Deserializer <ImportPartDTO>(inputXml2, rootAttributeName);

            var supplierIds = context.Suppliers.Select(x => x.Id);

            var parts = partsDTO
                        .Where(x => supplierIds.Contains(x.SupplierId))
                        .Select(x => new Part
            {
                Name       = x.Name,
                Price      = x.Price,
                Quantity   = x.Quantity,
                SupplierId = x.SupplierId
            });

            context.Parts.AddRange(parts);

            context.SaveChanges();

            return($"Successfully imported {parts.Count()}");
        }
コード例 #15
0
ファイル: StartUp.cs プロジェクト: GledachevY/softuni
        // problem 1
        public static string ImportUsers(ProductShopContext context, string inputXml)
        {
            const string rootElement = "Users";
            var          usersResult = XMLConverter.Deserializer <ImportUserDto>(inputXml, rootElement);

            List <User> users = new List <User>();

            foreach (var importUSerDto in usersResult)
            {
                var user = new User
                {
                    FirstName = importUSerDto.Firstname,
                    LastName  = importUSerDto.LastName,
                    Age       = importUSerDto.Age
                };

                users.Add(user);
            }

            context.Users.AddRange(users);
            context.SaveChanges();

            return($"Successfully imported {users.Count}");
        }
コード例 #16
0
        public static string ImportBooks(BookShopContext context, string xmlString)
        {
            var sb = new StringBuilder();

            const string rootElement = "Books";

            var booksResult = XMLConverter.Deserializer <ImportBooksDto>(xmlString, rootElement);

            var bookList = new List <Book>();

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

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

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

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

            return(sb.ToString().Trim());
        }
コード例 #17
0
        //01. Import Users
        public static string ImportUsers(ProductShopContext context, string inputXml)
        {
            const string roodElement = "Users";
            var          usersResult = XMLConverter.Deserializer <ImportUserDto>(inputXml, roodElement);

            /* Първи начин
             * List<User> users = new List<User>();
             *
             * foreach (var importUserDtio in usersResult)
             * {
             *  var user = new User
             *  {
             *      FirstName = importUserDtio.FirstName,
             *      LastName = importUserDtio.LastName,
             *      Age = importUserDtio.Age
             *  };
             *
             *  users.Add(user);
             * }
             */

            //Втори начин
            var users = usersResult
                        .Select(u => new User
            {
                FirstName = u.FirstName,
                LastName  = u.LastName,
                Age       = u.Age
            })
                        .ToArray();

            context.Users.AddRange(users);
            context.SaveChanges();

            return($"Successfully imported {users.Length}");
        }
コード例 #18
0
        //Query 12. Import Customers
        public static string ImportCustomers(CarDealerContext context, string inputXml)
        {
            //var result = ImportCustomers(context, customersXml);
            //Console.WriteLine(result);

            const string rootElement = "Customers";

            var customerResult = XMLConverter.Deserializer <ImportCustomersDto>(inputXml, rootElement);

            var customers = customerResult
                            .Select(c => new Customer
            {
                Name          = c.Name,
                BirthDate     = DateTime.Parse(c.BirthDate),
                IsYoungDriver = c.IsYoungDriver
            })
                            .ToArray();

            context.Customers.AddRange(customers);
            int customersCount = context.SaveChanges();


            return($"Successfully imported {customersCount}");
        }
コード例 #19
0
        //Query 2. Import Products
        public static string ImportProducts(ProductShopContext context, string inputXml)
        {
            //var result = ImportProducts(context, productsXml);
            //Console.WriteLine(result);

            const string rootElement = "Products";

            var productsResult = XMLConverter.Deserializer <ImportProductDto>(inputXml, rootElement);

            var products = productsResult
                           .Select(p => new Product
            {
                Name     = p.Name,
                Price    = p.Price,
                SellerId = p.SellerId,
                BuyerId  = p.BuyerId
            })
                           .ToArray();

            context.Products.AddRange(products);
            int productsCount = context.SaveChanges();

            return($"Successfully imported {productsCount}");
        }
コード例 #20
0
        public static string ImportCategoryProducts(ProductShopContext context, string inputXml)
        {
            const string xmlRoot = "CategoryProducts";

            var categoryProductsResult = XMLConverter.Deserializer <ImportCategoryProductsDto>(inputXml, xmlRoot);

            var categoryProducts = new List <CategoryProduct>();

            foreach (var importCategoryProductsDto in categoryProductsResult)
            {
                var categoryProduct = new CategoryProduct
                {
                    CategoryId = importCategoryProductsDto.CategoryId,
                    ProductId  = importCategoryProductsDto.ProductId
                };

                categoryProducts.Add(categoryProduct);
            }

            context.CategoryProducts.AddRange(categoryProducts);
            context.SaveChanges();

            return($"Successfully imported {context.CategoryProducts.Count()}");
        }
コード例 #21
0
        //Problem02
        public static string ImportProducts(ProductShopContext context, string inputXml)
        {
            var usersResult = XMLConverter.Deserializer <ImportProductDto>(inputXml, "Products");

            List <Product> products = new List <Product>();

            foreach (var p in usersResult)
            {
                Product product = new Product()
                {
                    Name     = p.Name,
                    Price    = (decimal)p.Price,
                    BuyerId  = p.BuyerId,
                    SellerId = p.SellerId
                };

                products.Add(product);
            }

            context.AddRange(products);
            context.SaveChanges();

            return($"Successfully imported {products.Count}");
        }
コード例 #22
0
        public static string ImportProjects(TeisterMaskContext context, string xmlString)
        {
            var rootAttributeName = "Projects";

            var projectsDTO = XMLConverter.Deserializer <ProjectDTO>(xmlString, rootAttributeName);

            var projects = new List <Project>();

            var result = string.Empty;

            foreach (var projectDTO in projectsDTO)
            {
                if (!IsValid(projectDTO))
                {
                    result += ErrorMessage + Environment.NewLine;
                    continue;
                }

                DateTime dateOpenProject;
                var      validOpenDateProject = DateTime.TryParseExact(projectDTO.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateOpenProject);

                if (!validOpenDateProject)
                {
                    result += ErrorMessage + Environment.NewLine;
                    continue;
                }

                DateTime dateDueProject;
                var      validDueDateProject = DateTime.TryParseExact(projectDTO.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateDueProject);

                if (!validDueDateProject)
                {
                    result += ErrorMessage + Environment.NewLine;
                    continue;
                }

                var tasks = new List <Task>();

                foreach (var taskDTO in projectDTO.Tasks)
                {
                    if (!IsValid(taskDTO))
                    {
                        result += ErrorMessage + Environment.NewLine;
                        continue;
                    }

                    DateTime dateOpenTask;
                    var      validOpenDateTask = DateTime.TryParseExact(projectDTO.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateOpenTask);

                    if (!validOpenDateTask)
                    {
                        result += ErrorMessage + Environment.NewLine;
                        continue;
                    }
                    DateTime dateDueTask;
                    var      validDueDateTask = DateTime.TryParseExact(projectDTO.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateDueTask);

                    if (!validDueDateTask)
                    {
                        result += ErrorMessage + Environment.NewLine;
                        continue;
                    }

                    if (dateOpenTask < dateOpenProject || dateDueTask > dateDueProject)
                    {
                        result += ErrorMessage + Environment.NewLine;
                        continue;
                    }

                    var task = new Task
                    {
                        Name          = taskDTO.Name,
                        OpenDate      = dateOpenTask,
                        DueDate       = dateDueTask,
                        ExecutionType = (ExecutionType)taskDTO.ExecutionType,
                        LabelType     = (LabelType)taskDTO.LabelType
                    };

                    tasks.Add(task);
                }

                projects.Add(new Project
                {
                    Name     = projectDTO.Name,
                    OpenDate = dateOpenProject,
                    DueDate  = dateDueProject,
                    Tasks    = tasks
                });

                result += string.Format(SuccessfullyImportedProject, projectDTO.Name, projectDTO.Tasks.Count) + Environment.NewLine;
            }
            ;

            context.Projects.AddRange(projects);

            context.SaveChanges();

            return(result.Trim());
        }
コード例 #23
0
        public static string ImportProjects(TeisterMaskContext context, string xmlString)
        {
            var sb = new StringBuilder();

            const string rootElement = "Projects";

            var projectsResultDto = XMLConverter.Deserializer <ImportProjectsDto>(xmlString, rootElement);

            var projectList = new List <Project>();

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

                var projectOpenDate = DateTime.ParseExact(projectDto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

                var projectDueDate = !projectDto.DueDate.IsNullOrEmpty() ? DateTime.ParseExact(projectDto.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null;

                var project = new Project
                {
                    Name     = projectDto.Name,
                    OpenDate = projectOpenDate,
                    DueDate  = projectDueDate,
                };

                foreach (var taskDto in projectDto.Task)
                {
                    var taskOpenDate = DateTime.ParseExact(taskDto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

                    var taskDueDate = DateTime.ParseExact(taskDto.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

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

                    if (taskOpenDate < projectOpenDate)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    if (projectDueDate.HasValue)
                    {
                        if (taskDueDate > projectDueDate.Value)
                        {
                            sb.AppendLine(ErrorMessage);
                            continue;
                        }
                    }

                    var task = new Task
                    {
                        Name          = taskDto.Name,
                        OpenDate      = taskOpenDate,
                        DueDate       = taskDueDate,
                        ExecutionType = (ExecutionType)taskDto.ExecutionType,
                        LabelType     = (LabelType)taskDto.ExecutionType
                    };
                    project.Tasks.Add(task);
                }

                projectList.Add(project);
                sb.AppendLine(string.Format(SuccessfullyImportedProject, project.Name, project.Tasks.Count));
            }

            context.Projects.AddRange(projectList);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }