diff --git a/Polyclinic/PolyclinicDatabaseImplement/Implements/ProcedureStorage.cs b/Polyclinic/PolyclinicDatabaseImplement/Implements/ProcedureStorage.cs index c596909..0194d9d 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Implements/ProcedureStorage.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Implements/ProcedureStorage.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.IdentityModel.Tokens; using PolyclinicContracts.BindingModels; using PolyclinicContracts.SearchModels; using PolyclinicContracts.StoragesContracts; @@ -21,34 +22,37 @@ namespace PolyclinicDatabaseImplement.Implements public List GetFilteredList(ProcedureSearchModel model) { - if (!model.Id.HasValue && !model.From.HasValue && !model.To.HasValue && !model.UserId.HasValue) + var elements = GetFullList(); + if (model.Id != null) { - return new(); + elements = elements.Where(x => x.Id == model.Id.Value).ToList(); } - if (string.IsNullOrEmpty(model.Name)) + if (model.UserId != null) { - return new(); + elements = elements.Where(x => x.UserId == model.UserId.Value).ToList(); } - using var database = new PolyclinicDatabase(); - return database.Procedures - .Where(x => x.Id == model.Id || x.Name == model.Name || x.DateStartProcedure <= model.To) - .Include(p => p.User) - .Select(x => x.GetViewModel) - .ToList(); + if (!model.Name.IsNullOrEmpty()) + { + elements = elements.Where(x => x.Name == model.Name).ToList(); + } + if (model.From != null) + { + elements = elements.Where(x => x.DateStartProcedure >= model.From.Value).ToList(); + } + if (model.To != null) + { + elements = elements + .Where(x => x.DateStartProcedure <= model.To.Value) + .Where(x => x.DateStopProcedure != null) + .Where(x => x.DateStopProcedure <= model.To.Value) + .ToList(); + } + return elements; } public ProcedureViewModel? GetElement(ProcedureSearchModel model) { - if (string.IsNullOrEmpty(model.Name) && - !model.Id.HasValue) - { - return null; - } - using var database = new PolyclinicDatabase(); - return database.Procedures - .Include(p => p.User) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return GetFilteredList(model).FirstOrDefault(); } public ProcedureViewModel? Insert(ProcedureBindingModel model)