Esempio n. 1
0
 public void CreateOrUpdate(OrderBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         Order element;
         if (model.Id.HasValue)
         {
             element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
             if (element == null)
             {
                 throw new Exception("Элемент не найден");
             }
         }
         else
         {
             element = new Order();
             context.Orders.Add(element);
         }
         element.ClientId      = model.ClientId.Value;
         element.ProductId     = model.ProductId;
         element.ImplementerId = model.ImplementerId;
         element.Count         = model.Count;
         element.Sum           = model.Sum;
         element.Status        = model.Status;
         element.DateCreate    = model.DateCreate;
         element.DateImplement = model.DateImplement;
         context.SaveChanges();
     }
 }
Esempio n. 2
0
 public void CreateOrUpdate(ProductBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         Product element = context.Products.FirstOrDefault(rec => rec.ProductName == model.ProductName && rec.Id != model.Id);
         if (element != null)
         {
             throw new Exception("Уже есть продукт с таким названием");
         }
         if (model.Id.HasValue)
         {
             element = context.Products.FirstOrDefault(rec => rec.Id == model.Id);
             if (element == null)
             {
                 throw new Exception("Продукт не найден");
             }
         }
         else
         {
             element = new Product();
             context.Products.Add(element);
         }
         element.ProductName = model.ProductName;
         element.Price       = model.Price;
         if (model.FillWeight.HasValue)
         {
             element.FillWeight = model.FillWeight.Value;
         }
         context.SaveChanges();
     }
 }
Esempio n. 3
0
 public void CreateOrUpdate(IngredientBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         Ingredient element = context.Ingredients.FirstOrDefault(rec => rec.IngredientName == model.IngredientName && rec.Id != model.Id);
         if (element != null)
         {
             throw new Exception("Уже есть элемент с таким названием");
         }
         if (model.Id.HasValue)
         {
             element = context.Ingredients.FirstOrDefault(rec => rec.Id == model.Id);
             if (element == null)
             {
                 throw new Exception("Элемент не найден");
             }
         }
         else
         {
             element = new Ingredient();
             context.Ingredients.Add(element);
         }
         element.IngredientName = model.IngredientName;
         context.SaveChanges();
     }
 }
Esempio n. 4
0
 public void CreateOrUpdate(ImplementerBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         Implementer element = context.Implementers.FirstOrDefault(rec => rec.ImplementerFIO == model.ImplementerFIO && rec.Id != model.Id);
         if (element != null)
         {
             throw new Exception("Уже есть исполнитель с таким ФИО!");
         }
         if (model.Id.HasValue)
         {
             element = context.Implementers.FirstOrDefault(rec => rec.Id == model.Id);
             if (element == null)
             {
                 throw new Exception("Исполнитель не найден");
             }
         }
         else
         {
             element = new Implementer();
             context.Implementers.Add(element);
         }
         element.ImplementerFIO = model.ImplementerFIO;
         element.WorkingTime    = model.WorkingTime;
         element.PauseTime      = model.PauseTime;
         context.SaveChanges();
     }
 }
Esempio n. 5
0
 public void Delete(DishBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 context.DishProducts.RemoveRange(context.DishProducts.Where(rec => rec.DishId == model.Id));
                 Dish dish = context.Dishes.FirstOrDefault(rec => rec.Id == model.Id);
                 if (dish != null)
                 {
                     context.Dishes.Remove(dish);
                     context.SaveChanges();
                 }
                 else
                 {
                     throw new Exception("Блюдо не найдено");
                 }
                 transaction.Commit();
             }
             catch (Exception)
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
 public void CreateOrUpdate(ClientBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         Client element = context.Clients.FirstOrDefault(rec => rec.Email == model.Email && rec.Id != model.Id);
         if (element != null)
         {
             throw new Exception("Уже есть сотрудник с таким логином!");
         }
         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.IsAdmin = false;
         }
         element.ClientFIO = model.ClientFIO;
         element.Email     = model.Email;
         element.Password  = model.Password;
         context.SaveChanges();
     }
 }
 public void Delete(ProductBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 // удаяем записи по ингредиентам при удалении кондитерского изделия
                 context.ProductIngredients.RemoveRange(context.ProductIngredients.Where(rec => rec.ProductId == model.Id));
                 Product element = context.Products.FirstOrDefault(rec => rec.Id == model.Id);
                 if (element != null)
                 {
                     context.Products.Remove(element);
                     context.SaveChanges();
                 }
                 else
                 {
                     throw new Exception("Элемент не найден");
                 }
                 transaction.Commit();
             }
             catch (Exception)
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
Esempio n. 8
0
 public void CreateOrUpdate(OrderBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 Order order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
                 if (order == null)
                 {
                     order = new Order
                     {
                         ClientId   = model.ClientId.Value,
                         OrderSum   = model.OrderSum,
                         DateCreate = model.DateCreate,
                         Status     = model.Status
                     };
                     context.Orders.Add(order);
                     context.SaveChanges();
                     foreach (var od in model.OrderDishes)
                     {
                         context.OrderDishes.Add(new OrderDish
                         {
                             OrderId   = order.Id,
                             DishId    = od.Key,
                             DishPrice = od.Value.Item2
                         });
                         context.SaveChanges();
                     }
                 }
                 else
                 {
                     order.Status        = model.Status;
                     order.DateImplement = model.DateImplement;
                     context.SaveChanges();
                 }
                 transaction.Commit();
             }
             catch (Exception)
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
 public bool Create(RequestBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 Request request = new Request
                 {
                     ClientId    = model.ClientId,
                     DateRequest = model.DateRequest
                 };
                 context.Requests.Add(request);
                 context.SaveChanges();
                 Product product;
                 foreach (var rp in model.RequestProducts)
                 {
                     product = context.Products.First(rec => rec.Id == rp.Key);
                     if (product.FillWeight <= rp.Value.Item2)
                     {
                         transaction.Rollback();
                         return(false);
                     }
                     product.FillWeight -= rp.Value.Item2;
                     context.RequestProducts.Add(new RequestProduct
                     {
                         RequestId = request.Id.Value,
                         ProductId = rp.Key,
                         Weight    = rp.Value.Item2
                     });
                     context.SaveChanges();
                 }
                 transaction.Commit();
                 return(true);
             }
             catch (Exception)
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
Esempio n. 10
0
 public void Delete(OrderBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         Order element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);;
         if (element != null)
         {
             context.Orders.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Элемент не найден");
         }
     }
 }
Esempio n. 11
0
 public void Delete(ProductBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         Product element = context.Products.FirstOrDefault(rec => rec.Id == model.Id);
         if (element != null)
         {
             context.Products.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Продукт не найден");
         }
     }
 }
Esempio n. 12
0
 public void Delete(ClientBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         context.Orders.RemoveRange(context.Orders.Where(rec => rec.ClientId == model.Id));
         Client element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
         if (element != null)
         {
             context.Clients.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Клиент не найден");
         }
     }
 }
 public void Create(ProductAddingBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         Product product = context.Products.FirstOrDefault(rec => rec.Id == model.ProductId);
         if (product == null)
         {
             throw new Exception("Продукт не найден");
         }
         product.FillWeight += model.Weight;
         context.AddProducts.Add(new ProductAdding
         {
             ProductId  = model.ProductId,
             DateAdding = model.DateAdding,
             Weight     = model.Weight
         });
         context.SaveChanges();
     }
 }
 public void Create(MessageInfoBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         MessageInfo element = context.MessageInfoes.FirstOrDefault(rec => rec.MessageId == model.MessageId);
         if (element != null)
         {
             throw new Exception("Уже есть письмо с таким идентификатором");
         }
         int?clientId = context.Clients.FirstOrDefault(rec => rec.Email == model.FromMailAddress)?.Id;
         context.MessageInfoes.Add(new MessageInfo
         {
             MessageId    = model.MessageId,
             ClientId     = clientId,
             SenderName   = model.FromMailAddress,
             DateDelivery = model.DateDelivery,
             Subject      = model.Subject,
             Body         = model.Body
         });
         context.SaveChanges();
     }
 }
Esempio n. 15
0
 public void CreateOrUpdate(DishBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 Dish tempDish = context.Dishes.FirstOrDefault(rec => rec.DishName == model.DishName && rec.Id != model.Id);
                 if (tempDish != null)
                 {
                     throw new Exception("Уже есть блюдо с таким названием");
                 }
                 if (model.Id.HasValue)
                 {
                     tempDish = context.Dishes.FirstOrDefault(rec => rec.Id == model.Id);
                     if (tempDish == null)
                     {
                         throw new Exception("Блюдо не найдено");
                     }
                 }
                 else
                 {
                     tempDish = new Dish();
                     context.Dishes.Add(tempDish);
                 }
                 tempDish.DishName = model.DishName;
                 tempDish.Price    = model.Price;
                 tempDish.DishType = model.DishType;
                 context.SaveChanges();
                 if (model.Id.HasValue)
                 {
                     var dishProducts = context.DishProducts.Where(rec => rec.DishId == model.Id.Value).ToList();
                     // удалили те, которых нет в модели
                     context.DishProducts.RemoveRange(dishProducts.Where(rec => !model.DishProducts.ContainsKey(rec.ProductId)).ToList());
                     context.SaveChanges();
                     dishProducts = context.DishProducts.Where(rec => rec.DishId == model.Id.Value).ToList();
                     // обновили количество у существующих записей
                     foreach (var updateProduct in dishProducts)
                     {
                         updateProduct.Weight = model.DishProducts[updateProduct.ProductId].Item2;
                         model.DishProducts.Remove(updateProduct.ProductId);
                     }
                     context.SaveChanges();
                 }
                 // добавили новые
                 foreach (var dp in model.DishProducts)
                 {
                     context.DishProducts.Add(new DishProduct
                     {
                         DishId    = tempDish.Id,
                         ProductId = dp.Key,
                         Weight    = dp.Value.Item2
                     });
                     context.SaveChanges();
                 }
                 transaction.Commit();
             }
             catch (Exception)
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
 public void CreateOrUpdate(ProductBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 Product element = context.Products.FirstOrDefault(rec => rec.ProductName == model.ProductName && rec.Id != model.Id);
                 if (element != null)
                 {
                     throw new Exception("Уже есть кондитерское изделие с таким названием");
                 }
                 if (model.Id.HasValue)
                 {
                     element = context.Products.FirstOrDefault(rec => rec.Id == model.Id);
                     if (element == null)
                     {
                         throw new Exception("Элемент не найден");
                     }
                 }
                 else
                 {
                     element = new Product();
                     context.Products.Add(element);
                 }
                 element.ProductName = model.ProductName;
                 element.Price       = model.Price;
                 context.SaveChanges();
                 if (model.Id.HasValue)
                 {
                     var productIngredients = context.ProductIngredients.Where(rec => rec.ProductId == model.Id.Value).ToList();
                     // удалили те, которых нет в модели
                     context.ProductIngredients.RemoveRange(productIngredients.Where(rec => !model.ProductIngredients.ContainsKey(rec.IngredientId)).ToList());
                     context.SaveChanges();
                     // обновили количество у существующих записей
                     foreach (var updateIngredient in productIngredients)
                     {
                         updateIngredient.Count = model.ProductIngredients[updateIngredient.IngredientId].Item2;
                         model.ProductIngredients.Remove(updateIngredient.IngredientId);
                     }
                     context.SaveChanges();
                 }
                 // добавили новые
                 foreach (var pi in model.ProductIngredients)
                 {
                     context.ProductIngredients.Add(new ProductIngredient
                     {
                         ProductId    = element.Id,
                         IngredientId = pi.Key,
                         Count        = pi.Value.Item2
                     });
                     context.SaveChanges();
                 }
                 transaction.Commit();
             }
             catch (Exception)
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }