PIbd-21_CourseWork_Polyclin.../Polyclinic/PolyclinicDatabaseImplement/Implements/DiagnoseStorage.cs

80 lines
2.6 KiB
C#

using Microsoft.EntityFrameworkCore;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.SearchModels;
using PolyclinicContracts.StoragesContracts;
using PolyclinicContracts.ViewModels;
using PolyclinicDatabaseImplement.Models;
using SecuritySystemDatabaseImplement;
namespace PolyclinicDatabaseImplement.Implements
{
public class DiagnoseStorage : IDiagnoseStorage
{
public DiagnoseViewModel? Delete(DiagnoseBindingModel model)
{
using var context = new PolyclinicDatabase();
var element = context.Diagnoses.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Diagnoses.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public DiagnoseViewModel? GetElement(DiagnoseSearchModel model)
{
return GetFilteredList(model).FirstOrDefault();
}
public List<DiagnoseViewModel> GetFilteredList(DiagnoseSearchModel model)
{
var elements = GetFullList();
foreach (var prop in model.GetType().GetProperties())
{
if (model.GetType().GetProperty(prop.Name)?.GetValue(model, null) != null)
{
elements = elements.Where(x => x.GetType().GetProperty(prop.Name)?.GetValue(x, null) == model.GetType().GetProperty(prop.Name)?.GetValue(model, null)).ToList();
}
}
return elements;
}
public List<DiagnoseViewModel> GetFullList()
{
using var context = new PolyclinicDatabase();
return context.Diagnoses
.Include(x => x.User)
.Select(x => x.GetViewModel)
.ToList();
}
public DiagnoseViewModel? Insert(DiagnoseBindingModel model)
{
var element = Diagnose.Create(model);
if (element == null)
{
return null;
}
using var context = new PolyclinicDatabase();
context.Diagnoses.Add(element);
context.SaveChanges();
return element.GetViewModel;
}
public DiagnoseViewModel? Update(DiagnoseBindingModel model)
{
using var context = new PolyclinicDatabase();
var element = context.Diagnoses.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
element.Update(model);
context.SaveChanges();
return element.GetViewModel;
}
}
}