This commit is contained in:
Marselchi 2024-04-28 17:41:50 +04:00
commit 62d4b6aebc

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;
}
}
}