PIbd-21_KozyrevSS_SewingDre.../SewingDresses/SewingDressesDatabaseImplement/Implements/OrderStorage.cs

90 lines
3.0 KiB
C#
Raw Normal View History

2024-03-08 14:55: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();
2024-04-02 19:16:26 +04:00
return context.Orders.Select(x => AcessDressesStorage(x.GetViewModel, context)).ToList();
2024-03-08 14:55:42 +04:00
}
public List<OrderViewModel?> GetFilteredList(OrderSearchModel model)
{
2024-04-02 19:16:26 +04:00
if (!model.Id.HasValue)
2024-03-08 14:55:42 +04:00
{
return new();
}
using var context = new SewingDressesDatabase();
2024-04-02 19:16:26 +04:00
return context.Orders.Where(x => x.Id == model.Id).Select(x => AcessDressesStorage(x.GetViewModel, context)).ToList();
2024-03-08 14:55:42 +04:00
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new SewingDressesDatabase();
2024-04-02 19:16:26 +04:00
return AcessDressesStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel, context);
2024-03-08 14:55:42 +04:00
}
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();
2024-04-02 19:16:26 +04:00
return AcessDressesStorage(order.GetViewModel, context);
2024-03-08 14:55:42 +04:00
}
public OrderViewModel? Update(OrderBindingModel model)
{
2024-04-02 19:16:26 +04:00
2024-03-08 14:55:42 +04:00
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();
2024-04-02 19:16:26 +04:00
return AcessDressesStorage(order.GetViewModel, context);
2024-03-08 14:55:42 +04:00
}
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();
2024-04-02 19:16:26 +04:00
return AcessDressesStorage(element.GetViewModel, context);
2024-03-08 14:55:42 +04:00
}
2024-04-02 19:16:26 +04:00
public static OrderViewModel? AcessDressesStorage(OrderViewModel? model, SewingDressesDatabase context)
2024-03-08 14:55:42 +04:00
{
if (model == null)
{
return null;
}
2024-04-02 19:16:26 +04:00
var dress = context.Dresses.FirstOrDefault(x => x.Id == model.DressId);
if (dress == null)
2024-03-08 14:55:42 +04:00
{
2024-04-02 19:16:26 +04:00
return null;
2024-03-08 14:55:42 +04:00
}
2024-04-02 19:16:26 +04:00
model.DressName = dress.DressName;
2024-03-08 14:55:42 +04:00
return model;
}
2024-04-02 19:16:26 +04:00
2024-03-08 14:55:42 +04:00
}
}