73 lines
2.3 KiB
C#
73 lines
2.3 KiB
C#
|
using Contracts.BindingModels;
|
|||
|
using Contracts.SearchModels;
|
|||
|
using Contracts.StoragesContracts;
|
|||
|
using Contracts.ViewModels;
|
|||
|
using DatabaseImplement.Models;
|
|||
|
|
|||
|
namespace DatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class GuarantorStorage : IGuarantorStorage
|
|||
|
{
|
|||
|
public GuarantorViewModel? Delete(GuarantorBindingModel model)
|
|||
|
{
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
var newGuarantor = context.Guarantors.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (newGuarantor == null)
|
|||
|
return null;
|
|||
|
context.Guarantors.Remove(newGuarantor);
|
|||
|
context.SaveChanges();
|
|||
|
return newGuarantor.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public GuarantorViewModel? GetElement(GuarantorSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login)) { return null; }
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
return context.Guarantors.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Login) && x.Login.Equals(model.Login)))?.GetViewModel; ;
|
|||
|
}
|
|||
|
|
|||
|
public List<GuarantorViewModel> GetFilteredList(GuarantorSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login))
|
|||
|
return new();
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
if (model.Id.HasValue)
|
|||
|
{
|
|||
|
return context.Guarantors.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return context.Guarantors.Where(x => x.Login.Equals(model.Login)).Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public List<GuarantorViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
return context.Guarantors.Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public GuarantorViewModel? Insert(GuarantorBindingModel model)
|
|||
|
{
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
var newGuarantor = Guarantor.Create(model);
|
|||
|
if (newGuarantor == null)
|
|||
|
return null;
|
|||
|
context.Guarantors.Add(newGuarantor);
|
|||
|
context.SaveChanges();
|
|||
|
return newGuarantor.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public GuarantorViewModel? Update(GuarantorBindingModel model)
|
|||
|
{
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
var newGuarantor = context.Guarantors.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (newGuarantor == null)
|
|||
|
return null;
|
|||
|
newGuarantor.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
return newGuarantor.GetViewModel;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|