protected override List <T> GetList <T>()
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.Set <T>().ToList());
     }
 }
Beispiel #2
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;
             }
         }
     }
 }
Beispiel #3
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();
     }
 }
Beispiel #4
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();
     }
 }
Beispiel #5
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();
     }
 }
 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();
     }
 }
Beispiel #7
0
 public List <ClientViewModel> Read(ClientBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         var result = context.Clients
                      .Where(rec => model == null || rec.Email.Equals(model.Email) && rec.Password.Equals(model.Password) ||
                             rec.Id == model.Id)
                      .Select(rec => new ClientViewModel
         {
             Id        = rec.Id,
             ClientFIO = rec.ClientFIO,
             Email     = rec.Email,
             Password  = rec.Password
         })
                      .ToList();
         if (result.Count() == 0)
         {
             return(null);
         }
         else
         {
             return(result);
         }
     }
 }
Beispiel #8
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();
     }
 }
 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;
             }
         }
     }
 }
Beispiel #10
0
 public List <OrderViewModel> Read(OrderBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.Orders
                .Where(rec => model == null || model.Id.HasValue && rec.Id == model.Id.Value ||
                       model.DateFrom.HasValue && model.DateTo.HasValue &&
                       rec.DateCreate >= model.DateFrom.Value && rec.DateCreate <= model.DateTo.Value ||
                       model.ClientId.HasValue && rec.ClientId == model.ClientId ||
                       model.FreeOrders.HasValue && model.FreeOrders.Value && !rec.ImplementerId.HasValue ||
                       model.ImplementerId.HasValue && rec.ImplementerId == model.ImplementerId.Value && rec.Status == OrderStatus.Выполняется)
                .Include(rec => rec.Product)
                .Include(rec => rec.Client)
                .Include(rec => rec.Implementer)
                .Select(rec => new OrderViewModel
         {
             Id = rec.Id,
             ClientId = rec.ClientId,
             ClientFIO = rec.Client.ClientFIO,
             Count = rec.Count,
             Sum = rec.Sum,
             Status = rec.Status,
             ProductId = rec.ProductId,
             ProductName = rec.Product.ProductName,
             DateCreate = rec.DateCreate,
             DateImplement = rec.DateImplement,
             ImplementerFIO = rec.ImplementerId.HasValue ? rec.Implementer.ImplementerFIO : string.Empty,
             ImplementerId = rec.ImplementerId
         })
                .ToList());
     }
 }
 protected override List <PropertyInfo> GetFullList()
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         Type type = context.GetType();
         return(type.GetProperties().Where(x => x.PropertyType.FullName.StartsWith("Microsoft.EntityFrameworkCore.DbSet")).ToList());
     }
 }
Beispiel #12
0
 public List <IngredientViewModel> Read(IngredientBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.Ingredients
                .Where(rec => model == null || rec.Id == model.Id)
                .Select(rec => new IngredientViewModel
         {
             Id = rec.Id,
             IngredientName = rec.IngredientName
         })
                .ToList());
     }
 }
 public bool IsAdmin(ClientBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         Client element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
         if (element != null)
         {
             return(element.IsAdmin);
         }
         else
         {
             throw new Exception("Сотрудник не найден");
         }
     }
 }
Beispiel #14
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("Элемент не найден");
         }
     }
 }
 public List <ClientViewModel> ReadAdmins()
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.Clients
                .Where(rec => rec.IsAdmin)
                .Select(rec => new ClientViewModel
         {
             Id = rec.Id,
             ClientFIO = rec.ClientFIO,
             Email = rec.Email,
             Password = rec.Password
         })
                .ToList());
     }
 }
Beispiel #16
0
 public List <ProductViewModel> Read(ProductBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.Products
                .Where(rec => model == null || rec.Id == model.Id || rec.ProductName.Equals(model.ProductName))
                .Select(rec => new ProductViewModel
         {
             Id = rec.Id,
             ProductName = rec.ProductName,
             FillWeight = rec.FillWeight,
             Price = rec.Price
         })
                .ToList());
     }
 }
Beispiel #17
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("Продукт не найден");
         }
     }
 }
Beispiel #18
0
 public List <ImplementerViewModel> Read(ImplementerBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.Implementers
                .Where(rec => model == null || rec.Id == model.Id)
                .Select(rec => new ImplementerViewModel
         {
             Id = rec.Id,
             ImplementerFIO = rec.ImplementerFIO,
             WorkingTime = rec.WorkingTime,
             PauseTime = rec.PauseTime
         })
                .ToList());
     }
 }
Beispiel #19
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 List <MessageInfoViewModel> Read(MessageInfoBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.MessageInfoes
                .Where(rec => model == null || rec.ClientId == model.ClientId)
                .Select(rec => new MessageInfoViewModel
         {
             MessageId = rec.MessageId,
             SenderName = rec.SenderName,
             DateDelivery = rec.DateDelivery,
             Subject = rec.Subject,
             Body = rec.Body
         })
                .ToList());
     }
 }
Beispiel #21
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 List <ProductAddingViewModel> Read(ProductAddingBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.AddProducts
                .Where(rec => model == null || rec.ProductId == model.ProductId)
                .Select(rec => new ProductAddingViewModel
         {
             Id = rec.Id,
             ProductId = rec.ProductId,
             DateAdding = rec.DateAdding,
             ProductName = context.Products.FirstOrDefault(recP => recP.Id == model.ProductId).ProductName,
             Weight = rec.Weight
         })
                .ToList());
     }
 }
 public List <ProductViewModel> Read(ProductBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.Products
                .Where(rec => model == null || rec.Id == model.Id)
                .ToList()
                .Select(rec => new ProductViewModel
         {
             Id = rec.Id,
             ProductName = rec.ProductName,
             Price = rec.Price,
             ProductIngredients = context.ProductIngredients
                                  .Include(recPC => recPC.Ingredient)
                                  .Where(recPC => recPC.ProductId == rec.Id)
                                  .ToDictionary(recPC => recPC.IngredientId, recPC =>
                                                (recPC.Ingredient?.IngredientName, recPC.Count))
         })
Beispiel #24
0
 public List <DishViewModel> Read(DishBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.Dishes
                .Where(rec => model == null || rec.Id == model.Id)
                .ToList()
                .Select(rec => new DishViewModel
         {
             Id = rec.Id,
             DishName = rec.DishName,
             DishType = rec.DishType,
             Price = rec.Price,
             DishProducts = context.DishProducts
                            .Include(recDP => recDP.Product)
                            .Where(recDP => recDP.DishId == rec.Id)
                            .ToDictionary(recDP => recDP.ProductId, recDP => (recDP.Product?.ProductName, recDP.Weight))
         })
 public List <RequestViewModel> Read(RequestBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.Requests
                .Where(rec => model == null || rec.Id == model.Id ||
                       rec.DateRequest >= model.DateFrom && rec.DateRequest <= model.DateTo)
                .ToList()
                .Select(rec => new RequestViewModel
         {
             Id = rec.Id,
             DateRequest = rec.DateRequest,
             RequestProducts = context.RequestProducts
                               .Where(recPR => recPR.RequestId == rec.Id)
                               .ToDictionary(recPR => recPR.ProductId, recPR =>
                                             (context.Products.First(recP => recP.Id == recPR.ProductId).ProductName,
                                              recPR.Weight))
         })
 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;
             }
         }
     }
 }
 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();
     }
 }
Beispiel #29
0
 public List <OrderViewModel> Read(OrderBindingModel model)
 {
     using (var context = new AbstractSweetShopDatabase())
     {
         return(context.Orders
                .Where(rec => model == null || rec.Id == model.Id ||
                       rec.DateCreate >= model.DateFrom && rec.DateCreate <= model.DateTo)
                .ToList()
                .Select(rec => new OrderViewModel
         {
             Id = rec.Id,
             ClientId = rec.ClientId,
             ClientFIO = context.Orders
                         .Include(recC => recC.Client)
                         .Where(recC => recC.ClientId == rec.ClientId)
                         .ToList()[0].Client.ClientFIO,
             DateCreate = rec.DateCreate,
             Status = rec.Status,
             OrderSum = rec.OrderSum,
             OrderDishes = context.OrderDishes
                           .Include(recOD => recOD.Dish)
                           .Where(recOD => recOD.OrderId == rec.Id)
                           .ToDictionary(recOD => recOD.DishId, recOD => (recOD.Dish?.DishName, recOD.DishPrice))
         })
Beispiel #30
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;
             }
         }
     }
 }