PIbd-31_Malafeev.L.S._COP_25/Cop_25/DatabaseImplement/Implements/DeliveryStorage.cs

76 lines
2.4 KiB
C#
Raw Normal View History

2024-11-05 22:04:24 +04:00
using Contracts.BindlingModels;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Implements
{
public class DeliveryStorage : IDeliveryStorage
{
public List<DeliveryViewModel> GetFullList()
{
using var context = new Database();
return context.Deliverys.Select(x => x.GetViewModel).ToList();
}
public List<DeliveryViewModel> GetFilteredList(DeliverySearchModel model)
{
if (string.IsNullOrEmpty(model.DeliveryType))
{
return new();
}
using var context = new Database();
return context.Deliverys.Where(x => x.DeliveryType == model.DeliveryType).Select(x => x.GetViewModel).ToList();
}
public DeliveryViewModel? GetElement(DeliverySearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.Deliverys.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public DeliveryViewModel? Insert(DeliveryBindingModel model)
{
var newDelivery = Delivery.Create(model);
if (newDelivery == null) return null;
using var context = new Database();
context.Deliverys.Add(newDelivery);
context.SaveChanges();
return newDelivery.GetViewModel;
}
public DeliveryViewModel? Update(DeliveryBindingModel model)
{
using var context = new Database();
var provider = context.Deliverys.FirstOrDefault(x => x.Id == model.Id);
if (provider == null) return null;
provider.Update(model);
context.SaveChanges();
return provider.GetViewModel;
}
public DeliveryViewModel? Delete(DeliveryBindingModel model)
{
using var context = new Database();
var element = context.Deliverys.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Deliverys.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}