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

95 lines
3.0 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
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();
if (model.Id != null)
{
elements = elements.Where(x => x.Id == model.Id).ToList();
}
if (!model.Name.IsNullOrEmpty())
{
elements = elements.Where(x => x.Name == model.Name).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();
using var transaction = context.Database.BeginTransaction();
try
{
var element = context.Symptomes.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
element.Update(model);
context.SaveChanges();
element.UpdateDiagnoses(context, model);
transaction.Commit();
return element.GetViewModel;
}
catch (Exception ex)
{
transaction.Rollback();
throw;
}
}
}
}