2024-04-28 22:42:48 +04:00
|
|
|
|
using PolyclinicContracts.BindingModels;
|
2024-04-28 21:41:17 +04:00
|
|
|
|
using PolyclinicContracts.SearchModels;
|
|
|
|
|
using PolyclinicContracts.StoragesContracts;
|
|
|
|
|
using PolyclinicContracts.ViewModels;
|
|
|
|
|
using PolyclinicDatabaseImplement.Models;
|
|
|
|
|
using SecuritySystemDatabaseImplement;
|
2024-04-28 20:10:45 +04:00
|
|
|
|
|
|
|
|
|
namespace PolyclinicDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class RecipeStorage : IRecipeStorage
|
|
|
|
|
{
|
2024-04-28 21:41:17 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 22:42:48 +04:00
|
|
|
|
public RecipeViewModel? Insert(RecipeBindingModel bindingModel)
|
2024-04-28 21:41:17 +04:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-04-28 20:10:45 +04:00
|
|
|
|
}
|
|
|
|
|
}
|