CourseWork_CompShop/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/EquipmentReceivingStorage.cs

88 lines
3.1 KiB
C#
Raw Normal View History

2023-04-07 16:10:59 +04:00
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement.Implements
{
internal class EquipmentReceivingStorage : IEquipmentReceivingStorage
{
public EquipmentReceivingViewModel? Delete(EquipmentReceivingBindingModel model)
{
using var context = new ComputerShopDatabase();
var element = context.EquipmentReceivings.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.EquipmentReceivings.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public EquipmentReceivingViewModel? GetElement(EquipmentReceivingSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ComputerShopDatabase();
return context.EquipmentReceivings
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public List<EquipmentReceivingViewModel> GetFilteredList(EquipmentReceivingSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ComputerShopDatabase();
return context.EquipmentReceivings
2023-04-07 17:53:57 +04:00
.Where(x => x.Id == model.Supply.Component.Id)
2023-04-07 16:10:59 +04:00
.Select(x => x.GetViewModel)
.ToList();
}
public List<EquipmentReceivingViewModel> GetFullList()
{
using var context = new ComputerShopDatabase();
return context.EquipmentReceivings.Select(x => x.GetViewModel).ToList();
}
public EquipmentReceivingViewModel? Insert(EquipmentReceivingBindingModel model)
{
var newOrder = EquipmentReceiving.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new ComputerShopDatabase();
context.EquipmentReceivings.Add(newOrder);
context.SaveChanges();
return context.EquipmentReceivings.FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
}
public EquipmentReceivingViewModel? Update(EquipmentReceivingBindingModel model)
{
using var context = new ComputerShopDatabase();
var order = context.EquipmentReceivings.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.EquipmentReceivings.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
}
}