ImplementerStorage

This commit is contained in:
Sergey Kozyrev 2024-04-24 12:50:12 +04:00
parent 0c18013245
commit 7830d9c21f
2 changed files with 72 additions and 4 deletions

View File

@ -6,10 +6,6 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.22" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.22" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,72 @@
using DatabaseImplement.Models;
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using Contracts.StoragesContracts;
namespace DatabaseImplement.Implements
{
public class ImplementerStorage : IImplementerStorage
{
public ImplementerViewModel? Delete(ImplementerBindingModel model)
{
using var context = new FactoryGoWorkDatabase();
var newImplementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
if (newImplementer == null)
return null;
context.Implementers.Remove(newImplementer);
context.SaveChanges();
return newImplementer.GetViewModel;
}
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
{
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login)) { return null; }
using var context = new FactoryGoWorkDatabase();
return context.Implementers.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Login) && x.Login.Equals(model.Login)))?.GetViewModel; ;
}
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
{
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login))
return new();
using var context = new FactoryGoWorkDatabase();
if (model.Id.HasValue)
{
return context.Implementers.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
else
{
return context.Implementers.Where(x => x.Login.Equals(model.Login)).Select(x => x.GetViewModel).ToList();
}
}
public List<ImplementerViewModel> GetFullList()
{
using var context = new FactoryGoWorkDatabase();
return context.Implementers.Select(x => x.GetViewModel).ToList();
}
public ImplementerViewModel? Insert(ImplementerBindingModel model)
{
using var context = new FactoryGoWorkDatabase();
var newImplementer = Implementer.Create(model);
if (newImplementer == null)
return null;
context.Implementers.Add(newImplementer);
context.SaveChanges();
return newImplementer.GetViewModel;
}
public ImplementerViewModel? Update(ImplementerBindingModel model)
{
using var context = new FactoryGoWorkDatabase();
var newImplementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
if (newImplementer == null)
return null;
newImplementer.Update(model);
context.SaveChanges();
return newImplementer.GetViewModel;
}
}
}