2024-05-04 19:44:42 +04:00
|
|
|
|
using SewingDressesContracts.BindingModels;
|
|
|
|
|
using SewingDressesContracts.SearchModels;
|
|
|
|
|
using SewingDressesContracts.StoragesContracts;
|
|
|
|
|
using SewingDressesContracts.ViewModels;
|
|
|
|
|
using SewingDressesDatabaseImplement.Models;
|
|
|
|
|
|
|
|
|
|
namespace SewingDressesDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class OrderStorage : IOrderStorage
|
|
|
|
|
{
|
|
|
|
|
public List<OrderViewModel?> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
|
|
|
|
return context.Orders.Select(x => AcessDressesStorage(x.GetViewModel, context)).ToList();
|
|
|
|
|
}
|
|
|
|
|
public List<OrderViewModel?> GetFilteredList(OrderSearchModel model)
|
|
|
|
|
{
|
2024-05-04 19:46:22 +04:00
|
|
|
|
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue)
|
2024-05-04 19:44:42 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
2024-05-04 19:46:22 +04:00
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
return context.Orders.Where(x => x.Id == model.Id).Select(x => AcessDressesStorage(x.GetViewModel, context)).ToList();
|
|
|
|
|
else
|
|
|
|
|
return context.Orders.Where(x => x.DateCreate >= model.DateFrom).Where(x => x.DateCreate <= model.DateTo).Select(x => AcessDressesStorage(x.GetViewModel, context)).ToList();
|
2024-05-04 19:44:42 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
|
|
|
|
return AcessDressesStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel, context);
|
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var order = Order.Create(model);
|
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
|
|
|
|
context.Orders.Add(order);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return AcessDressesStorage(order.GetViewModel, context);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
|
|
|
|
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
order.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return AcessDressesStorage(order.GetViewModel, context);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
|
|
|
|
var element = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Orders.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return AcessDressesStorage(element.GetViewModel, context);
|
|
|
|
|
}
|
|
|
|
|
public static OrderViewModel? AcessDressesStorage(OrderViewModel? model, SewingDressesDatabase context)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var dress = context.Dresses.FirstOrDefault(x => x.Id == model.DressId);
|
|
|
|
|
if (dress == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
model.DressName = dress.DressName;
|
|
|
|
|
return model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|