StorekeeperStorage implement

This commit is contained in:
Леонид Малафеев 2024-04-28 17:39:36 +04:00
parent 93835aae7a
commit a61d49a15a
2 changed files with 99 additions and 4 deletions

View File

@ -6,10 +6,6 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.29">

View File

@ -0,0 +1,99 @@
using CarCenterContracts.BindingModels;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using CarCenterDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarCenterDatabaseImplement.Implements
{
public class StorekeeperStorage : IStorekeeperStorage
{
public List<StorekeeperViewModel> GetFullList()
{
using var context = new CarCenterDatabase();
return context.Storekeepers
.Select(x => x.GetViewModel)
.ToList();
}
public List<StorekeeperViewModel> GetFilteredList(StorekeeperSearchModel model)
{
if (model == null)
{
return new();
}
if (model.Id.HasValue)
{
var res = GetElement(model);
return res != null ? new() { res } : new();
}
return new();
}
public StorekeeperViewModel? GetElement(StorekeeperSearchModel model)
{
using var context = new CarCenterDatabase();
if (model.Id.HasValue)
return context.Storekeepers
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
return null;
}
public StorekeeperViewModel? Delete(StorekeeperBindingModel model)
{
using var context = new CarCenterDatabase();
var res = context.Storekeepers
.FirstOrDefault(x => x.Id == model.Id);
if (res != null)
{
context.Storekeepers.Remove(res);
context.SaveChanges();
}
return res?.GetViewModel;
}
public StorekeeperViewModel? Insert(StorekeeperBindingModel model)
{
using var context = new CarCenterDatabase();
var res = Storekeeper.Create(model);
if (res != null)
{
context.Storekeepers.Add(res);
context.SaveChanges();
}
return res?.GetViewModel;
}
public StorekeeperViewModel? Update(StorekeeperBindingModel model)
{
using var context = new CarCenterDatabase();
var res = context.Storekeepers.FirstOrDefault(x => x.Id == model.Id);
if (res != null)
{
res.Update(model);
context.SaveChanges();
}
return res?.GetViewModel;
}
}
}