2024-04-30 16:20:00 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
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();
|
2024-04-30 16:20:00 +04:00
|
|
|
|
return database.Recipes
|
2024-04-30 22:51:17 +04:00
|
|
|
|
.Include(x => x.Course)
|
2024-04-30 16:20:00 +04:00
|
|
|
|
.Include(x => x.Procedures)
|
|
|
|
|
.ThenInclude(x => x.Procedure)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2024-04-28 21:41:17 +04:00
|
|
|
|
}
|
|
|
|
|
public List<RecipeViewModel> GetFilteredList(RecipeSearchModel bindingModel)
|
|
|
|
|
{
|
|
|
|
|
if (!bindingModel.Id.HasValue || string.IsNullOrEmpty(bindingModel.Comment))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var database = new PolyclinicDatabase();
|
2024-04-30 16:20:00 +04:00
|
|
|
|
return database.Recipes
|
|
|
|
|
.Include(x => x.Procedures)
|
|
|
|
|
.ThenInclude(x => x.Procedure)
|
|
|
|
|
.Where(x => x.Comment.Contains(bindingModel.Comment))
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2024-04-28 21:41:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RecipeViewModel? GetElement(RecipeSearchModel bindingModel)
|
|
|
|
|
{
|
|
|
|
|
if (!bindingModel.Id.HasValue || string.IsNullOrEmpty(bindingModel.Comment))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var database = new PolyclinicDatabase();
|
2024-04-30 16:20:00 +04:00
|
|
|
|
return database.Recipes
|
|
|
|
|
.Include(x => x.Procedures)
|
|
|
|
|
.ThenInclude(x => x.Procedure)
|
|
|
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(bindingModel.Comment) &&
|
|
|
|
|
x.Comment == bindingModel.Comment) ||
|
|
|
|
|
(bindingModel.Id.HasValue && x.Id == bindingModel.Id))?
|
|
|
|
|
.GetViewModel;
|
2024-04-28 21:41:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
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();
|
2024-04-30 16:20:00 +04:00
|
|
|
|
var newRecipe = Recipe.Create(database, bindingModel);
|
|
|
|
|
if (newRecipe == null)
|
2024-04-28 21:41:17 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
database.Recipes.Add(newRecipe);
|
|
|
|
|
database.SaveChanges();
|
|
|
|
|
return newRecipe.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RecipeViewModel? Update(RecipeBindingModel bindingModel)
|
|
|
|
|
{
|
|
|
|
|
using var database = new PolyclinicDatabase();
|
2024-04-30 16:20:00 +04:00
|
|
|
|
using var transaction = database.Database.BeginTransaction();
|
|
|
|
|
try
|
2024-04-28 21:41:17 +04:00
|
|
|
|
{
|
2024-04-30 16:20:00 +04:00
|
|
|
|
var Recipe = database.Recipes.FirstOrDefault(rec => rec.Id == bindingModel.Id);
|
|
|
|
|
if (Recipe == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Recipe.Update(bindingModel);
|
|
|
|
|
database.SaveChanges();
|
|
|
|
|
Recipe.UpdateProcedures(database, bindingModel);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return Recipe.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
2024-04-28 21:41:17 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RecipeViewModel? Delete(RecipeBindingModel bindingModel)
|
|
|
|
|
{
|
|
|
|
|
using var database = new PolyclinicDatabase();
|
2024-04-30 16:20:00 +04:00
|
|
|
|
var recipe = database.Recipes
|
|
|
|
|
.Include(x => x.Procedures)
|
|
|
|
|
.FirstOrDefault(x => x.Id == bindingModel.Id);
|
2024-04-28 21:41:17 +04:00
|
|
|
|
if (recipe == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
database.Recipes.Remove(recipe);
|
|
|
|
|
database.SaveChanges();
|
|
|
|
|
return recipe.GetViewModel;
|
|
|
|
|
}
|
2024-04-28 20:10:45 +04:00
|
|
|
|
}
|
|
|
|
|
}
|