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 SymptomsStorage : ISymptomsStorage { public List GetFullList() { using var context = new HospitalDatabase(); return context.Symptomses .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(SymptomsSearchModel model) { if (string.IsNullOrEmpty(model.SymptomName)) { return new(); } using var context = new HospitalDatabase(); return context.Symptomses .Where(x => x.SymptomName.Contains(model.SymptomName)) .Select(x => x.GetViewModel) .ToList(); } public SymptomsViewModel? GetElement(SymptomsSearchModel model) { if (string.IsNullOrEmpty(model.SymptomName) && !model.Id.HasValue) { return null; } using var context = new HospitalDatabase(); return context.Symptomses .FirstOrDefault(x => (!string.IsNullOrEmpty(model.SymptomName) && x.SymptomName == model.SymptomName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } public SymptomsViewModel? Insert(SymptomsBindingModel model) { var newSymptoms = Symptoms.Create(model); if (newSymptoms == null) { return null; } using var context = new HospitalDatabase(); context.Symptomses.Add(newSymptoms); context.SaveChanges(); return newSymptoms.GetViewModel; } public SymptomsViewModel? Update(SymptomsBindingModel model) { using var context = new HospitalDatabase(); var symptom = context.Symptomses.FirstOrDefault(x => x.Id == model.Id); if (symptom == null) { return null; } symptom.Update(model); context.SaveChanges(); return symptom.GetViewModel; } public SymptomsViewModel? Delete(SymptomsBindingModel model) { using var context = new HospitalDatabase(); var element = context.Symptomses.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Symptomses.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } } }