using HospitalContracts.BindingModels; using HospitalContracts.SearchModels; using HospitalContracts.StoragesContracts; using HospitalContracts.ViewModels; using HospitalDataBaseImplements.Models; 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.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(RecipesSearchModel model) { if (!model.Id.HasValue) { return new(); } using var context = new HospitalDatabase(); return context.Recipes.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); } public List GetFullList() { using var context = new HospitalDatabase(); return context.Recipes.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; } } }