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

108 lines
3.6 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;
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();
2023-05-20 06:49:33 +04:00
var element = context.Recipes.Include(x => x.Procedures).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.Medicines).
FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public List<RecipesViewModel> GetFilteredList(RecipesSearchModel model)
{
2023-05-20 06:49:33 +04:00
if (model is null)
{
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();
}
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();
}
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)
{
return null;
}
2023-05-20 06:49:33 +04:00
context.Recipes.Add(newDrugCourse);
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;
}
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
}
}