83 lines
2.7 KiB
C#
83 lines
2.7 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;
|
|
|
|
namespace HospitalDataBaseImplements.Implements
|
|
{
|
|
public class RecipesStorage : IRecipesStorage
|
|
{
|
|
public RecipesViewModel? Delete(RecipesBindingModel model)
|
|
{
|
|
using var context = new HospitalDatabase();
|
|
var element = context.Recipes.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).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
}
|
|
|
|
public List<RecipesViewModel> GetFilteredList(RecipesSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new HospitalDatabase();
|
|
return context.Recipes.Include(x => x.Client).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<RecipesViewModel> GetFullList()
|
|
{
|
|
using var context = new HospitalDatabase();
|
|
return context.Recipes.Include(x => x.Client).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public RecipesViewModel? Insert(RecipesBindingModel model)
|
|
{
|
|
using var context = new HospitalDatabase();
|
|
var newRecipe = Recipes.Create(context, model);
|
|
if (newRecipe == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Recipes.Add(newRecipe);
|
|
context.SaveChanges();
|
|
return newRecipe.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;
|
|
}
|
|
}
|
|
}
|