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

87 lines
2.9 KiB
C#

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 ProceduresStorage : IProceduresStorage
{
public List<ProceduresViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Procedures
.Select(x => x.GetViewModel)
.ToList();
}
public List<ProceduresViewModel> GetFilteredList(ProceduresSearchModel model)
{
if (string.IsNullOrEmpty(model.ProceduresName))
{
return new();
}
using var context = new HospitalDatabase();
return context.Procedures
.Where(x => x.ProceduresName.Contains(model.ProceduresName))
.Select(x => x.GetViewModel)
.ToList();
}
public ProceduresViewModel? GetElement(ProceduresSearchModel model)
{
if (string.IsNullOrEmpty(model.ProceduresName) && !model.Id.HasValue)
{
return null;
}
using var context = new HospitalDatabase();
return context.Procedures
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ProceduresName) && x.ProceduresName ==
model.ProceduresName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ProceduresViewModel? Insert(ProceduresBindingModel model)
{
var newProcedures = Procedures.Create(model);
if (newProcedures == null)
{
return null;
}
using var context = new HospitalDatabase();
context.Procedures.Add(newProcedures);
context.SaveChanges();
return newProcedures.GetViewModel;
}
public ProceduresViewModel? Update(ProceduresBindingModel model)
{
using var context = new HospitalDatabase();
var procedure = context.Procedures.FirstOrDefault(x => x.Id == model.Id);
if (procedure == null)
{
return null;
}
procedure.Update(model);
context.SaveChanges();
return procedure.GetViewModel;
}
public ProceduresViewModel? Delete(ProceduresBindingModel model)
{
using var context = new HospitalDatabase();
var element = context.Procedures.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Procedures.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}