public void AddElement(RequestBindingModel model) { using (var transaction = context.Database.BeginTransaction()) { try { var request = new Request { DateCreate = model.DateCreate }; context.Requests.Add(request); context.SaveChanges(); var groupDetails = model.DetailRequests .GroupBy(record => record.DetailId) .Select(record => new { detailId = record.Key, amount = record.Sum(r => r.Amount) }); foreach (var gr in groupDetails) { var detailRequest = new DetailRequest { RequestId = request.Id, DetailId = gr.detailId, Amount = gr.amount }; context.DetailRequests.Add(detailRequest); context.SaveChanges(); var updateDetail = context.Details.FirstOrDefault(record => record.Id == detailRequest.DetailId); if (updateDetail == null) { continue; } updateDetail.TotalAmount += detailRequest.Amount; context.SaveChanges(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } }
public void AddElement(DetailBindingModel model) { var detail = context.Details.FirstOrDefault(record => record.DetailName == model.DetailName); if (detail != null) { throw new Exception("Уже есть деталь"); } context.Details.Add(new Detail { DetailName = model.DetailName, TotalAmount = model.TotalAmount }); context.SaveChanges(); }
public void AddElement(ClientBindingModel model) { var element = context.Clients.FirstOrDefault(rec => rec.ClientFIO == model.ClientFIO); if (element != null) { throw new Exception("Уже есть клиент с таким ФИО"); } context.Clients.Add(new Client { ClientFIO = model.ClientFIO, Login = model.Login, Mail = model.Mail, Password = model.Password }); context.SaveChanges(); }
public void AddElement(CarBindingModel model) { using (var transaction = context.Database.BeginTransaction()) { try { var car = context.Cars.FirstOrDefault(record => record.CarName == model.CarName); if (car != null) { throw new Exception("Такое авто уже существует."); } else { car = new Car { CarName = model.CarName, Price = model.Price, Year = model.Year }; } context.Cars.Add(car); context.SaveChanges(); var duplicates = model.CarDetails .GroupBy(record => record.DetailId) .Select(record => new { detailId = record.Key, amount = record.Sum(rec => rec.Amount) }); foreach (var duplicate in duplicates) { context.CarDetails.Add(new CarDetail { CarId = car.Id, DetailId = duplicate.detailId, Amount = duplicate.amount }); context.SaveChanges(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } }
public void CreateOrder(OrderBindingModel model) { using (var transaction = context.Database.BeginTransaction()) { try { var element = new Order { ClientId = model.ClientId, DateCreate = DateTime.Now, TotalSum = model.TotalSum, OrderStatus = OrderStatus.Принят }; context.Orders.Add(element); context.SaveChanges(); var groupCars = model.OrderCars .GroupBy(rec => rec.CarId) .Select(rec => new { CarId = rec.Key, Amount = rec.Sum(r => r.Amount) }); foreach (var groupCar in groupCars) { context.OrderCars.Add(new OrderCar { OrderId = element.Id, CarId = groupCar.CarId, Amount = groupCar.Amount }); context.SaveChanges(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } }