public void FinishOrder(OrderBindingModel model) { Order o = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); if (o == null) { throw new Exception("Элемент не найден"); } var ms = new Dictionary <Material, int>(); if (!o.Reserved) { ms = totalMaterials(o); foreach (var m in ms) { if (context.Materials.First(e => e.Id == m.Key.Id).Count < m.Value) { throw new Exception("Не достаточно матералов на складе"); } } } o.Status = OrderStatus.Готов; if (!o.Reserved) { decMaterials(ms); } context.SaveChanges(); }
public void CreateManufacturersInUsa(IEnumerable <string> names) { var manufacturers = names .Select(name => new Manufacturer() { Name = name, Country = "USA" }); _context.Manufacturers.AddRange(manufacturers); _context.SaveChanges(); }
public void AddElement(MaterialBindingModel model) { Material element = context.Materials.FirstOrDefault(rec => rec.Name == model.Name); if (element != null) { throw new Exception("Уже есть материал с таким названием"); } context.Materials.Add(new Material { Name = model.Name, Count = model.Count }); context.SaveChanges(); }
public void AddElement(ClientBindingModel model) { Client element = context.Clients.FirstOrDefault(rec => rec.Name == model.Name && rec.Email == model.Email); if (element != null) { throw new Exception("Уже есть клиент с таким ФИО или почтой"); } context.Clients.Add(new Client { Name = model.Name, Email = model.Email }); context.SaveChanges(); }
public void Create_WhenManufactuerWithTwoProducts_AddsEntriesToDb() { var manufacturer = new Manufacturer { Country = "Lithuania", Name = "Toy Lasers", Products = new List <Product> { new() { Name = "Laser S", Price = 4.01m }, new() { Name = "Laser M", Price = 7.99m } } }; _manufacturersRepository.Create(manufacturer); var toyLasersManufacturer = _db.Manufacturers .Include(m => m.Products) .Where(m => m.Name == "Toy Lasers"); Assert.AreEqual(2, toyLasersManufacturer.First().Products.Count); _db.Manufacturers.RemoveRange(toyLasersManufacturer); _db.SaveChanges(); }
private static void ImportXMLToSqlServer() { Console.WriteLine("Importing Data from Xml to Sql Server..."); var db = new FactoryDbContext(); var collection = FactoryXmlImporter.ImportSpaceships(Constants.XmlDataToImport); foreach (var spaceship in collection) { var sp = new SpaceshipMission { SpaceshipName = spaceship.SpaceshipName, Captain = spaceship.Captain, HomePlanet = spaceship.HomePlanet, NumberOfCrewMembers = spaceship.NumberOfCrewMembers, MissionType = spaceship.MissionType, Commision = spaceship.Commission, MissionStatus = spaceship.MissionStatus }; db.SpaceshipMissions.Add(sp); db.SaveChanges(); db = new FactoryDbContext(); } db.SaveChanges(); }
public void CreateOrder(OrderBindingModel model) { foreach (var m in totalMaterials(model)) { if (context.Materials.First(e => e.Id == m.Key.Id).Count < m.Value && model.Reserved) { throw new Exception("Не достаточно матералов на складе"); } } var o = context.Orders.Add(new Order { ClientId = model.ClientId, DateCreate = DateTime.Now, Reserved = model.Reserved, Sum = model.Sum, Status = OrderStatus.Принят }); context.SaveChanges(); int curOrderId = o.Id; foreach (var op in model.OrderProducts) { context.OrderProducts.Add(new OrderProduct { OrderId = curOrderId, ProductId = op.ProductId, Count = op.Count }); } context.SaveChanges(); if (o.Reserved) { decMaterials(curOrderId); } }
public void AddElement(ProductBindingModel model) { using (var transaction = context.Database.BeginTransaction()) { try { Product element = context.Products.FirstOrDefault(rec => rec.Name == model.Name); if (element != null) { throw new Exception("Уже есть изделие с таким названием"); } element = context.Products.Add(new Product { Name = model.Name, Price = model.Price }); context.SaveChanges(); // убираем дубли по компонентам var groupMaterials = model.ProductMaterials .GroupBy(rec => rec.MaterialId) .Select(rec => new { MaterialId = rec.Key, Count = rec.Sum(r => r.Count) }); // добавляем компоненты foreach (var groupMaterial in groupMaterials) { context.ProductMaterials.Add(new ProductMaterial { ProductId = element.Id, MaterialId = groupMaterial.MaterialId, Count = groupMaterial.Count }); context.SaveChanges(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } }
public void createMaterialRequest(ReportBindingModel model) { var materials = new Dictionary <Material, int>(); foreach (var o in context.Orders.Where(o => o.Status == OrderStatus.Принят)) { foreach (var op in context.OrderProducts.Where(x => x.OrderId == o.Id)) { var p = context.Products.FirstOrDefault(x => x.Id == op.ProductId); foreach (var pm in context.ProductMaterials.Where(pm => pm.ProductId == p.Id)) { var m = context.Materials.FirstOrDefault(x => x.Id == pm.MaterialId); if (!materials.ContainsKey(m)) { materials.Add(m, 0); } materials[m] += pm.Count * op.Count; } } } foreach (var m in materials.Keys.ToArray()) { if (materials[m] > context.Materials.First(rec => rec.Id == m.Id).Count) { materials[m] = materials[m] - context.Materials.First(rec => rec.Id == m.Id).Count; } else { materials.Remove(m); } } if (materials.Count == 0) { return; } if (File.Exists(model.FileName)) { File.Delete(model.FileName); } var winword = new Microsoft.Office.Interop.Word.Application(); try { object missing = System.Reflection.Missing.Value; //создаем документ Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing); //получаем ссылку на параграф var paragraph = document.Paragraphs.Add(missing); var range = paragraph.Range; //задаем текст range.Text = "Заявка на материалы"; //задаем настройки шрифта var font = range.Font; font.Size = 16; font.Name = "Times New Roman"; font.Bold = 1; //задаем настройки абзаца var paragraphFormat = range.ParagraphFormat; paragraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; paragraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceSingle; paragraphFormat.SpaceAfter = 10; paragraphFormat.SpaceBefore = 0; //добавляем абзац в документ range.InsertParagraphAfter(); //создаем таблицу var paragraphTable = document.Paragraphs.Add(Type.Missing); var rangeTable = paragraphTable.Range; var table = document.Tables.Add(rangeTable, materials.Count, 2, ref missing, ref missing); font = table.Range.Font; font.Size = 14; font.Name = "Times New Roman"; var paragraphTableFormat = table.Range.ParagraphFormat; paragraphTableFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceSingle; paragraphTableFormat.SpaceAfter = 0; paragraphTableFormat.SpaceBefore = 0; var orderedMaterials = materials.ToList().OrderBy(kv => kv.Key.Name); for (int i = 0; i < materials.Count; ++i) { table.Cell(i + 1, 1).Range.Text = orderedMaterials.ElementAt(i).Key.Name; table.Cell(i + 1, 2).Range.Text = orderedMaterials.ElementAt(i).Value.ToString(); } //задаем границы таблицы table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleInset; table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle; paragraph = document.Paragraphs.Add(missing); range = paragraph.Range; range.Text = "Дата: " + DateTime.Now.ToLongDateString(); font = range.Font; font.Size = 12; font.Name = "Times New Roman"; paragraphFormat = range.ParagraphFormat; paragraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; paragraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceSingle; paragraphFormat.SpaceAfter = 10; paragraphFormat.SpaceBefore = 10; range.InsertParagraphAfter(); //сохраняем object fileFormat = WdSaveFormat.wdFormatXMLDocument; document.SaveAs(model.FileName, ref fileFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); document.Close(ref missing, ref missing, ref missing); } catch (Exception) { throw; } finally { winword.Quit(); } SendEmail(model.Email, "Заявка на материалы", "", model.FileName); foreach (var m in materials) { context.Materials.First(x => x.Id == m.Key.Id).Count += m.Value; context.Requests.Add(new Request { MaterialId = m.Key.Id, Count = m.Value, ImplementDate = DateTime.Now }); } context.SaveChanges(); }
public ICollection <Spaceship> GetProductData(ICollection <SpaceshipMap> maps, FactoryDbContext context) { var categories = this.GetCategories(maps); context.Categories.AddRange(categories); context.SaveChanges(); var countries = this.GetCountries(maps); context.Countries.AddRange(countries); context.SaveChanges(); var cities = this.GetCities(maps, context.Countries); context.Cities.AddRange(cities); context.SaveChanges(); var partTypes = this.GetPartTypes(maps); context.PartTypes.AddRange(partTypes); context.SaveChanges(); var suppliers = this.GetSuppliers(maps, context.Cities); context.Suppliers.AddRange(suppliers); context.SaveChanges(); var parts = this.GetParts(maps, context.Suppliers, context.PartTypes).ToList(); context.Parts.AddRange(parts); context.SaveChanges(); var products = new Collection <Spaceship>(); foreach (var spaceshipMap in maps) { var existingParts = spaceshipMap.Parts.Select(x => new { Supplier = x.Supplier.Name, x.Name, Part = x.PartType.Name, Price = (decimal)x.Price, x.Quantity }); var spaceshipParts = context.Parts .ToList() .Select(x => new { Supplier = x.Supplier.Name, x.Name, Part = x.PartType.Name, x.Price, x.Quantity }) .Where(x => existingParts.Contains(x)) .Select(x => x.Name); var currentShipParts = context.Parts.Where(x => spaceshipParts.Contains(x.Name)).ToList(); var spaceship = new Spaceship { Category = context.Categories.FirstOrDefault(x => x.Name.Equals(spaceshipMap.Category)), Color = spaceshipMap.Color, Model = spaceshipMap.Model, Price = (decimal)spaceshipMap.Price, Year = spaceshipMap.Year, Parts = currentShipParts }; products.Add(spaceship); } return(products); }
private static void PopulateSqlDbWithReports(IEnumerable <Report> reportsForSql, FactoryDbContext context) { Console.WriteLine("Populating Sql Database with Reports..."); context.Reports.AddRange(reportsForSql); context.SaveChanges(); }
private static void PopulateSQLDbWithProducts(IEnumerable <Spaceship> productData, FactoryDbContext context) { Console.WriteLine("Populating Sql Database with Models..."); context.Spaceships.AddRange(productData); context.SaveChanges(); }
public void Create(Product product) { db.Products.Add(product); db.SaveChanges(); }
public void Create(Manufacturer manufacturer) { db.Manufacturers.Add(manufacturer); db.SaveChanges(); }