PIbd-21_CourseWork_Polyclin.../Polyclinic/PolyclinicDatabaseImplement/Implements/RecipeStorage.cs

78 lines
2.8 KiB
C#

using PolyclinicContracts.BindingModels;
using PolyclinicContracts.SearchModels;
using PolyclinicContracts.StoragesContracts;
using PolyclinicContracts.ViewModels;
using PolyclinicDatabaseImplement.Models;
using SecuritySystemDatabaseImplement;
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;
}
}
}