CRUD and storages
This commit is contained in:
parent
c81946029d
commit
cd13ac1206
@ -2,6 +2,8 @@
|
||||
{
|
||||
public class ChequeBindingModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public List<ChequeItemBindingModel> ChequeItems { get; set; } = new();
|
||||
|
||||
public int? CustomerId { get; set; }
|
||||
|
@ -2,6 +2,8 @@
|
||||
{
|
||||
public class CookBindingModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Fio { get; set; } = string.Empty;
|
||||
|
||||
public DateTime EmploymentDate { get; set; }
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -2,6 +2,8 @@
|
||||
{
|
||||
public class PromotionBindingModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string PromotionName { get; set; } = string.Empty;
|
||||
|
||||
public float Discount { get; set; }
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
{
|
||||
public class CustomerSearchModel
|
||||
{
|
||||
public int? Id { get; set; }
|
||||
|
||||
public double? SumOfAllOrders { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
{
|
||||
public class DishSearchModel
|
||||
{
|
||||
public int? Id { get; set; }
|
||||
|
||||
public string? DishName { get; set; }
|
||||
|
||||
public string? Category { get; set; }
|
||||
|
@ -2,6 +2,8 @@
|
||||
{
|
||||
public class IngredientSearchModel
|
||||
{
|
||||
public int? Id { get; set; }
|
||||
|
||||
public string? IngredientName { get; set; }
|
||||
|
||||
public double? Cost { get; set; }
|
||||
|
@ -2,6 +2,8 @@
|
||||
{
|
||||
public class PromotionSearchModel
|
||||
{
|
||||
public int? Id { get; set; }
|
||||
|
||||
public float? Discount { get; set; }
|
||||
|
||||
public double? TriggeringSum { get; set; }
|
||||
|
@ -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<ChequeItem> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<ChequeItem> 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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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<Cheque> 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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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<ChequeItem> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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<Cheque> 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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
120
SushiBarDatabaseImplement/Storages/ChequeStorage.cs
Normal file
120
SushiBarDatabaseImplement/Storages/ChequeStorage.cs
Normal file
@ -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<ChequeViewModel> 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<ChequeViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
83
SushiBarDatabaseImplement/Storages/CookStorage.cs
Normal file
83
SushiBarDatabaseImplement/Storages/CookStorage.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using SushiBarContracts.BindingModels;
|
||||
using SushiBarContracts.SearchModels;
|
||||
using SushiBarContracts.ViewModels;
|
||||
using SushiBarDatabaseImplement.Models;
|
||||
|
||||
namespace SushiBarDatabaseImplement.Storages
|
||||
{
|
||||
public class CookStorage
|
||||
{
|
||||
public List<CookViewModel> GetFullList()
|
||||
{
|
||||
using var Context = new SushiBarDatabase();
|
||||
return Context.Cooks.Select(x => x.ViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<CookViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
83
SushiBarDatabaseImplement/Storages/CustomerStorage.cs
Normal file
83
SushiBarDatabaseImplement/Storages/CustomerStorage.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using SushiBarContracts.BindingModels;
|
||||
using SushiBarContracts.SearchModels;
|
||||
using SushiBarContracts.ViewModels;
|
||||
using SushiBarDatabaseImplement.Models;
|
||||
|
||||
namespace SushiBarDatabaseImplement.Storages
|
||||
{
|
||||
public class CustomerStorage
|
||||
{
|
||||
public List<CustomerViewModel> GetFullList()
|
||||
{
|
||||
using var Context = new SushiBarDatabase();
|
||||
return Context.Customers.Select(x => x.ViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<CustomerViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
100
SushiBarDatabaseImplement/Storages/DishStorage.cs
Normal file
100
SushiBarDatabaseImplement/Storages/DishStorage.cs
Normal file
@ -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<DishViewModel> GetFullList()
|
||||
{
|
||||
using var Context = new SushiBarDatabase();
|
||||
|
||||
return Context.Dishes
|
||||
.Include(x => x.DishIngredients)
|
||||
.Select(x => x.ViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<DishViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<IngredientViewModel> GetFullList()
|
||||
{
|
||||
using var Context = new SushiBarDatabase();
|
||||
return Context.Ingredients.Select(x => x.ViewModel).ToList();
|
||||
}
|
||||
|
||||
return
|
||||
public List<IngredientViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
80
SushiBarDatabaseImplement/Storages/PromotionStorage.cs
Normal file
80
SushiBarDatabaseImplement/Storages/PromotionStorage.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using SushiBarContracts.BindingModels;
|
||||
using SushiBarContracts.SearchModels;
|
||||
using SushiBarContracts.ViewModels;
|
||||
using SushiBarDatabaseImplement.Models;
|
||||
|
||||
namespace SushiBarDatabaseImplement.Storages
|
||||
{
|
||||
public class PromotionStorage
|
||||
{
|
||||
public List<PromotionViewModel> GetFullList()
|
||||
{
|
||||
using var Context = new SushiBarDatabase();
|
||||
return Context.Promotions.Select(x => x.ViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<PromotionViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user