140 lines
4.9 KiB
C#
140 lines
4.9 KiB
C#
using HospitalContracts.BindingModels;
|
||
using HospitalContracts.SearchModels;
|
||
using HospitalContracts.StoragesContracts;
|
||
using HospitalContracts.ViewModels;
|
||
using HospitalDataBaseImplements.Models;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Xml.Linq;
|
||
|
||
namespace HospitalDataBaseImplements.Implements
|
||
{
|
||
public class RecipesStorage : IRecipesStorage
|
||
{
|
||
public RecipesViewModel? Delete(RecipesBindingModel model)
|
||
{
|
||
using var context = new HospitalDatabase();
|
||
var element = context.Recipes.Include(x => x.Procedures).ThenInclude(x => x.Procedure).Include(x => x.Symptoms)
|
||
.Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id);
|
||
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();
|
||
return context.Recipes.
|
||
Include(x => x.Client).
|
||
Include(x => x.Procedures).
|
||
ThenInclude(x => x.Procedure).
|
||
Include(x => x.Symptoms).
|
||
FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?
|
||
.GetViewModel;
|
||
}
|
||
//НУЖЕН ТУТ ЕЩЕ ОДИН ТУ ЛИСТ???
|
||
public List<RecipesViewModel> GetFilteredList(RecipesSearchModel model)
|
||
{
|
||
if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue)
|
||
{
|
||
return new();
|
||
}
|
||
if (model is null)
|
||
{
|
||
return new();
|
||
}
|
||
using var context = new HospitalDatabase();
|
||
if (model.DateFrom.HasValue)
|
||
{
|
||
return context.Recipes.
|
||
Include(x => x.Procedures).
|
||
ThenInclude(x => x.Procedure)
|
||
.Include(x => x.Symptoms)
|
||
.Include(x => x.Client)
|
||
.Where(x => x.Date >= model.DateFrom && x.Date <= model.DateTo && x.ClientId == model.ClientId)
|
||
.Where(x => x.ClientId == model.ClientId).ToList()
|
||
.Select(x => x.GetViewModel)
|
||
.ToList();
|
||
}
|
||
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();
|
||
}
|
||
}
|
||
|
||
public List<RecipesViewModel> GetFullList()
|
||
{
|
||
using var context = new HospitalDatabase();
|
||
return context.Recipes.Include(x => x.Procedures).
|
||
ThenInclude(x => x.Procedure)
|
||
.Include(x => x.Client)
|
||
.Include(x => x.Symptoms)
|
||
.ToList()
|
||
.Select(x => x.GetViewModel)
|
||
.ToList();
|
||
}
|
||
|
||
public RecipesViewModel? Insert(RecipesBindingModel model)
|
||
{
|
||
using var context = new HospitalDatabase();
|
||
var newRecipe = Recipes.Create(model);
|
||
if (newRecipe == null)
|
||
{
|
||
return null;
|
||
}
|
||
context.Recipes.Add(newRecipe);
|
||
context.SaveChanges();
|
||
return context.Recipes
|
||
.Include(x => x.Procedures)
|
||
.ThenInclude(x => x.Procedure)
|
||
.Include(x => x.Symptoms)
|
||
.Include(x => x.Client)
|
||
.FirstOrDefault(x => x.Id == newRecipe.Id)
|
||
?.GetViewModel;
|
||
}
|
||
|
||
public RecipesViewModel? Update(RecipesBindingModel model)
|
||
{
|
||
using var context = new HospitalDatabase();
|
||
using var transaction = context.Database.BeginTransaction();
|
||
try
|
||
{
|
||
var recipe = context.Recipes.FirstOrDefault(x => x.Id == model.Id);
|
||
if (recipe == null)
|
||
{
|
||
return null;
|
||
}
|
||
recipe.Update(model);
|
||
context.SaveChanges();
|
||
if (model.RecipeProcedures != null)
|
||
recipe.UpdateProcedures(context, model);
|
||
transaction.Commit();
|
||
return recipe.GetViewModel;
|
||
}
|
||
catch
|
||
{
|
||
transaction.Rollback();
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
}
|