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 GetFilteredList(RecipesSearchModel model) { if (model is null) { return new(); } using var context = new HospitalDatabase(); 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 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; } } } }