From 1cf3c92d5aecdfa0878c88e15af91b26b6243253 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: Sun, 28 Apr 2024 22:42:48 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8E=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0-=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5=D0=B4=D1=83=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ProcedureStorage.cs | 98 ++++++++++++++++++- .../Implements/RecipeStorage.cs | 10 +- 2 files changed, 99 insertions(+), 9 deletions(-) diff --git a/Polyclinic/PolyclinicDatabaseImplement/Implements/ProcedureStorage.cs b/Polyclinic/PolyclinicDatabaseImplement/Implements/ProcedureStorage.cs index 0930c6a..43f51f6 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Implements/ProcedureStorage.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Implements/ProcedureStorage.cs @@ -1,4 +1,10 @@ -using PolyclinicContracts.StoragesContracts; +using Microsoft.EntityFrameworkCore; +using PolyclinicContracts.BindingModels; +using PolyclinicContracts.SearchModels; +using PolyclinicContracts.StoragesContracts; +using PolyclinicContracts.ViewModels; +using PolyclinicDatabaseImplement.Models; +using SecuritySystemDatabaseImplement; using System; using System.Collections.Generic; using System.Linq; @@ -9,5 +15,95 @@ namespace PolyclinicDatabaseImplement.Implements { public class ProcedureStorage : IProcedureStorage { + public List GetFullList() + { + using var database = new PolyclinicDatabase(); + return database.Procedures + .Include(x => x.Recipes) + .ThenInclude(x => x.Recipe) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ProcedureSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var database = new PolyclinicDatabase(); + return database.Procedures + .Include(x => x.Recipes) + .ThenInclude(x => x.Recipe) + .Where(x => x.Name == model.Name) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ProcedureViewModel? GetElement(ProcedureSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && + !model.Id.HasValue) + { + return null; + } + using var database = new PolyclinicDatabase(); + return database.Procedures + .Include(x => x.Recipes) + .ThenInclude(x => x.Recipe) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ProcedureViewModel? Insert(ProcedureBindingModel model) + { + using var database = new PolyclinicDatabase(); + var newProcedure = Procedure.Create(database, model); + if (newProcedure == null) + { + return null; + } + database.Procedures.Add(newProcedure); + database.SaveChanges(); + return newProcedure.GetViewModel; + } + public ProcedureViewModel? Update(ProcedureBindingModel model) + { + using var database = new PolyclinicDatabase(); + using var transaction = database.Database.BeginTransaction(); + try + { + var Procedure = database.Procedures.FirstOrDefault(rec => rec.Id == model.Id); + if (Procedure == null) + { + return null; + } + Procedure.Update(model); + database.SaveChanges(); + Procedure.UpdateRecipes(database, model); + transaction.Commit(); + return Procedure.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ProcedureViewModel? Delete(ProcedureBindingModel model) + { + using var database = new PolyclinicDatabase(); + var element = database.Procedures + .Include(x => x.Recipes) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + database.Procedures.Remove(element); + database.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/Polyclinic/PolyclinicDatabaseImplement/Implements/RecipeStorage.cs b/Polyclinic/PolyclinicDatabaseImplement/Implements/RecipeStorage.cs index 1ee027d..c9d0583 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Implements/RecipeStorage.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Implements/RecipeStorage.cs @@ -1,15 +1,9 @@ -using Microsoft.EntityFrameworkCore; -using PolyclinicContracts.BindingModels; +using PolyclinicContracts.BindingModels; using PolyclinicContracts.SearchModels; using PolyclinicContracts.StoragesContracts; using PolyclinicContracts.ViewModels; using PolyclinicDatabaseImplement.Models; using SecuritySystemDatabaseImplement; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace PolyclinicDatabaseImplement.Implements { @@ -41,7 +35,7 @@ namespace PolyclinicDatabaseImplement.Implements x.Comment == bindingModel.Comment) || (bindingModel.Id.HasValue && x.Id == bindingModel.Id))?.GetViewModel; } - public RecipeViewModel Insert(RecipeBindingModel bindingModel) + public RecipeViewModel? Insert(RecipeBindingModel bindingModel) { using var database = new PolyclinicDatabase(); var newRecipe = Recipe.Create(bindingModel);