Rogashova_E.A._CourseWork_H.../HospitalDataBaseImplements/Implements/RecipesStorage.cs

140 lines
4.9 KiB
C#
Raw Normal View History

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;
using System;
2023-04-05 23:24:58 +04:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
2023-04-05 23:24:58 +04:00
namespace HospitalDataBaseImplements.Implements
{
public class RecipesStorage : IRecipesStorage
2023-04-05 23:24:58 +04:00
{
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();
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.Symptoms).
2023-05-20 06:49:33 +04:00
FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
//НУЖЕН ТУТ ЕЩЕ ОДИН ТУ ЛИСТ???
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)
{
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)
.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)
.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();
}
}
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)
.Include(x => x.Symptoms)
.ToList()
2023-05-20 06:49:33 +04:00
.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();
2023-05-20 06:49:33 +04:00
return context.Recipes
.Include(x => x.Procedures)
.ThenInclude(x => x.Procedure)
.Include(x => x.Symptoms)
2023-05-20 06:49:33 +04:00
.Include(x => x.Client)
.FirstOrDefault(x => x.Id == newRecipe.Id)
2023-05-20 06:49:33 +04:00
?.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;
}
}
2023-04-05 23:24:58 +04:00
}
}