PIbd-21_CourseWork_Polyclin.../Polyclinic/PolyclinicDatabaseImplement/Implements/ProcedureStorage.cs

93 lines
3.2 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.SearchModels;
using PolyclinicContracts.StoragesContracts;
using PolyclinicContracts.ViewModels;
using PolyclinicDatabaseImplement.Models;
using SecuritySystemDatabaseImplement;
namespace PolyclinicDatabaseImplement.Implements
{
public class ProcedureStorage : IProcedureStorage
{
public List<ProcedureViewModel> GetFullList()
{
using var database = new PolyclinicDatabase();
return database.Procedures
.Include(p => p.User)
.Select(x => x.GetViewModel)
.ToList();
}
public List<ProcedureViewModel> GetFilteredList(ProcedureSearchModel model)
{
if (!model.Id.HasValue && !model.From.HasValue && !model.To.HasValue && !model.UserId.HasValue)
{
return new();
}
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var database = new PolyclinicDatabase();
return database.Procedures
.Where(x => x.Id == model.Id || x.Name == model.Name || x.DateStartProcedure <= model.To)
.Include(p => p.User)
.Select(x => x.GetViewModel)
.ToList();
}
public ProcedureViewModel? GetElement(ProcedureSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) &&
!model.Id.HasValue)
{
return null;
}
using var database = new PolyclinicDatabase();
return database.Procedures
.Include(p => p.User)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ProcedureViewModel? Insert(ProcedureBindingModel model)
{
using var database = new PolyclinicDatabase();
var newProcedure = Procedure.Create(model);
if (newProcedure == null)
{
return null;
}
database.Procedures.Add(newProcedure);
database.SaveChanges();
return newProcedure.GetViewModel;
}
public ProcedureViewModel? Update(ProcedureBindingModel model)
{
using var database = new PolyclinicDatabase();
var procedure = database.Procedures.FirstOrDefault(x => x.Id == model.Id);
if (procedure == null)
{
return null;
}
procedure.Update(model);
database.SaveChanges();
return procedure.GetViewModel;
}
public ProcedureViewModel? Delete(ProcedureBindingModel model)
{
using var database = new PolyclinicDatabase();
var element = database.Procedures
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
database.Procedures.Remove(element);
database.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}