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

88 lines
2.9 KiB
C#
Raw Normal View History

using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDataBaseImplements.Models;
2023-05-20 06:49:33 +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 SymptomsStorage : ISymptomsStorage
2023-04-05 23:24:58 +04:00
{
public List<SymptomsViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Symptomses
.Select(x => x.GetViewModel)
.ToList();
}
public List<SymptomsViewModel> 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;
}
2023-04-05 23:24:58 +04:00
}
}