From cd13ac1206e2dbfd0101cb401ef27248767a6423 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Thu, 16 May 2024 17:13:55 +0400 Subject: [PATCH] CRUD and storages --- .../BindingModels/ChequeBindingModel.cs | 2 + .../BindingModels/CookBindingModel.cs | 2 + .../BindingModels/CustomerBindingModel.cs | 4 + .../BindingModels/DishBindingModel.cs | 2 + .../BindingModels/IngredientBindingModel.cs | 2 + .../BindingModels/PromotionBindingModel.cs | 2 + .../SearchModels/ChequeSearchModel.cs | 6 +- .../SearchModels/CookSearchModel.cs | 6 +- .../SearchModels/CustomerSearchModel.cs | 2 + .../SearchModels/DishSearchModel.cs | 2 + .../SearchModels/IngredientSearchModel.cs | 2 + .../SearchModels/PromotionSearchModel.cs | 2 + SushiBarDatabaseImplement/Models/Cheque.cs | 62 ++++++++- SushiBarDatabaseImplement/Models/Cook.cs | 25 +++- SushiBarDatabaseImplement/Models/Customer.cs | 29 ++++- SushiBarDatabaseImplement/Models/Dish.cs | 73 ++++++++++- .../Models/Ingredient.cs | 27 ++-- SushiBarDatabaseImplement/Models/Promotion.cs | 28 +++- .../Storages/ChequeStorage.cs | 120 ++++++++++++++++++ .../Storages/CookStorage.cs | 83 ++++++++++++ .../Storages/CustomerStorage.cs | 83 ++++++++++++ .../Storages/DishStorage.cs | 100 +++++++++++++++ .../Storages/IngredientStorage.cs | 70 +++++++++- .../Storages/PromotionStorage.cs | 80 ++++++++++++ 24 files changed, 790 insertions(+), 24 deletions(-) create mode 100644 SushiBarDatabaseImplement/Storages/ChequeStorage.cs create mode 100644 SushiBarDatabaseImplement/Storages/CookStorage.cs create mode 100644 SushiBarDatabaseImplement/Storages/CustomerStorage.cs create mode 100644 SushiBarDatabaseImplement/Storages/DishStorage.cs create mode 100644 SushiBarDatabaseImplement/Storages/PromotionStorage.cs diff --git a/SushiBarContracts/BindingModels/ChequeBindingModel.cs b/SushiBarContracts/BindingModels/ChequeBindingModel.cs index d3e261f..17222a1 100644 --- a/SushiBarContracts/BindingModels/ChequeBindingModel.cs +++ b/SushiBarContracts/BindingModels/ChequeBindingModel.cs @@ -2,6 +2,8 @@ { public class ChequeBindingModel { + public int Id { get; set; } + public List ChequeItems { get; set; } = new(); public int? CustomerId { get; set; } diff --git a/SushiBarContracts/BindingModels/CookBindingModel.cs b/SushiBarContracts/BindingModels/CookBindingModel.cs index 910ede1..d7ce3ca 100644 --- a/SushiBarContracts/BindingModels/CookBindingModel.cs +++ b/SushiBarContracts/BindingModels/CookBindingModel.cs @@ -2,6 +2,8 @@ { public class CookBindingModel { + public int Id { get; set; } + public string Fio { get; set; } = string.Empty; public DateTime EmploymentDate { get; set; } diff --git a/SushiBarContracts/BindingModels/CustomerBindingModel.cs b/SushiBarContracts/BindingModels/CustomerBindingModel.cs index 0a6767c..f2a3e43 100644 --- a/SushiBarContracts/BindingModels/CustomerBindingModel.cs +++ b/SushiBarContracts/BindingModels/CustomerBindingModel.cs @@ -2,8 +2,12 @@ { public class CustomerBindingModel { + public int Id { get; set; } + public string Fio { get; set; } = string.Empty; public DateTime? BirthdayDate { get; set; } + + public double SumOfAllOrders { get; set; } } } diff --git a/SushiBarContracts/BindingModels/DishBindingModel.cs b/SushiBarContracts/BindingModels/DishBindingModel.cs index 327b48a..bd7e47f 100644 --- a/SushiBarContracts/BindingModels/DishBindingModel.cs +++ b/SushiBarContracts/BindingModels/DishBindingModel.cs @@ -2,6 +2,8 @@ { public class DishBindingModel { + public int Id { get; set; } + public string DishName { get; set; } = string.Empty; public string Category { get; set;} = string.Empty; diff --git a/SushiBarContracts/BindingModels/IngredientBindingModel.cs b/SushiBarContracts/BindingModels/IngredientBindingModel.cs index 2731028..201d44f 100644 --- a/SushiBarContracts/BindingModels/IngredientBindingModel.cs +++ b/SushiBarContracts/BindingModels/IngredientBindingModel.cs @@ -2,6 +2,8 @@ { public class IngredientBindingModel { + public int Id { get; set; } + public string IngredientName { get; set; } = string.Empty; public string Unit { get; set; } = string.Empty; diff --git a/SushiBarContracts/BindingModels/PromotionBindingModel.cs b/SushiBarContracts/BindingModels/PromotionBindingModel.cs index 5beb11a..4626f9d 100644 --- a/SushiBarContracts/BindingModels/PromotionBindingModel.cs +++ b/SushiBarContracts/BindingModels/PromotionBindingModel.cs @@ -2,6 +2,8 @@ { public class PromotionBindingModel { + public int Id { get; set; } + public string PromotionName { get; set; } = string.Empty; public float Discount { get; set; } diff --git a/SushiBarContracts/SearchModels/ChequeSearchModel.cs b/SushiBarContracts/SearchModels/ChequeSearchModel.cs index 3ba968f..48146e3 100644 --- a/SushiBarContracts/SearchModels/ChequeSearchModel.cs +++ b/SushiBarContracts/SearchModels/ChequeSearchModel.cs @@ -2,8 +2,12 @@ { public class ChequeSearchModel { + public int? Id { get; set; } + public int? CustomerId { get; set; } - public DateTime? OrderDate { get; set; } + public DateTime? OrderDateFrom { get; set; } + + public DateTime? OrderDateTo { get; set; } } } diff --git a/SushiBarContracts/SearchModels/CookSearchModel.cs b/SushiBarContracts/SearchModels/CookSearchModel.cs index 5e697b4..71ca6db 100644 --- a/SushiBarContracts/SearchModels/CookSearchModel.cs +++ b/SushiBarContracts/SearchModels/CookSearchModel.cs @@ -2,6 +2,10 @@ { public class CookSearchModel { - public DateTime? EmploymentDate { get; set; } + public int? Id { get; set; } + + public DateTime? EmploymentDateFrom { get; set; } + + public DateTime? EmploymentDateTo { get; set; } } } diff --git a/SushiBarContracts/SearchModels/CustomerSearchModel.cs b/SushiBarContracts/SearchModels/CustomerSearchModel.cs index 7a059cd..95add85 100644 --- a/SushiBarContracts/SearchModels/CustomerSearchModel.cs +++ b/SushiBarContracts/SearchModels/CustomerSearchModel.cs @@ -2,6 +2,8 @@ { public class CustomerSearchModel { + public int? Id { get; set; } + public double? SumOfAllOrders { get; set; } } } diff --git a/SushiBarContracts/SearchModels/DishSearchModel.cs b/SushiBarContracts/SearchModels/DishSearchModel.cs index 15f4d1b..1c44bee 100644 --- a/SushiBarContracts/SearchModels/DishSearchModel.cs +++ b/SushiBarContracts/SearchModels/DishSearchModel.cs @@ -2,6 +2,8 @@ { public class DishSearchModel { + public int? Id { get; set; } + public string? DishName { get; set; } public string? Category { get; set; } diff --git a/SushiBarContracts/SearchModels/IngredientSearchModel.cs b/SushiBarContracts/SearchModels/IngredientSearchModel.cs index 8e1faa6..dccc3d0 100644 --- a/SushiBarContracts/SearchModels/IngredientSearchModel.cs +++ b/SushiBarContracts/SearchModels/IngredientSearchModel.cs @@ -2,6 +2,8 @@ { public class IngredientSearchModel { + public int? Id { get; set; } + public string? IngredientName { get; set; } public double? Cost { get; set; } diff --git a/SushiBarContracts/SearchModels/PromotionSearchModel.cs b/SushiBarContracts/SearchModels/PromotionSearchModel.cs index 4fed747..df0b461 100644 --- a/SushiBarContracts/SearchModels/PromotionSearchModel.cs +++ b/SushiBarContracts/SearchModels/PromotionSearchModel.cs @@ -2,6 +2,8 @@ { public class PromotionSearchModel { + public int? Id { get; set; } + public float? Discount { get; set; } public double? TriggeringSum { get; set; } diff --git a/SushiBarDatabaseImplement/Models/Cheque.cs b/SushiBarDatabaseImplement/Models/Cheque.cs index 738309f..21e1c64 100644 --- a/SushiBarDatabaseImplement/Models/Cheque.cs +++ b/SushiBarDatabaseImplement/Models/Cheque.cs @@ -1,4 +1,6 @@ -using System.ComponentModel.DataAnnotations; +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace SushiBarDatabaseImplement.Models @@ -24,5 +26,63 @@ namespace SushiBarDatabaseImplement.Models [ForeignKey("ChequeId")] public virtual List ChequeItems { get; set; } = new(); + + public static Cheque Create(SushiBarDatabase Context, ChequeBindingModel Model) + { + return new Cheque() + { + CustomerId = Model.CustomerId, + OrderDate = Model.OrderDate, + TotalSum = Model.TotalSum, + PromotionId = Model.PromotionId, + ChequeItems = Model.ChequeItems.Select(x => new ChequeItem + { + Dish = Context.Dishes.First(y => y.Id == x.DishId), + Cook = Context.Cooks.First(y => y.Id == x.CookId), + Count = x.Count, + }).ToList() + }; + } + + public void Update(ChequeBindingModel Model) + { + return; + } + + public ChequeViewModel ViewModel => new() + { + Id = Id, + CustomerId = CustomerId, + CustomerFio = Customer?.Fio, + OrderDate = OrderDate, + TotalSum = TotalSum, + PromotionId = PromotionId, + Discount = Promotion?.Discount, + ChequeItems = ChequeItems.ToDictionary(x => x.DishId, x => new ChequeItemViewModel + { + CheuqueId = x.ChequeId, + DishId = x.DishId, + CookId = x.CookId, + CookFio = x.Cook.Fio, + Count = x.Count, + }), + }; + + public void UpdateChequeItems(SushiBarDatabase Context, ChequeBindingModel Model) + { + var Cheque = Context.Cheques.First(x => x.Id == Id); + + foreach (var ChequeItem in Model.ChequeItems) + { + Context.ChequeItems.Add(new ChequeItem + { + Cheque = Cheque, + Dish = Context.Dishes.First(x => x.Id == ChequeItem.DishId), + Count = ChequeItem.Count + }); + + Context.SaveChanges(); + } + } } } diff --git a/SushiBarDatabaseImplement/Models/Cook.cs b/SushiBarDatabaseImplement/Models/Cook.cs index bd662d2..5867303 100644 --- a/SushiBarDatabaseImplement/Models/Cook.cs +++ b/SushiBarDatabaseImplement/Models/Cook.cs @@ -1,4 +1,6 @@ -using System.ComponentModel.DataAnnotations; +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace SushiBarDatabaseImplement.Models @@ -16,5 +18,26 @@ namespace SushiBarDatabaseImplement.Models [ForeignKey("CookId")] public virtual List ChequeItems { get; set; } = new(); + + public static Cook? Create(CookBindingModel Model) + { + return new Cook() + { + Fio = Model.Fio, + EmploymentDate = Model.EmploymentDate, + }; + } + + public void Update(CookBindingModel Model) + { + Fio = Model.Fio; + } + + public CookViewModel ViewModel => new() + { + Id = Id, + Fio = Fio, + EmploymentDate = EmploymentDate, + }; } } diff --git a/SushiBarDatabaseImplement/Models/Customer.cs b/SushiBarDatabaseImplement/Models/Customer.cs index 99925ef..2c828ff 100644 --- a/SushiBarDatabaseImplement/Models/Customer.cs +++ b/SushiBarDatabaseImplement/Models/Customer.cs @@ -1,4 +1,6 @@ -using System.ComponentModel.DataAnnotations; +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace SushiBarDatabaseImplement.Models @@ -18,5 +20,30 @@ namespace SushiBarDatabaseImplement.Models [ForeignKey("CustomerId")] public virtual List Cheques { get; set; } = new(); + + public static Customer? Create(CustomerBindingModel Model) + { + return new Customer() + { + Fio = Model.Fio, + BirthdayDate = Model.BirthdayDate, + SumOfAllOrders = Model.SumOfAllOrders, + }; + } + + public void Update(CustomerBindingModel Model) + { + Fio = Model.Fio; + BirthdayDate = Model.BirthdayDate; + SumOfAllOrders = Model.SumOfAllOrders; + } + + public CustomerViewModel ViewModel => new() + { + Id = Id, + Fio = Fio, + BirthdayDate = BirthdayDate, + SumOfAllOrders = SumOfAllOrders, + }; } } diff --git a/SushiBarDatabaseImplement/Models/Dish.cs b/SushiBarDatabaseImplement/Models/Dish.cs index 2f277d7..a75a85b 100644 --- a/SushiBarDatabaseImplement/Models/Dish.cs +++ b/SushiBarDatabaseImplement/Models/Dish.cs @@ -1,4 +1,6 @@ -using System.ComponentModel.DataAnnotations; +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace SushiBarDatabaseImplement.Models @@ -19,5 +21,74 @@ namespace SushiBarDatabaseImplement.Models [ForeignKey("DishId")] public virtual List ChequeItems { get; set; } = new(); + + public static Dish Create(SushiBarDatabase Context, DishBindingModel Model) + { + return new Dish() + { + DishName = Model.DishName, + Category = Model.Category, + DishIngredients = Model.Ingredients.Select(x => new DishIngredient + { + Ingredient = Context.Ingredients.First(y => y.Id == x.IngredientId), + Count = x.Count, + }).ToList() + }; + } + + public void Update(DishBindingModel Model) + { + DishName = Model.DishName; + Category = Model.Category; + } + + public DishViewModel ViewModel => new() + { + Id = Id, + DishName = DishName, + Category = Category, + Ingredients = DishIngredients.ToDictionary(x => x.IngredientId, x => new DishIngredientViewModel + { + DishId = x.DishId, + IngredientId = x.IngredientId, + IngredientName = x.Ingredient.IngredientName, + Count = x.Count, + }) + }; + + public void UpdateIngredients(SushiBarDatabase Context, DishBindingModel Model) + { + var IngredientsForThisDish = Context.DishIngredients.Where(x => x.DishId == Model.Id).ToList(); + + if (IngredientsForThisDish.Count > 0) + { + // Delete DishIngredient records for selected dish if there is no record with such ingredient in passed model + var UsedComponentIds = Model.Ingredients.Select(x => x.IngredientId).ToList(); + Context.DishIngredients.RemoveRange(IngredientsForThisDish.Where(x => !UsedComponentIds.Contains(x.IngredientId))); + Context.SaveChanges(); + + foreach (var DishIngredientToUpdate in IngredientsForThisDish) + { + DishIngredientToUpdate.Count = Model.Ingredients.First(x => x.IngredientId == DishIngredientToUpdate.IngredientId).Count; + Model.Ingredients.Remove(Model.Ingredients.First(x => x.IngredientId == DishIngredientToUpdate.IngredientId)); + } + + Context.SaveChanges(); + } + + var Dish = Context.Dishes.First(x => x.Id == Id); + + foreach (var DishIngredient in Model.Ingredients) + { + Context.DishIngredients.Add(new DishIngredient + { + Dish = Dish, + Ingredient = Context.Ingredients.First(x => x.Id == DishIngredient.IngredientId), + Count = DishIngredient.Count + }); + + Context.SaveChanges(); + } + } } } diff --git a/SushiBarDatabaseImplement/Models/Ingredient.cs b/SushiBarDatabaseImplement/Models/Ingredient.cs index 38225f1..cf3cdb9 100644 --- a/SushiBarDatabaseImplement/Models/Ingredient.cs +++ b/SushiBarDatabaseImplement/Models/Ingredient.cs @@ -1,7 +1,7 @@ using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using System.Xml.Linq; namespace SushiBarDatabaseImplement.Models { @@ -27,25 +27,22 @@ namespace SushiBarDatabaseImplement.Models return new Ingredient() { IngredientName = Model.IngredientName, - name = model.name, - facultyid = model.facultyid, + Unit = Model.Unit, + Cost = Model.Cost, }; } - public void Update(CourseBindingModel model) + + public void Update(IngredientBindingModel Model) { - if (model == null) - { - return; - } - name = model.name; - facultyid = model.facultyid; + Cost = Model.Cost; } - public CourseViewModel ViewModel => new() + + public IngredientViewModel ViewModel => new() { - course_id = course_id, - name = name, - facultyid = facultyid, - FacultyName = Faculty.name, + Id = Id, + IngredientName = IngredientName, + Unit = Unit, + Cost = Cost, }; } } diff --git a/SushiBarDatabaseImplement/Models/Promotion.cs b/SushiBarDatabaseImplement/Models/Promotion.cs index c198c53..c675d0e 100644 --- a/SushiBarDatabaseImplement/Models/Promotion.cs +++ b/SushiBarDatabaseImplement/Models/Promotion.cs @@ -1,4 +1,6 @@ -using System.ComponentModel.DataAnnotations; +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace SushiBarDatabaseImplement.Models @@ -19,5 +21,29 @@ namespace SushiBarDatabaseImplement.Models [ForeignKey("PromotionId")] public virtual List Cheques { get; set; } = new(); + + public static Promotion? Create(PromotionBindingModel Model) + { + return new Promotion() + { + PromotionName = Model.PromotionName, + Discount = Model.Discount, + TriggeringSum = Model.TriggeringSum, + }; + } + + public void Update(PromotionBindingModel Model) + { + PromotionName = Model.PromotionName; + Discount = Model.Discount; + TriggeringSum = Model.TriggeringSum; + } + + public PromotionViewModel ViewModel => new() + { + PromotionName = PromotionName, + Discount = Discount, + TriggeringSum = TriggeringSum, + }; } } diff --git a/SushiBarDatabaseImplement/Storages/ChequeStorage.cs b/SushiBarDatabaseImplement/Storages/ChequeStorage.cs new file mode 100644 index 0000000..98e980a --- /dev/null +++ b/SushiBarDatabaseImplement/Storages/ChequeStorage.cs @@ -0,0 +1,120 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Storages +{ + public class ChequeStorage + { + public List GetFullList() + { + using var Context = new SushiBarDatabase(); + + return Context.Cheques + .Include(x => x.Customer) + .Include(x => x.Promotion) + .Include(x => x.ChequeItems) + .Select(x => x.ViewModel) + .ToList(); + } + + public List GetFilteredList(ChequeSearchModel Model) + { + using var Context = new SushiBarDatabase(); + + if (Model.CustomerId.HasValue) + { + return Context.Cheques + .Include(x => x.Customer) + .Include(x => x.Promotion) + .Include(x => x.ChequeItems) + .Where(x => x.CustomerId == Model.CustomerId) + .Select(x => x.ViewModel) + .ToList(); + } + + if (Model.OrderDateFrom.HasValue) + { + return Context.Cheques + .Include(x => x.Customer) + .Include(x => x.Promotion) + .Include(x => x.ChequeItems) + .Where(x => x.OrderDate >= Model.OrderDateFrom && x.OrderDate <= Model.OrderDateTo) + .Select(x => x.ViewModel) + .ToList(); + } + + return new(); + } + + public ChequeViewModel? GetElement(ChequeSearchModel Model) + { + if (!Model.Id.HasValue) + return null; + + using var Context = new SushiBarDatabase(); + return Context.Cheques + .Include(x => x.Customer) + .Include(x => x.Promotion) + .Include(x => x.ChequeItems) + .FirstOrDefault(x => x.Id == Model.Id)?.ViewModel; + } + + public ChequeViewModel? Insert(ChequeBindingModel Model) + { + using var Context = new SushiBarDatabase(); + var NewCheque = Cheque.Create(Context, Model); + + if (NewCheque == null) + return null; + + Context.Cheques.Add(NewCheque); + Context.SaveChanges(); + + return NewCheque.ViewModel; + } + + public ChequeViewModel? Update(ChequeBindingModel Model) + { + using var Context = new SushiBarDatabase(); + + using var Transaction = Context.Database.BeginTransaction(); + try + { + var Cheque = Context.Cheques.FirstOrDefault(x => x.Id == Model.Id); + + if (Cheque == null) + return null; + + Cheque.Update(Model); + Context.SaveChanges(); + Cheque.UpdateChequeItems(Context, Model); + + Transaction.Commit(); + return Cheque.ViewModel; + } + catch + { + Transaction.Rollback(); + throw; + } + } + + public ChequeViewModel? Delete(ChequeBindingModel Model) + { + using var Context = new SushiBarDatabase(); + var Cheque = Context.Cheques + .Include(x => x.ChequeItems) + .FirstOrDefault(rec => rec.Id == Model.Id); + + if (Cheque == null) + return null; + + Context.Cheques.Remove(Cheque); + Context.SaveChanges(); + return Cheque.ViewModel; + } + } +} diff --git a/SushiBarDatabaseImplement/Storages/CookStorage.cs b/SushiBarDatabaseImplement/Storages/CookStorage.cs new file mode 100644 index 0000000..c555b89 --- /dev/null +++ b/SushiBarDatabaseImplement/Storages/CookStorage.cs @@ -0,0 +1,83 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Storages +{ + public class CookStorage + { + public List GetFullList() + { + using var Context = new SushiBarDatabase(); + return Context.Cooks.Select(x => x.ViewModel).ToList(); + } + + public List GetFilteredList(CookSearchModel Model) + { + using var Context = new SushiBarDatabase(); + + if (Model.EmploymentDateFrom.HasValue) + { + return Context.Cooks + .Where(x => x.EmploymentDate >= Model.EmploymentDateFrom && x.EmploymentDate <= Model.EmploymentDateTo) + .Select(x => x.ViewModel) + .ToList(); + } + + return new(); + } + + public CookViewModel? GetElement(CookSearchModel Model) + { + if (!Model.Id.HasValue) + return null; + + using var Context = new SushiBarDatabase(); + return Context.Cooks + .FirstOrDefault(x => x.Id == Model.Id)?.ViewModel; + } + + public CookViewModel? Insert(CookBindingModel Model) + { + var NewCook = Cook.Create(Model); + + if (NewCook == null) + return null; + + using var Context = new SushiBarDatabase(); + Context.Cooks.Add(NewCook); + Context.SaveChanges(); + + return NewCook.ViewModel; + } + + public CookViewModel? Update(CookBindingModel Model) + { + using var Context = new SushiBarDatabase(); + + var Cook = Context.Cooks.FirstOrDefault(x => x.Id == Model.Id); + + if (Cook == null) + return null; + + Cook.Update(Model); + Context.SaveChanges(); + + return Cook.ViewModel; + } + + public CookViewModel? Delete(CookBindingModel Model) + { + using var Context = new SushiBarDatabase(); + var Cook = Context.Cooks.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Cook == null) + return null; + + Context.Cooks.Remove(Cook); + Context.SaveChanges(); + return Cook.ViewModel; + } + } +} diff --git a/SushiBarDatabaseImplement/Storages/CustomerStorage.cs b/SushiBarDatabaseImplement/Storages/CustomerStorage.cs new file mode 100644 index 0000000..56ad302 --- /dev/null +++ b/SushiBarDatabaseImplement/Storages/CustomerStorage.cs @@ -0,0 +1,83 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Storages +{ + public class CustomerStorage + { + public List GetFullList() + { + using var Context = new SushiBarDatabase(); + return Context.Customers.Select(x => x.ViewModel).ToList(); + } + + public List GetFilteredList(CustomerSearchModel Model) + { + using var Context = new SushiBarDatabase(); + + if (Model.SumOfAllOrders.HasValue) + { + return Context.Customers + .Where(x => x.SumOfAllOrders >= Model.SumOfAllOrders) + .Select(x => x.ViewModel) + .ToList(); + } + + return new(); + } + + public CustomerViewModel? GetElement(CustomerSearchModel Model) + { + if (!Model.Id.HasValue) + return null; + + using var Context = new SushiBarDatabase(); + return Context.Customers + .FirstOrDefault(x => x.Id == Model.Id)?.ViewModel; + } + + public CustomerViewModel? Insert(CustomerBindingModel Model) + { + var NewCustomer = Customer.Create(Model); + + if (NewCustomer == null) + return null; + + using var Context = new SushiBarDatabase(); + Context.Customers.Add(NewCustomer); + Context.SaveChanges(); + + return NewCustomer.ViewModel; + } + + public CustomerViewModel? Update(CustomerBindingModel Model) + { + using var Context = new SushiBarDatabase(); + + var Customer = Context.Customers.FirstOrDefault(x => x.Id == Model.Id); + + if (Customer == null) + return null; + + Customer.Update(Model); + Context.SaveChanges(); + + return Customer.ViewModel; + } + + public CustomerViewModel? Delete(CustomerBindingModel Model) + { + using var Context = new SushiBarDatabase(); + var Customer = Context.Customers.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Customer == null) + return null; + + Context.Customers.Remove(Customer); + Context.SaveChanges(); + return Customer.ViewModel; + } + } +} diff --git a/SushiBarDatabaseImplement/Storages/DishStorage.cs b/SushiBarDatabaseImplement/Storages/DishStorage.cs new file mode 100644 index 0000000..788e8d9 --- /dev/null +++ b/SushiBarDatabaseImplement/Storages/DishStorage.cs @@ -0,0 +1,100 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Storages +{ + public class DishStorage + { + public List GetFullList() + { + using var Context = new SushiBarDatabase(); + + return Context.Dishes + .Include(x => x.DishIngredients) + .Select(x => x.ViewModel) + .ToList(); + } + + public List GetFilteredList(DishSearchModel Model) + { + if (string.IsNullOrEmpty(Model.DishName)) + return new(); + + using var Context = new SushiBarDatabase(); + return Context.Dishes + .Include(x => x.DishIngredients) + .Where(x => x.DishName.Contains(Model.DishName)) + .Select(x => x.ViewModel) + .ToList(); + } + + public DishViewModel? GetElement(DishSearchModel Model) + { + if (!Model.Id.HasValue) + return null; + + using var Context = new SushiBarDatabase(); + return Context.Dishes + .Include(x => x.DishIngredients) + .FirstOrDefault(x => x.Id == Model.Id)?.ViewModel; + } + + public DishViewModel? Insert(DishBindingModel Model) + { + using var Context = new SushiBarDatabase(); + var NewDish = Dish.Create(Context, Model); + + if (NewDish == null) + return null; + + Context.Dishes.Add(NewDish); + Context.SaveChanges(); + + return NewDish.ViewModel; + } + + public DishViewModel? Update(DishBindingModel Model) + { + using var Context = new SushiBarDatabase(); + + using var Transaction = Context.Database.BeginTransaction(); + try + { + var Dish = Context.Dishes.FirstOrDefault(x => x.Id == Model.Id); + + if (Dish == null) + return null; + + Dish.Update(Model); + Context.SaveChanges(); + Dish.UpdateIngredients(Context, Model); + + Transaction.Commit(); + return Dish.ViewModel; + } + catch + { + Transaction.Rollback(); + throw; + } + } + + public DishViewModel? Delete(DishBindingModel Model) + { + using var Context = new SushiBarDatabase(); + var Dish = Context.Dishes + .Include(x => x.DishIngredients) + .FirstOrDefault(rec => rec.Id == Model.Id); + + if (Dish == null) + return null; + + Context.Dishes.Remove(Dish); + Context.SaveChanges(); + return Dish.ViewModel; + } + } +} diff --git a/SushiBarDatabaseImplement/Storages/IngredientStorage.cs b/SushiBarDatabaseImplement/Storages/IngredientStorage.cs index 1dbee47..e88a08a 100644 --- a/SushiBarDatabaseImplement/Storages/IngredientStorage.cs +++ b/SushiBarDatabaseImplement/Storages/IngredientStorage.cs @@ -1,4 +1,7 @@ -using SushiBarContracts.ViewModels; +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; namespace SushiBarDatabaseImplement.Storages { @@ -7,8 +10,71 @@ namespace SushiBarDatabaseImplement.Storages public List GetFullList() { using var Context = new SushiBarDatabase(); + return Context.Ingredients.Select(x => x.ViewModel).ToList(); + } - return + public List GetFilteredList(IngredientSearchModel Model) + { + if (string.IsNullOrEmpty(Model.IngredientName)) + return new(); + + using var Context = new SushiBarDatabase(); + return Context.Ingredients + .Where(x => x.IngredientName.Contains(Model.IngredientName)) + .Select(x => x.ViewModel) + .ToList(); + } + + public IngredientViewModel? GetElement(IngredientSearchModel Model) + { + if (!Model.Id.HasValue) + return null; + + using var Context = new SushiBarDatabase(); + return Context.Ingredients + .FirstOrDefault(x => x.Id == Model.Id)?.ViewModel; + } + + public IngredientViewModel? Insert(IngredientBindingModel Model) + { + var NewIngredient = Ingredient.Create(Model); + + if (NewIngredient == null) + return null; + + using var Context = new SushiBarDatabase(); + Context.Ingredients.Add(NewIngredient); + Context.SaveChanges(); + + return NewIngredient.ViewModel; + } + + public IngredientViewModel? Update(IngredientBindingModel Model) + { + using var Context = new SushiBarDatabase(); + + var Ingredient = Context.Ingredients.FirstOrDefault(x => x.Id == Model.Id); + + if (Ingredient == null) + return null; + + Ingredient.Update(Model); + Context.SaveChanges(); + + return Ingredient.ViewModel; + } + + public IngredientViewModel? Delete(IngredientBindingModel Model) + { + using var Context = new SushiBarDatabase(); + var Ingredient = Context.Ingredients.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Ingredient == null) + return null; + + Context.Ingredients.Remove(Ingredient); + Context.SaveChanges(); + return Ingredient.ViewModel; } } } diff --git a/SushiBarDatabaseImplement/Storages/PromotionStorage.cs b/SushiBarDatabaseImplement/Storages/PromotionStorage.cs new file mode 100644 index 0000000..47ae0f0 --- /dev/null +++ b/SushiBarDatabaseImplement/Storages/PromotionStorage.cs @@ -0,0 +1,80 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Storages +{ + public class PromotionStorage + { + public List GetFullList() + { + using var Context = new SushiBarDatabase(); + return Context.Promotions.Select(x => x.ViewModel).ToList(); + } + + public List GetFilteredList(PromotionSearchModel Model) + { + if (!Model.TriggeringSum.HasValue) + return new(); + + using var Context = new SushiBarDatabase(); + return Context.Promotions + .Where(x => x.TriggeringSum >= Model.TriggeringSum) + .Select(x => x.ViewModel) + .ToList(); + } + + public PromotionViewModel? GetElement(PromotionSearchModel Model) + { + if (!Model.Id.HasValue) + return null; + + using var Context = new SushiBarDatabase(); + return Context.Promotions + .FirstOrDefault(x => x.Id == Model.Id)?.ViewModel; + } + + public PromotionViewModel? Insert(PromotionBindingModel Model) + { + var NewPromotion = Promotion.Create(Model); + + if (NewPromotion == null) + return null; + + using var Context = new SushiBarDatabase(); + Context.Promotions.Add(NewPromotion); + Context.SaveChanges(); + + return NewPromotion.ViewModel; + } + + public PromotionViewModel? Update(PromotionBindingModel Model) + { + using var Context = new SushiBarDatabase(); + + var Promotion = Context.Promotions.FirstOrDefault(x => x.Id == Model.Id); + + if (Promotion == null) + return null; + + Promotion.Update(Model); + Context.SaveChanges(); + + return Promotion.ViewModel; + } + + public PromotionViewModel? Delete(PromotionBindingModel Model) + { + using var Context = new SushiBarDatabase(); + var Promotion = Context.Promotions.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Promotion == null) + return null; + + Context.Promotions.Remove(Promotion); + Context.SaveChanges(); + return Promotion.ViewModel; + } + } +}