PIbd-22_CourseWork_Hospital.../Polyclinic/HospitalDatabaseImplement/Implements/SymptomStorage.cs

81 lines
2.7 KiB
C#
Raw Normal View History

2024-05-01 23:49:06 +04:00
using Microsoft.EntityFrameworkCore;
using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDatabaseImplement.Models;
using SecuritySystemDatabaseImplement;
namespace HospitalDatabaseImplement.Implements
{
public class SymptomStorage : ISymptomStorage
{
public SymptomViewModel? Delete(SymptomBindingModel model)
{
using var context = new HospitalDatabase();
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 HospitalDatabase();
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 HospitalDatabase();
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 HospitalDatabase();
var element = context.Symptomes.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
element.Update(model);
context.SaveChanges();
return element.GetViewModel;
}
}
}