From e4030ac2e24d874991564fd4219f43d09e2214f6 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 21:41:17 +0400 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=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?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0-=D1=85=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D1=80?= =?UTF-8?q?=D0=B5=D1=86=D0=B5=D0=BF=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/RecipeSearchModel.cs | 2 +- .../Implements/RecipeStorage.cs | 72 ++++++++++++++++++- .../Models/Recipe.cs | 2 +- 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/Polyclinic/PolyclinicContracts/SearchModels/RecipeSearchModel.cs b/Polyclinic/PolyclinicContracts/SearchModels/RecipeSearchModel.cs index 4105c94..2679649 100644 --- a/Polyclinic/PolyclinicContracts/SearchModels/RecipeSearchModel.cs +++ b/Polyclinic/PolyclinicContracts/SearchModels/RecipeSearchModel.cs @@ -3,6 +3,6 @@ public class RecipeSearchModel { public int? Id { get; set; } - public string? Name { get; set; } + public string? Comment { get; set; } } } \ No newline at end of file diff --git a/Polyclinic/PolyclinicDatabaseImplement/Implements/RecipeStorage.cs b/Polyclinic/PolyclinicDatabaseImplement/Implements/RecipeStorage.cs index 4e3f643..1ee027d 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Implements/RecipeStorage.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Implements/RecipeStorage.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,69 @@ namespace PolyclinicDatabaseImplement.Implements { public class RecipeStorage : IRecipeStorage { + public List GetFullList() + { + using var database = new PolyclinicDatabase(); + return database.Recipes.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(RecipeSearchModel bindingModel) + { + if (!bindingModel.Id.HasValue || string.IsNullOrEmpty(bindingModel.Comment)) + { + return new(); + } + using var database = new PolyclinicDatabase(); + return database.Recipes.Where(x => x.Comment.Contains(bindingModel.Comment)).Select(x => x.GetViewModel).ToList(); + } + + public RecipeViewModel? GetElement(RecipeSearchModel bindingModel) + { + if (!bindingModel.Id.HasValue || string.IsNullOrEmpty(bindingModel.Comment)) + { + return null; + } + using var database = new PolyclinicDatabase(); + return database.Recipes.FirstOrDefault(x => (!string.IsNullOrEmpty(bindingModel.Comment) && + x.Comment == bindingModel.Comment) || (bindingModel.Id.HasValue && x.Id == bindingModel.Id))?.GetViewModel; + } + + public RecipeViewModel Insert(RecipeBindingModel bindingModel) + { + using var database = new PolyclinicDatabase(); + var newRecipe = Recipe.Create(bindingModel); + if(newRecipe == null) + { + return null; + } + database.Recipes.Add(newRecipe); + database.SaveChanges(); + return newRecipe.GetViewModel; + } + + public RecipeViewModel? Update(RecipeBindingModel bindingModel) + { + using var database = new PolyclinicDatabase(); + var recipe = database.Recipes.FirstOrDefault(x => x.Id == bindingModel.Id); + if(recipe == null) + { + return null; + } + recipe.Update(bindingModel); + database.SaveChanges(); + return recipe.GetViewModel; + } + + public RecipeViewModel? Delete(RecipeBindingModel bindingModel) + { + using var database = new PolyclinicDatabase(); + var recipe = database.Recipes.FirstOrDefault(x => x.Id == bindingModel.Id); + if (recipe == null) + { + return null; + } + database.Recipes.Remove(recipe); + database.SaveChanges(); + return recipe.GetViewModel; + } } } diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Recipe.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Recipe.cs index 7641782..0648311 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Models/Recipe.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Recipe.cs @@ -16,7 +16,7 @@ namespace PolyclinicDatabaseImplement.Models [Required] public string Comment { get; set; } = string.Empty; - public static Recipe Create(PolyclinicDatabase database, RecipeBindingModel bindingModel) + public static Recipe Create( RecipeBindingModel bindingModel) { return new Recipe() {