public void Delete(RequestBindingModel model) { using (var context = new KorytoDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { context.DetailRequests.RemoveRange(context.DetailRequests.Where(rec => rec.RequestId == model.Id)); Request element = context.Requests.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Requests.Remove(element); context.SaveChanges(); } else { throw new Exception("Элемент не найден"); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public List <OrderViewModel> Read(OrderBindingModel model) { using (var context = new KorytoDatabase()) { List <OrderViewModel> result = new List <OrderViewModel>(); if (model != null) { if ((model.DateTo != null) && (model.DateFrom != null)) { result.AddRange(context.Orders .Where(rec => (rec.Id == model.Id || rec.ClientId == model.ClientId) && (rec.DateCreate >= model.DateFrom && rec.DateCreate <= model.DateTo)) .Select(rec => CreateViewModel(rec))); } else { result.AddRange(context.Orders .Where(rec => rec.Id == model.Id || rec.ClientId == model.ClientId) .Select(rec => CreateViewModel(rec))); } } else { result.AddRange(context.Orders.Select(rec => CreateViewModel(rec))); } return(result); } }
public void CreateOrUpdate(DetailBindingModel model) { using (var context = new KorytoDatabase()) { Detail element = context.Details.FirstOrDefault(rec => rec.DetailName == model.DetailName && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть деталь с таким названием"); } if (model.Id.HasValue) { element = context.Details.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Detail(); context.Details.Add(element); } element.DetailName = model.DetailName; element.Price = model.Price; element.TotalAmount = model.TotalAmount; context.SaveChanges(); } }
public void CreateOrUpdate(ClientBindingModel model) { using (var context = new KorytoDatabase()) { Client element = model.Id.HasValue ? null : new Client(); if (model.Id.HasValue) { element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Client(); context.Clients.Add(element); } element.ClientFIO = model.ClientFIO; element.Password = model.Password; element.Mail = model.Mail == null ? element.Mail : model.Mail; element.Login = model.Login == null ? element.Login : model.Login; context.SaveChanges(); } }
public List <DetailViewModel> Read(DetailBindingModel model) { using (var context = new KorytoDatabase()) { return(context.Details .Where(rec => model == null || rec.Id == model.Id) .Select(rec => new DetailViewModel { Id = rec.Id, DetailName = rec.DetailName, Price = rec.Price, TotalAmount = rec.TotalAmount }) .ToList()); } }
public List <SpareViewModel> Read(SpareBindingModel model) { using (var context = new KorytoDatabase()) { return(context.Spares .Where(rec => model == null || rec.Id == model.Id) .Select(rec => new SpareViewModel { Id = rec.Id, SpareName = rec.SpareName, Price = rec.Price, TotalAmount = rec.TotalAmount }) .ToList()); } }
public void Delete(DetailBindingModel model) { using (var context = new KorytoDatabase()) { Detail element = context.Details.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Details.Remove(element); context.SaveChanges(); } else { throw new Exception("Элемент не найден"); } } }
public FormRaz() { InitializeComponent(); using (var context = new KorytoDatabase()) { var orders = context.Orders.Where(rec => rec.Status == Logic.Enums.OrderStatus.Ожидает_поставки_деталей).ToList(); if (orders != null) { foreach (var order in orders) { var ordercars = context.OrderCars.Where(rec => rec.OrderId == order.Id); foreach (var car in ordercars) { if (car.Count > 0) { var cardetails = context.CarDetails.Where(rec => rec.CarId == car.CarId); foreach (var det in cardetails) { var detcount = context.Details.FirstOrDefault(rec => rec.Id == det.DetailId); if ((det.Count * car.Count) > detcount.TotalAmount) { int raznica = (det.Count * car.Count) - detcount.TotalAmount; Raz.Add(new Tuple <string, int>(detcount.DetailName, raznica)); } } } } } } } dataGridView.Columns.AddRange( new DataGridViewTextBoxColumn() { Name = "clmName", HeaderText = "Деталь", DataPropertyName = "Item1" }, new DataGridViewTextBoxColumn() { Name = "clmAmount", HeaderText = "Требуемое количество", DataPropertyName = "Item2" } ); dataGridView.DataSource = Raz; // textBox1.Text = Raz[0].Item1; }
public List <RequestViewModel> Read(RequestBindingModel model) { using (var context = new KorytoDatabase()) { return(context.Requests .Where(rec => model == null || rec.Id == model.Id || (model.DateFrom.HasValue && model.DateTo.HasValue && rec.DateCreate >= model.DateFrom && rec.DateCreate <= model.DateTo)) .ToList() .Select(rec => new RequestViewModel { Id = rec.Id, RequestName = rec.RequestName, DateCreate = rec.DateCreate, DetailRequests = context.DetailRequests .Include(recPC => recPC.Detail) .Where(recPC => recPC.RequestId == rec.Id) .ToDictionary(recPC => recPC.DetailId, recPC => (recPC.Detail?.DetailName, recPC.Count)) })
public List <ClientViewModel> Read(ClientBindingModel model) { using (var context = new KorytoDatabase()) { return(context.Clients .Where(rec => model == null || (rec.Id == model.Id) || (rec.Login == model.Login || rec.Mail == model.Mail) && (model.Password == null || rec.Password == model.Password)) .Select(rec => new ClientViewModel { Id = rec.Id, ClientFIO = rec.ClientFIO, Mail = rec.Mail, Login = rec.Login, Password = rec.Password }) .ToList()); } }
public List <CarViewModel> Read(CarBindingModel model) { using (var context = new KorytoDatabase()) { return(context.Cars .Where(rec => model == null || rec.Id == model.Id) .ToList() .Select(rec => new CarViewModel { Id = rec.Id, CarName = rec.CarName, Price = rec.Price, FullPrice = rec.FullPrice, Year = rec.Year, CarDetails = context.CarDetails .Include(recPC => recPC.Detail) .Where(recPC => recPC.CarId == rec.Id) .ToDictionary(recPC => recPC.DetailId, recPC => (recPC.Detail?.DetailName, recPC.Count, recPC.Sum)) })
static private OrderViewModel CreateViewModel(Order order) { using (var context = new KorytoDatabase()) { var cars = context.OrderCars .Where(rec => rec.OrderId == order.Id) .Include(rec => rec.Car) .Select(rec => new OrderCarViewModel { Id = rec.Id, OrderId = rec.OrderId, CarId = rec.CarId, CarName = rec.Car.CarName, Count = rec.Count }).ToList(); foreach (var car in cars) { var carData = context.Cars.Where(rec => rec.Id == car.CarId).FirstOrDefault(); if (carData != null) { car.CarName = carData.CarName; car.Year = carData.Year; car.Price = carData.Price; } } return(new OrderViewModel { Id = order.Id, ClientId = order.ClientId, ClientFIO = context.Clients.Where(rec => rec.Id == order.ClientId).Select(rec => rec.ClientFIO).FirstOrDefault(), DateCreate = order.DateCreate, DateImplement = order.DateImplement, Status = order.Status, Price = order.Price, OrderCars = cars }); } }
public void CreateOrUpdate(RequestBindingModel model) { using (var context = new KorytoDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Request element = context.Requests.FirstOrDefault(rec => rec.RequestName == model.RequestName && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть заявка с таким названием"); } if (model.Id.HasValue) { element = context.Requests.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Request(); context.Requests.Add(element); } element.RequestName = model.RequestName; element.DateCreate = model.DateCreate; context.SaveChanges(); if (model.Id.HasValue) { var DetailRequests = context.DetailRequests.Where(rec => rec.RequestId == model.Id.Value).ToList(); context.DetailRequests.RemoveRange(DetailRequests.Where(rec => !model.DetailRequests.ContainsKey(rec.DetailId)).ToList()); context.SaveChanges(); foreach (var updateDetail in DetailRequests) { updateDetail.Count = model.DetailRequests[updateDetail.DetailId].Item2; model.DetailRequests.Remove(updateDetail.DetailId); } context.SaveChanges(); } foreach (var pc in model.DetailRequests) { context.DetailRequests.Add(new DetailRequest { RequestId = element.Id, DetailId = pc.Key, Count = pc.Value.Item2 }); context.SaveChanges(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public void CreateOrUpdate(CarBindingModel model) { using (var context = new KorytoDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Car element = context.Cars.FirstOrDefault(rec => rec.CarName == model.CarName && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть автомобиль с таким названием"); } if (model.Id.HasValue) { element = context.Cars.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Car(); context.Cars.Add(element); } element.CarName = model.CarName; element.Price = model.Price; element.FullPrice = model.FullPrice; element.Year = model.Year; context.SaveChanges(); if (model.Id.HasValue) { var CarDetails = context.CarDetails.Where(rec => rec.CarId == model.Id.Value).ToList(); context.CarDetails.RemoveRange(CarDetails.Where(rec => !model.CarDetails.ContainsKey(rec.DetailId)).ToList()); context.SaveChanges(); foreach (var updateDetail in CarDetails) { updateDetail.Count = model.CarDetails[updateDetail.DetailId].Item2; model.CarDetails.Remove(updateDetail.DetailId); } context.SaveChanges(); } foreach (var pc in model.CarDetails) { context.CarDetails.Add(new CarDetail { CarId = element.Id, DetailId = pc.Key, Count = pc.Value.Item2, Sum = pc.Value.Item3 }); context.SaveChanges(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public void CreateOrUpdate(OrderBindingModel model) { using (var context = new KorytoDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Order element = model.Id.HasValue ? null : new Order(); if (model.Id.HasValue) { element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } element.ClientId = model.ClientId; element.DateCreate = model.DateCreate; element.DateImplement = model.DateImplement; element.Price = model.Price; element.Status = model.Status; context.SaveChanges(); } else { element.ClientId = model.ClientId; element.DateCreate = model.DateCreate; element.DateImplement = model.DateImplement; element.Price = model.Price; element.Status = model.Status; context.Orders.Add(element); context.SaveChanges(); var groupCars = model.OrderCars .GroupBy(rec => rec.CarId) .Select(rec => new { CarId = rec.Key, Count = rec.Sum(r => r.Count) }); foreach (var groupCar in groupCars) { context.OrderCars.Add(new OrderCar { OrderId = element.Id, CarId = groupCar.CarId, Count = groupCar.Count }); context.SaveChanges(); } } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }