PIbd-23-Nasyrov-A.G.-Flower.../FlowerShopDatabaseImplement/OrderStorage.cs

85 lines
2.9 KiB
C#
Raw Normal View History

2024-03-12 21:47:28 +04:00
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlowerShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new FlowerShopDataBase();
return context.Orders.Include(x => x.Flower)
.Select(x => x.GetViewModel)
2024-03-12 21:47:28 +04:00
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
2024-03-12 21:47:28 +04:00
using var context = new FlowerShopDataBase();
return context.Orders.Include(x => x.Flower)
.Where(x => ((!model.Id.HasValue || x.Id == model.Id) &&
(!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) &&
(!model.DateTo.HasValue || x.DateCreate <= model.DateTo)))
.Select(x => x.GetViewModel)
2024-03-12 21:47:28 +04:00
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new FlowerShopDataBase();
2024-04-06 18:45:56 +04:00
return context.Orders.Include(x => x.Flower).Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
2024-03-12 21:47:28 +04:00
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new FlowerShopDataBase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
2024-03-12 21:47:28 +04:00
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new FlowerShopDataBase();
2024-04-06 18:45:56 +04:00
var order = context.Orders.Include(x => x.Flower).Include(x => x.Client).FirstOrDefault(x => x.Id ==
2024-03-12 21:47:28 +04:00
model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
2024-03-12 21:47:28 +04:00
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new FlowerShopDataBase();
2024-04-06 18:45:56 +04:00
var element = context.Orders.Include(x => x.Flower).Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id);
2024-03-12 21:47:28 +04:00
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
2024-03-12 21:47:28 +04:00
}
return null;
}
}
}