CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs
Илья Федотов 92b18d3a1a Что с веткой?
2024-05-30 13:08:24 +04:00

93 lines
3.1 KiB
C#

using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopDataBaseImplement.Implements
{
public class CostItemStorage : ICostItemStorage
{
public CostItemViewModel? Insert(CostItemBindingModel model) {
var newComponent = CostItem.Create(model);
if (newComponent == null) {
return null;
}
using var context = new Database();
context.CostItems.Add(newComponent);
context.SaveChanges();
return context.CostItems
.Include(x => x.Employee)
.FirstOrDefault(x => x.ID == newComponent.ID)?.GetViewModel;
}
public CostItemViewModel? Update(CostItemBindingModel model) {
using var context = new Database();
var component = context.CostItems.FirstOrDefault(x => x.ID == model.ID);
if (component == null) {
return null;
}
component.Update(model);
context.SaveChanges();
return context.CostItems
.Include(x => x.Employee)
.FirstOrDefault(x => x.ID == component.ID)?.GetViewModel;
}
public CostItemViewModel? Delete(CostItemBindingModel model)
{
using var context = new Database();
var element = context.CostItems.FirstOrDefault(rec => rec.ID == model.ID);
if (element != null)
{
context.CostItems.Remove(element);
context.SaveChanges();
return context.CostItems
.Include(x => x.Employee)
.FirstOrDefault(x => x.ID == element.ID)?.GetViewModel;
}
return null;
}
public CostItemViewModel? GetElement(CostItemSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue)
{
return null;
}
using var context = new Database();
return context.CostItems
.Include(x => x.Employee)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) ||
(model.ID.HasValue && x.ID == model.ID))?.GetViewModel;
}
public List<CostItemViewModel> GetFillteredList(CostItemSearchModel model)
{
using var context = new Database();
if (model.EmployeeID.HasValue) {
return context.CostItems
.Include(x => x.Employee)
.Where(x => x.EmployeeID == model.EmployeeID)
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public List<CostItemViewModel> GetFullList()
{
using var context = new Database();
return context.CostItems.Select(x => x.GetViewModel).ToList();
}
}
}