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 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.DateStopDiagnose <= model.To.Value).ToList(); } return elements; } public List 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; } } }