From 4bf51abf2694ec98640f9102e2a8f6919e1584da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=91=D0=B0=D0=BA=D0=B0?= =?UTF-8?q?=D0=BB=D1=8C=D1=81=D0=BA=D0=B0=D1=8F?= Date: Mon, 29 Apr 2024 21:10:25 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D0=BA=D0=B0?= =?UTF-8?q?=D0=BA=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=D0=B0=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8E=20?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BC=D0=B5=D0=B4=D0=B8=D0=BA=D0=B0=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/MedicamentStorage.cs | 93 ++++++++++++++++++- .../Models/Medicament.cs | 5 +- .../PolyclinicDatabase.cs | 1 + 3 files changed, 97 insertions(+), 2 deletions(-) diff --git a/Polyclinic/PolyclinicDatabaseImplement/Implements/MedicamentStorage.cs b/Polyclinic/PolyclinicDatabaseImplement/Implements/MedicamentStorage.cs index 1cafe40..851d7d3 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Implements/MedicamentStorage.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Implements/MedicamentStorage.cs @@ -1,8 +1,99 @@ -using PolyclinicContracts.StoragesContracts; +using Microsoft.EntityFrameworkCore; +using PolyclinicContracts.BindingModels; +using PolyclinicContracts.SearchModels; +using PolyclinicContracts.StoragesContracts; +using PolyclinicContracts.ViewModels; +using PolyclinicDatabaseImplement.Models; +using SecuritySystemDatabaseImplement; namespace PolyclinicDatabaseImplement.Implements { public class MedicamentStorage : IMedicamentStorage { + public List GetFullList() + { + using var context = new PolyclinicDatabase(); + return context.Medicaments + .Include(x => x.Symptom) + .Include(x => x.Procedure) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(MedicamentSearchModel model) + { + using var context = new PolyclinicDatabase(); + + if (!model.Id.HasValue || !string.IsNullOrEmpty(model.Name)) + { + return new(); + } + + return context.Medicaments + .Where(x => x.Id == model.Id || model.Name == x.Name) + .Include(x => x.Symptom) + .Include(x => x.Procedure) + .Select(x => x.GetViewModel) + .ToList(); + } + + public MedicamentViewModel? GetElement(MedicamentSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new PolyclinicDatabase(); + return context.Medicaments + .Include(x => x.Symptom) + .Include(x => x.Procedure) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public MedicamentViewModel? Insert(MedicamentBindingModel model) + { + using var context = new PolyclinicDatabase(); + + var newMedicament = Medicament.Create(model); + if(newMedicament == null) + { + return null; + } + context.Medicaments.Add(newMedicament); + context.SaveChanges(); + return context.Medicaments + .Include(x => x.Symptom) + .Include(x => x.Procedure) + .FirstOrDefault(x => x.Id == newMedicament.Id)?.GetViewModel; + } + + public MedicamentViewModel? Update(MedicamentBindingModel model) + { + using var context = new PolyclinicDatabase(); + var medicament = context.Medicaments.FirstOrDefault(x => x.Id == model.Id); + if (medicament == null) + { + return null; + } + medicament.Update(model); + context.SaveChanges(); + return context.Medicaments + .Include(x => x.Symptom) + .Include(x => x.Procedure) + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public MedicamentViewModel? Delete(MedicamentBindingModel model) + { + using var context = new PolyclinicDatabase(); + var element = context.Medicaments.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Medicaments.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Medicament.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Medicament.cs index 5655caf..9e1b33b 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Models/Medicament.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Medicament.cs @@ -22,7 +22,10 @@ namespace PolyclinicDatabaseImplement.Models [Required] public int SymptomId { get; set; } - public static Medicament Create(PolyclinicDatabase database, MedicamentBindingModel bindingModel) + public virtual Symptom? Symptom { get; set; } + public virtual Procedure? Procedure { get; set; } + + public static Medicament Create(MedicamentBindingModel bindingModel) { return new Medicament() { diff --git a/Polyclinic/PolyclinicDatabaseImplement/PolyclinicDatabase.cs b/Polyclinic/PolyclinicDatabaseImplement/PolyclinicDatabase.cs index 79eaaaf..8e062d2 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/PolyclinicDatabase.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/PolyclinicDatabase.cs @@ -19,6 +19,7 @@ namespace SecuritySystemDatabaseImplement public virtual DbSet CourseDiagnoses { set; get; } public virtual DbSet Recipes { set; get; } public virtual DbSet Procedures { set; get; } + public virtual DbSet Medicaments { set; get; } public virtual DbSet ProcedureRecipes { set; get; } } }