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

81 lines
2.7 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 SymptomStorage : ISymptomStorage
{
public SymptomViewModel? Delete(SymptomBindingModel model)
{
using var context = new PolyclinicDatabase();
var element = context.Symptomes.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Symptomes.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public SymptomViewModel? GetElement(SymptomSearchModel model)
{
return GetFilteredList(model).FirstOrDefault();
}
public List<SymptomViewModel> GetFilteredList(SymptomSearchModel 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<SymptomViewModel> GetFullList()
{
using var context = new PolyclinicDatabase();
return context.Symptomes
.Include(x => x.Diagnoses)
.ThenInclude(x => x.Diagnose)
.Select(x => x.GetViewModel)
.ToList();
}
public SymptomViewModel? Insert(SymptomBindingModel model)
{
using var context = new PolyclinicDatabase();
var element = Symptom.Create(context, model);
if (element == null)
{
return null;
}
context.Symptomes.Add(element);
context.SaveChanges();
return element.GetViewModel;
}
public SymptomViewModel? Update(SymptomBindingModel model)
{
using var context = new PolyclinicDatabase();
var element = context.Symptomes.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
element.Update(model);
context.SaveChanges();
return element.GetViewModel;
}
}
}