сделала реализацию класса-хранилища для рецепта

This commit is contained in:
Елена Бакальская 2024-04-28 21:41:17 +04:00
parent 1de7afe627
commit e4030ac2e2
3 changed files with 73 additions and 3 deletions

View File

@ -3,6 +3,6 @@
public class RecipeSearchModel
{
public int? Id { get; set; }
public string? Name { get; set; }
public string? Comment { get; set; }
}
}

View File

@ -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<RecipeViewModel> GetFullList()
{
using var database = new PolyclinicDatabase();
return database.Recipes.Select(x => x.GetViewModel).ToList();
}
public List<RecipeViewModel> 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;
}
}
}

View File

@ -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()
{