103 lines
3.4 KiB
C#
103 lines
3.4 KiB
C#
using ElectronicsShopContracts.BindingModels;
|
|
using ElectronicsShopContracts.SearchModels;
|
|
using ElectronicsShopContracts.StorageContracts;
|
|
using ElectronicsShopContracts.ViewModels;
|
|
using ElectronicsShopDataBaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
namespace ElectronicsShopDataBaseImplement.Implements
|
|
{
|
|
public class OrderStorage : IOrderStorage
|
|
{
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
{
|
|
using var context = new Database();
|
|
var newComponent = Order.Create(context,model);
|
|
if (newComponent == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Orders.Add(newComponent);
|
|
context.SaveChanges();
|
|
return newComponent.GetViewModel;
|
|
}
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model) {
|
|
using var context = new Database();
|
|
using var transcation = context.Database.BeginTransaction();
|
|
try {
|
|
var product = context.Orders.FirstOrDefault(rec => rec.ID == model.ID);
|
|
if (product == null) {
|
|
return null;
|
|
}
|
|
product.UpdateProducts(context, model);
|
|
transcation.Commit();
|
|
return product.GetViewModel;
|
|
}
|
|
catch {
|
|
transcation.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
{
|
|
using var context = new Database();
|
|
var element = context.Orders.FirstOrDefault(rec => rec.ID == model.ID);
|
|
if (element != null)
|
|
{
|
|
context.Orders.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
{
|
|
if (!model.ID.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new Database();
|
|
return context.Orders
|
|
.Include(x => x.Payments)
|
|
.Include(x => x.Products)
|
|
.ThenInclude(x => x._product)
|
|
.FirstOrDefault(x => (model.ID.HasValue && x.ID == model.ID))?.GetViewModel;
|
|
}
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
{
|
|
using var context = new Database();
|
|
if (!model.ID.HasValue && (model.DateFrom == null || model.DateTo == null))
|
|
{
|
|
return new();
|
|
}
|
|
else if (model.ClientID.HasValue) {
|
|
return context.Orders
|
|
.Include (x => x.Products)
|
|
.Where(x => x.ClientID == model.ClientID)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
return context.Orders
|
|
.Include(x => x.Products)
|
|
.ThenInclude(x => x._product)
|
|
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
{
|
|
using var context = new Database();
|
|
return context.Orders
|
|
.Include(x => x.Products)
|
|
.ThenInclude(x => x._product).ToList()
|
|
.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
}
|
|
}
|