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

105 lines
3.5 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 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();
if (model.Id != null)
{
elements = elements.Where(x => x.Id == model.Id.Value).ToList();
}
if (!model.Name.IsNullOrEmpty())
{
elements = elements.Where(x => x.Name == model.Name).ToList();
}
if (model.UserId != null)
{
elements = elements.Where(x => x.UserId == model.UserId.Value).ToList();
}
if (model.From != null)
{
elements = elements.Where(x => x.DateStartDiagnose >= model.From.Value).ToList();
}
if (model.To != null)
{
elements = elements.Where(x => x.DateStartDiagnose <= model.To.Value).ToList();
var elemWithDateStop = elements
.Where(x => x.DateStopDiagnose != null)
.Where(x => x.DateStopDiagnose <= model.To.Value)
.ToList();
var elemWithoutDateStop = elements.Where(x => x.DateStopDiagnose == null).ToList();
elemWithDateStop.AddRange(elemWithoutDateStop);
elements = elemWithDateStop;
}
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;
}
}
}