84 lines
2.8 KiB
C#
84 lines
2.8 KiB
C#
using SYBDContracts.BindingModels;
|
|
using SYBDContracts.SearchModels;
|
|
using SYBDContracts.StoragesContracts;
|
|
using SYBDContracts.ViewModels;
|
|
using SYBDDatabaseImplement.Models;
|
|
|
|
namespace SYBDDatabaseImplement.Implements
|
|
{
|
|
public class InsuranceStorage : IInsuranceStorage
|
|
{
|
|
public List<InsuranceViewModel> GetFullList()
|
|
{
|
|
using var context = new SYBDDatabase();
|
|
return context.insurance
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<InsuranceViewModel> GetFilteredList(InsuranceSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new SYBDDatabase();
|
|
return context.insurance
|
|
.Where(x => x.Name.Contains(model.Name))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public InsuranceViewModel? GetElement(InsuranceSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SYBDDatabase();
|
|
return context.insurance
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public InsuranceViewModel? Insert(InsuranceBindingModel model)
|
|
{
|
|
var newInsurance = Insurance.Create(model);
|
|
if (newInsurance == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SYBDDatabase();
|
|
context.insurance.Add(newInsurance);
|
|
context.SaveChanges();
|
|
return newInsurance.GetViewModel;
|
|
}
|
|
|
|
public InsuranceViewModel? Update(InsuranceBindingModel model)
|
|
{
|
|
using var context = new SYBDDatabase();
|
|
var component = context.insurance.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
|
|
public InsuranceViewModel? Delete(InsuranceBindingModel model)
|
|
{
|
|
using var context = new SYBDDatabase();
|
|
var element = context.insurance.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.insurance.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
} |