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;
|
2023-06-03 15:50:25 +04:00
|
|
|
|
using System.Xml.Linq;
|
2023-04-05 23:24:58 +04:00
|
|
|
|
|
|
|
|
|
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-06-19 13:29:55 +04:00
|
|
|
|
var element = context.Recipes.Include(x => x.Procedures).ThenInclude(x => x.Procedure).Include(x => x.Symptoms)
|
2023-06-03 15:50:25 +04:00
|
|
|
|
.Include(x => x.Client).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).
|
2023-06-03 15:50:25 +04:00
|
|
|
|
Include(x => x.Symptoms).
|
2023-05-20 06:49:33 +04:00
|
|
|
|
FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?
|
|
|
|
|
.GetViewModel;
|
2023-04-06 21:56:38 +04:00
|
|
|
|
}
|
2023-06-03 15:50:25 +04:00
|
|
|
|
//НУЖЕН ТУТ ЕЩЕ ОДИН ТУ ЛИСТ???
|
2023-04-06 21:56:38 +04:00
|
|
|
|
public List<RecipesViewModel> GetFilteredList(RecipesSearchModel model)
|
|
|
|
|
{
|
2023-06-21 23:38:26 +04:00
|
|
|
|
if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
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-06-21 23:38:26 +04:00
|
|
|
|
if (model.DateFrom.HasValue)
|
|
|
|
|
{
|
2023-05-20 06:49:33 +04:00
|
|
|
|
return context.Recipes.
|
|
|
|
|
Include(x => x.Procedures).
|
|
|
|
|
ThenInclude(x => x.Procedure)
|
2023-06-03 15:50:25 +04:00
|
|
|
|
.Include(x => x.Symptoms)
|
2023-04-07 11:31:56 +04:00
|
|
|
|
.Include(x => x.Client)
|
2023-06-21 23:38:26 +04:00
|
|
|
|
.Where(x => x.Date >= model.DateFrom && x.Date <= model.DateTo && x.ClientId == model.ClientId)
|
2023-06-03 15:50:25 +04:00
|
|
|
|
.Where(x => x.ClientId == model.ClientId).ToList()
|
2023-04-07 11:31:56 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-06-21 23:38:26 +04:00
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
return context.Recipes.
|
|
|
|
|
Include(x => x.Procedures).
|
|
|
|
|
ThenInclude(x => x.Procedure)
|
|
|
|
|
.Include(x => x.Symptoms)
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Where(x => x.ClientId == model.ClientId).ToList()
|
|
|
|
|
.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.Client)
|
2023-06-03 15:50:25 +04:00
|
|
|
|
.Include(x => x.Symptoms)
|
|
|
|
|
.ToList()
|
2023-05-20 06:49:33 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-06 21:56:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RecipesViewModel? Insert(RecipesBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new HospitalDatabase();
|
2023-06-03 15:50:25 +04:00
|
|
|
|
var newRecipe = Recipes.Create(model);
|
|
|
|
|
if (newRecipe == null)
|
2023-04-06 21:56:38 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-06-03 15:50:25 +04:00
|
|
|
|
context.Recipes.Add(newRecipe);
|
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)
|
2023-06-03 15:50:25 +04:00
|
|
|
|
.Include(x => x.Symptoms)
|
2023-05-20 06:49:33 +04:00
|
|
|
|
.Include(x => x.Client)
|
2023-06-03 15:50:25 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == newRecipe.Id)
|
2023-05-20 06:49:33 +04:00
|
|
|
|
?.GetViewModel;
|
2023-04-06 21:56:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RecipesViewModel? Update(RecipesBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new HospitalDatabase();
|
2023-06-03 15:50:25 +04:00
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var recipe = context.Recipes.FirstOrDefault(x => x.Id == model.Id);
|
2023-04-06 21:56:38 +04:00
|
|
|
|
if (recipe == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
recipe.Update(model);
|
|
|
|
|
context.SaveChanges();
|
2023-06-19 13:29:55 +04:00
|
|
|
|
if (model.RecipeProcedures != null)
|
|
|
|
|
recipe.UpdateProcedures(context, model);
|
2023-06-03 15:50:25 +04:00
|
|
|
|
transaction.Commit();
|
|
|
|
|
return recipe.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2023-04-06 21:56:38 +04:00
|
|
|
|
}
|
2023-04-05 23:24:58 +04:00
|
|
|
|
}
|
|
|
|
|
}
|