102 lines
2.8 KiB
C#
102 lines
2.8 KiB
C#
using PlumbingRepairContracts.BindingModels;
|
|
using PlumbingRepairContracts.SearchModels;
|
|
using PlumbingRepairContracts.StoragesContracts;
|
|
using PlumbingRepairContracts.ViewModels;
|
|
using PlumbingRepairDatabaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace PlumbingRepairDatabaseImplement.Implements
|
|
{
|
|
public class ImplementerStorage : IImplementerStorage
|
|
{
|
|
public ImplementerViewModel? Delete(ImplementerBindingModel model)
|
|
{
|
|
using var context = new PlumbingRepairDataBase();
|
|
var element = context.Implementers
|
|
.Include(x => x.Orders)
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Implementers.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
|
|
{
|
|
using var context = new PlumbingRepairDataBase();
|
|
if (model.Id.HasValue)
|
|
{
|
|
return context.Implementers
|
|
.FirstOrDefault(x => (x.Id == model.Id))?.GetViewModel;
|
|
}
|
|
else if (!string.IsNullOrEmpty(model.ImplementerFIO) && !string.IsNullOrEmpty(model.Password))
|
|
{
|
|
return context.Implementers
|
|
.FirstOrDefault(x => (x.ImplementerFIO == model.ImplementerFIO && x.Password == model.Password))?.GetViewModel;
|
|
}
|
|
else if (!string.IsNullOrEmpty(model.ImplementerFIO))
|
|
{
|
|
return context.Implementers
|
|
.FirstOrDefault(x => (x.ImplementerFIO == model.ImplementerFIO))?.GetViewModel;
|
|
}
|
|
return new();
|
|
}
|
|
|
|
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return new();
|
|
}
|
|
if (!string.IsNullOrEmpty(model.ImplementerFIO))
|
|
{
|
|
using var context = new PlumbingRepairDataBase();
|
|
return context.Implementers
|
|
.Include(x => x.Orders)
|
|
.Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
return new();
|
|
}
|
|
|
|
public List<ImplementerViewModel> GetFullList()
|
|
{
|
|
using var context = new PlumbingRepairDataBase();
|
|
return context.Implementers
|
|
.Include(x => x.Orders)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public ImplementerViewModel? Insert(ImplementerBindingModel model)
|
|
{
|
|
var newImplementer = Implementer.Create(model);
|
|
if (newImplementer == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new PlumbingRepairDataBase();
|
|
context.Implementers.Add(newImplementer);
|
|
context.SaveChanges();
|
|
return newImplementer.GetViewModel;
|
|
}
|
|
|
|
public ImplementerViewModel? Update(ImplementerBindingModel model)
|
|
{
|
|
using var context = new PlumbingRepairDataBase();
|
|
var client = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
|
|
if (client == null)
|
|
{
|
|
return null;
|
|
}
|
|
client.Update(model);
|
|
context.SaveChanges();
|
|
return client.GetViewModel;
|
|
}
|
|
}
|
|
}
|