public static string ImportPurchases(VaporStoreDbContext context, string xmlString) { var sb = new StringBuilder(); var rootName = "Purchases"; var dtos = XMLCustomSerializer.Deserialize <PurchaseImportDTO[]>(xmlString, rootName); var validModels = new List <Purchase>(); foreach (var dto in dtos) { if (!IsValid(dto)) { sb.AppendLine("Invalid Data"); continue; } var purchase = new Purchase { Type = (PurchaseType)Enum.Parse(typeof(PurchaseType), dto.Type), ProductKey = dto.ProductKey, Date = DateTime.ParseExact(dto.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture), Card = context.Cards.FirstOrDefault(c => c.Number == dto.Card), Game = context.Games.FirstOrDefault(g => g.Name == dto.Game) }; validModels.Add(purchase); sb.AppendLine($"Imported {purchase.Game.Name} for {purchase.Card.User.Username}"); } context.Purchases.AddRange(validModels); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string GetTotalSalesByCustomer(CarDealerContext context) { InitializeAutomaper(); //****WORKS WITH EF CORE 2.1.1 ONLY AND IN JUDGE***// //var customerDTO = context.Customers // .Where(c => c.Sales.Count > 0) // .ProjectTo<CustomerOutputDTO>(mapper.ConfigurationProvider) // .OrderByDescending(dto => dto.SpentMoney) // .ToArray(); var customerDTO = context.Customers .Include(c => c.Sales) .ThenInclude(s => s.Car) .ThenInclude(s => s.PartCars) .ThenInclude(pc => pc.Part) .ToArray() .Where(c => c.Sales.Count > 0) .AsQueryable() .ProjectTo <CustomerOutputDTO>(mapper.ConfigurationProvider) .OrderByDescending(c => c.SpentMoney) .ToArray(); var rootName = "customers"; return(XMLCustomSerializer.Serialize(customerDTO, rootName)); }
public static string ExportProjectWithTheirTasks(TeisterMaskContext context) { var rootName = "Projects"; var dtos = context.Projects .Include(p => p.Tasks) .ToList() .Where(p => p.Tasks.Any()) .Select(p => new ExportProjectsWithTasksDTO { TasksCount = p.Tasks.Count, ProjectName = p.Name, HasEndDate = p.DueDate == null ? "No" : "Yes", Tasks = p.Tasks.Select(t => new ExportTaskPartialDTO { Name = t.Name, Label = t.LabelType.ToString() }) .OrderBy(t => t.Name) .ToList() }) .OrderByDescending(p => p.TasksCount) .ThenBy(p => p.ProjectName) .ToList(); return(XMLCustomSerializer.Serialize(dtos, rootName)); }
public static string ImportBooks(BookShopContext context, string xmlString) { var sb = new StringBuilder(); var dtos = XMLCustomSerializer.Deserialize <ImportBooks[]>(xmlString, "Books"); var validModels = new List <Book>(); foreach (var dto in dtos) { if (!IsValid(dto)) { sb.AppendLine("Invalid data!"); continue; } var book = new Book { Name = dto.Name, Genre = (Genre)Enum.Parse(typeof(Genre), dto.Genre), Price = dto.Price, Pages = dto.Pages, PublishedOn = DateTime.ParseExact(dto.PublishedOn, "MM/dd/yyyy", CultureInfo.InvariantCulture) }; validModels.Add(book); sb.AppendLine(string.Format(SuccessfullyImportedBook, book.Name, book.Price)); } context.AddRange(validModels); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string GetSalesWithAppliedDiscount(CarDealerContext context) { InitializeAutomaper(); var salesDTO = context.Sales .ProjectTo <SaleOutputDTO>(mapper.ConfigurationProvider) .ToArray(); var rootName = "sales"; return(XMLCustomSerializer.Serialize(salesDTO, rootName)); }
public static string GetLocalSuppliers(CarDealerContext context) { InitializeAutomaper(); var suppliersDTO = context.Suppliers .Where(s => !s.IsImporter) .ProjectTo <LocalSupplierDTO>(mapper.ConfigurationProvider) .ToArray(); var rootName = "suppliers"; return(XMLCustomSerializer.Serialize(suppliersDTO, rootName)); }
public static string GetCategoriesByProductsCount(ProductShopContext context) { InitializeMapper(); var categoriesDto = context.Categories .ProjectTo <CategoriesByProductCountDTO>(mapper.ConfigurationProvider) .OrderByDescending(c => c.Count) .ThenBy(c => c.TotalRevenue) .ToArray(); var rootName = "Categories"; return(XMLCustomSerializer.Serialize(categoriesDto, rootName)); }
public static string ExportPrisonersInbox(SoftJailDbContext context, string prisonersNames) { var names = prisonersNames.Split(",").ToArray(); var dtos = context.Prisoners .Where(p => names.Contains(p.FullName)) .OrderBy(p => p.FullName) .ThenBy(p => p.Id) .ProjectTo <InboxForPrisonerDTO>(mapper.ConfigurationProvider) .ToArray(); var rootName = "Prisoners"; return(XMLCustomSerializer.Serialize(dtos, rootName)); }
public static string ImportCategories(ProductShopContext context, string inputXml) { InitializeMapper(); string rootElement = "Categories"; var categoriesDto = XMLCustomSerializer.Deserialize(inputXml, rootElement, new CategoryDTO()); var categories = mapper.Map <IEnumerable <Category> >(categoriesDto).Where(c => c.Name != null); context.Categories.AddRange(categories); context.SaveChanges(); return($"Successfully imported {categories.Count()}"); }
public static string ImportProducts(ProductShopContext context, string inputXml) { InitializeMapper(); string rootElement = "Products"; var productsDto = XMLCustomSerializer.Deserialize(inputXml, rootElement, new ProductDTO()); var products = mapper.Map <IEnumerable <Product> >(productsDto); context.Products.AddRange(products); context.SaveChanges(); return($"Successfully imported {products.Count()}"); }
public static string ImportUsers(ProductShopContext context, string inputXml) { InitializeMapper(); string rootElement = "Users"; var usersDto = XMLCustomSerializer.Deserialize(inputXml, rootElement, new UserDTO()); var users = mapper.Map <IEnumerable <User> >(usersDto); context.Users.AddRange(users); context.SaveChanges(); return($"Successfully imported {users.Count()}"); }
public static string ImportSuppliers(CarDealerContext context, string inputXml) { InitializeAutomaper(); var rootName = "Suppliers"; var suppliersDTO = XMLCustomSerializer.Deserialize <SupplierDTO[]>(inputXml, rootName); var suppliers = mapper.Map <Supplier[]>(suppliersDTO); context.Suppliers.AddRange(suppliers); context.SaveChanges(); return($"Successfully imported {suppliers.Length}"); }
public static string GetProductsInRange(ProductShopContext context) { InitializeMapper(); var productsDTO = context.Products .Where(p => p.Price >= 500 && p.Price <= 1000) .OrderBy(p => p.Price) .Take(10) .ProjectTo <ProductInRangeDTO>(mapper.ConfigurationProvider) .ToArray(); var rootName = "Products"; return(XMLCustomSerializer.Serialize(productsDTO, rootName)); }
public static string GetCarsFromMakeBmw(CarDealerContext context) { InitializeAutomaper(); var carsDTO = context.Cars .Where(c => c.Make == "BMW") .OrderBy(c => c.Model) .ThenByDescending(c => c.TravelledDistance) .ProjectTo <CarBmwDTO>(mapper.ConfigurationProvider) .ToArray(); var rootName = "cars"; return(XMLCustomSerializer.Serialize(carsDTO, rootName)); }
public static string GetCarsWithTheirListOfParts(CarDealerContext context) { InitializeAutomaper(); var carsDTO = context.Cars .OrderByDescending(c => c.TravelledDistance) .ThenBy(c => c.Model) .Take(5) .ProjectTo <CarWithAllPartsDTO>(mapper.ConfigurationProvider) .ToArray(); var rootName = "cars"; return(XMLCustomSerializer.Serialize(carsDTO, rootName)); }
public static string ImportParts(CarDealerContext context, string inputXml) { InitializeAutomaper(); var rootName = "Parts"; var partsDTO = XMLCustomSerializer.Deserialize <PartDTO[]>(inputXml, rootName); var parts = mapper.Map <Part[]>(partsDTO).Where(p => context.Suppliers.Any(s => s.Id == p.SupplierId) ).ToArray(); context.Parts.AddRange(parts); context.SaveChanges(); return($"Successfully imported {parts.Length}"); }
public static string GetCarsWithDistance(CarDealerContext context) { InitializeAutomaper(); var carsDTO = context.Cars .Where(c => c.TravelledDistance > 2000000) .OrderBy(c => c.Make) .ThenBy(c => c.Model) .Take(10) .ProjectTo <CarOutputDTO>(mapper.ConfigurationProvider) .ToArray(); var rootName = "cars"; return(XMLCustomSerializer.Serialize(carsDTO, rootName)); }
public static string GetSoldProducts(ProductShopContext context) { InitializeMapper(); var usersDto = context.Users .Where(u => u.ProductsSold.Count > 0) .OrderBy(u => u.LastName) .ThenBy(u => u.FirstName) .Take(5) .ProjectTo <UserSoldProductDTO>(mapper.ConfigurationProvider) .ToArray(); var rootName = "Users"; return(XMLCustomSerializer.Serialize(usersDto, rootName)); }
public static string ImportSales(CarDealerContext context, string inputXml) { InitializeAutomaper(); var rootName = "Sales"; var salesDTO = XMLCustomSerializer.Deserialize <SaleDTO[]>(inputXml, rootName); var carsIds = context.Cars.Select(c => c.Id).ToList(); var sales = mapper.Map <Sale[]>(salesDTO).Where(c => carsIds.Contains(c.CarId)).ToArray(); context.Sales.AddRange(sales); context.SaveChanges(); return($"Successfully imported {sales.Length}"); }
public static string ExportOldestBooks(BookShopContext context, DateTime date) { var dtos = context.Books .Where(b => b.PublishedOn < date && b.Genre == Genre.Science) .Select(b => new OldestBooksDTO { Pages = b.Pages, Name = b.Name, Date = b.PublishedOn.ToString("d", CultureInfo.InvariantCulture) }) .OrderByDescending(b => b.Pages) .ThenByDescending(b => b.Date) .Take(10) .ToList(); var rootName = "Books"; return(XMLCustomSerializer.Serialize(dtos, rootName)); }
public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType) { var dtos = context.Users .Include(u => u.Cards) .ThenInclude(c => c.Purchases) .ThenInclude(p => p.Game) .ToList() .Where(u => u.Cards.Any(c => c.Purchases.Any())) .Select(u => new UserPurchasesByTypeExportDTO { Username = u.Username, Purchases = u.Cards.SelectMany(c => c.Purchases) .Where(p => p.Type.ToString() == storeType && p != null) .Select(p => new PurchaseExportDTO { CardNumber = p.Card.Number, Cvc = p.Card.Cvc, Date = p.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), Game = new GameXmlExportDTO { Title = p.Game.Name, GenreType = p.Game.Genre.Name, Price = p.Game.Price } }) .OrderBy(p => p.Date) .ToList(), TotalSpent = u.Cards.Sum(c => c.Purchases .Where(p => p.Type.ToString() == storeType) .Sum(p => p.Game.Price)) }) .Where(p => p.Purchases.Any()) .OrderByDescending(u => u.TotalSpent) .ThenBy(u => u.Username) .ToList(); var rootName = "Users"; return(XMLCustomSerializer.Serialize(dtos, rootName)); }
public static string ImportOfficersPrisoners(SoftJailDbContext context, string xmlString) { mapper = Automapper.InitializeAutomaper(); var sb = new StringBuilder(); var rootName = "Officers"; var officersPrisonersDTO = XMLCustomSerializer.Deserialize <OfficerPrisonerInputDTO[]>(xmlString, rootName); var validModels = new List <Officer>(); foreach (var officer in officersPrisonersDTO) { var isValidWeapon = Enum.TryParse(officer.Weapon, out Weapon weapon); var isValidPosition = Enum.TryParse(officer.Position, out Position position); if (!IsValid(officer) || !isValidPosition || !isValidWeapon) { sb.AppendLine("Invalid Data"); continue; } var curOfficer = new Officer { FullName = officer.FullName, Salary = officer.Salary, Position = position, Weapon = weapon, DepartmentId = officer.DepartmentId, OfficerPrisoners = officer.Prisoners.Select(p => new OfficerPrisoner { PrisonerId = p.Id }).ToList() }; validModels.Add(curOfficer); sb.AppendLine($"Imported {curOfficer.FullName} ({curOfficer.OfficerPrisoners.Count} prisoners)"); } context.Officers.AddRange(validModels); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportCategoryProducts(ProductShopContext context, string inputXml) { InitializeMapper(); string rootElement = "CategoryProducts"; var exsitingCategories = context.Categories .Select(c => c.Id) .ToList(); var exsitingProducts = context.Products .Select(p => p.Id) .ToList(); var catsProductsDTO = XMLCustomSerializer.Deserialize(inputXml, rootElement, new CategoryProductDTO()) .Where(cp => exsitingCategories.Contains(cp.CategoryId) && exsitingProducts.Contains(cp.ProductId)); var categoriesProducts = mapper.Map <IEnumerable <CategoryProduct> >(catsProductsDTO); context.CategoryProducts.AddRange(categoriesProducts); context.SaveChanges(); return($"Successfully imported {categoriesProducts.Count()}"); }
public static string GetUsersWithProducts(ProductShopContext context) { InitializeMapper(); var usersDto = context.Users .Where(u => u.ProductsSold.Any(p => p.BuyerId != null)) .OrderByDescending(u => u.ProductsSold.Count) .Take(10) .Include(u => u.ProductsSold) .ToArray() .AsQueryable() .ProjectTo <UserSoldProductsExtendDTO>(mapper.ConfigurationProvider) .ToArray(); var rootName = "Users"; var users = new UserExportDTO { Count = context.Users .Where(u => u.ProductsSold.Any(p => p.BuyerId != null)).Count(), Users = usersDto }; return(XMLCustomSerializer.Serialize(users, rootName)); }
public static string ImportCars(CarDealerContext context, string inputXml) { var rootName = "Cars"; var carsDTO = XMLCustomSerializer.Deserialize <CarInputDTO[]>(inputXml, rootName); var allParts = context.Parts.Select(p => p.Id).ToList(); var cars = new List <Car>(); foreach (var dto in carsDTO) { var uniqueParts = dto.CarPartsIds.Select(p => p.PartId).Distinct(); var currCar = new Car() { Model = dto.Model, Make = dto.Make, TravelledDistance = dto.TravelledDistance }; foreach (var dtoCarPartId in uniqueParts) { if (allParts.Contains(dtoCarPartId)) { currCar.PartCars.Add(new PartCar { PartId = dtoCarPartId }); } } cars.Add(currCar); } context.Cars.AddRange(cars); context.SaveChanges(); return($"Successfully imported {cars.Count}"); }
public static string ImportProjects(TeisterMaskContext context, string xmlString) { var sb = new StringBuilder(); var rootName = "Projects"; var dtos = XMLCustomSerializer.Deserialize <ProjectImportDTO[]>(xmlString, rootName); var validModels = new List <Project>(); foreach (var dto in dtos) { if (!IsValid(dto)) { sb.AppendLine(string.Format(ErrorMessage)); continue; } var projectOpenDate = DateTime.ParseExact(dto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); var isDate = DateTime.TryParseExact(dto.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime projectDueDate); var validTasks = new List <Task>(); foreach (var task in dto.Tasks) { if (!IsValid(task)) { sb.AppendLine(string.Format(ErrorMessage)); continue; } var taskOpenDate = DateTime.ParseExact(task.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); var taskDueDate = DateTime.ParseExact(task.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); if (projectOpenDate > taskOpenDate || (isDate && projectDueDate < taskDueDate)) { sb.AppendLine(string.Format(ErrorMessage)); continue; } var curTask = new Task { Name = task.Name, OpenDate = DateTime.ParseExact(task.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), DueDate = DateTime.ParseExact(task.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), ExecutionType = (ExecutionType)task.ExecutionType, LabelType = (LabelType)task.LabelType }; validTasks.Add(curTask); } var project = new Project { Name = dto.Name, OpenDate = projectOpenDate, DueDate = (DateTime?)projectDueDate, Tasks = validTasks }; validModels.Add(project); sb.AppendLine(string.Format(SuccessfullyImportedProject, project.Name, project.Tasks.Count)); } context.AddRange(validModels); context.SaveChanges(); return(sb.ToString().Trim()); }