2023-04-06 21:56:38 +04:00
|
|
|
|
using HospitalContracts.BindingModels;
|
|
|
|
|
using HospitalContracts.SearchModels;
|
|
|
|
|
using HospitalContracts.StoragesContracts;
|
|
|
|
|
using HospitalContracts.ViewModels;
|
|
|
|
|
using HospitalDataBaseImplements.Models;
|
2023-04-07 00:53:55 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-04-06 21:56:38 +04:00
|
|
|
|
using System;
|
2023-04-05 23:24:58 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace HospitalDataBaseImplements.Implements
|
|
|
|
|
{
|
2023-04-06 21:56:38 +04:00
|
|
|
|
public class RecipesStorage : IRecipesStorage
|
2023-04-05 23:24:58 +04:00
|
|
|
|
{
|
2023-04-06 21:56:38 +04:00
|
|
|
|
public RecipesViewModel? Delete(RecipesBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new HospitalDatabase();
|
2023-05-20 06:49:33 +04:00
|
|
|
|
var element = context.Recipes.Include(x => x.Procedures).FirstOrDefault(rec => rec.Id == model.Id);
|
2023-04-06 21:56:38 +04:00
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Recipes.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RecipesViewModel? GetElement(RecipesSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new HospitalDatabase();
|
2023-05-20 06:49:33 +04:00
|
|
|
|
return context.Recipes.
|
|
|
|
|
Include(x => x.Client).
|
|
|
|
|
Include(x => x.Procedures).
|
|
|
|
|
ThenInclude(x => x.Procedure).
|
|
|
|
|
Include(x => x.Medicines).
|
|
|
|
|
FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?
|
|
|
|
|
.GetViewModel;
|
2023-04-06 21:56:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<RecipesViewModel> GetFilteredList(RecipesSearchModel model)
|
|
|
|
|
{
|
2023-05-20 06:49:33 +04:00
|
|
|
|
if (model is null)
|
2023-04-06 21:56:38 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new HospitalDatabase();
|
2023-05-20 06:49:33 +04:00
|
|
|
|
|
|
|
|
|
return context.Recipes.
|
|
|
|
|
Include(x => x.Procedures).
|
|
|
|
|
ThenInclude(x => x.Procedure)
|
2023-04-07 11:31:56 +04:00
|
|
|
|
.Include(x => x.Medicines)
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Where(x => x.ClientId == model.ClientId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-06 21:56:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<RecipesViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new HospitalDatabase();
|
2023-05-20 06:49:33 +04:00
|
|
|
|
return context.Recipes.Include(x => x.Procedures).
|
|
|
|
|
ThenInclude(x => x.Procedure)
|
|
|
|
|
.Include(x => x.Medicines)
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-06 21:56:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RecipesViewModel? Insert(RecipesBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new HospitalDatabase();
|
2023-05-20 06:49:33 +04:00
|
|
|
|
var newDrugCourse = Recipes.Create(model);
|
|
|
|
|
if (newDrugCourse == null)
|
2023-04-06 21:56:38 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-05-20 06:49:33 +04:00
|
|
|
|
context.Recipes.Add(newDrugCourse);
|
2023-04-06 21:56:38 +04:00
|
|
|
|
context.SaveChanges();
|
2023-05-20 06:49:33 +04:00
|
|
|
|
return context.Recipes
|
|
|
|
|
.Include(x => x.Procedures)
|
|
|
|
|
.ThenInclude(x => x.Procedure)
|
|
|
|
|
.Include(x => x.Medicines)
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.FirstOrDefault(x => x.Id == newDrugCourse.Id)
|
|
|
|
|
?.GetViewModel;
|
2023-04-06 21:56:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RecipesViewModel? Update(RecipesBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new HospitalDatabase();
|
|
|
|
|
var recipe = context.Recipes.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (recipe == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
recipe.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return recipe.GetViewModel;
|
|
|
|
|
}
|
2023-04-05 23:24:58 +04:00
|
|
|
|
}
|
|
|
|
|
}
|