2024-04-28 22:42:48 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using PolyclinicContracts.BindingModels;
|
|
|
|
|
using PolyclinicContracts.SearchModels;
|
|
|
|
|
using PolyclinicContracts.StoragesContracts;
|
|
|
|
|
using PolyclinicContracts.ViewModels;
|
|
|
|
|
using PolyclinicDatabaseImplement.Models;
|
|
|
|
|
using SecuritySystemDatabaseImplement;
|
2024-04-28 20:10:45 +04:00
|
|
|
|
|
|
|
|
|
namespace PolyclinicDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class ProcedureStorage : IProcedureStorage
|
|
|
|
|
{
|
2024-04-28 22:42:48 +04:00
|
|
|
|
public List<ProcedureViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var database = new PolyclinicDatabase();
|
|
|
|
|
return database.Procedures
|
|
|
|
|
.Include(x => x.Recipes)
|
|
|
|
|
.ThenInclude(x => x.Recipe)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ProcedureViewModel> GetFilteredList(ProcedureSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var database = new PolyclinicDatabase();
|
|
|
|
|
return database.Procedures
|
|
|
|
|
.Include(x => x.Recipes)
|
|
|
|
|
.ThenInclude(x => x.Recipe)
|
|
|
|
|
.Where(x => x.Name == model.Name)
|
|
|
|
|
.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(x => x.Recipes)
|
|
|
|
|
.ThenInclude(x => x.Recipe)
|
|
|
|
|
.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(database, 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();
|
|
|
|
|
using var transaction = database.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var Procedure = database.Procedures.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (Procedure == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Procedure.Update(model);
|
|
|
|
|
database.SaveChanges();
|
|
|
|
|
Procedure.UpdateRecipes(database, model);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return Procedure.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public ProcedureViewModel? Delete(ProcedureBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var database = new PolyclinicDatabase();
|
|
|
|
|
var element = database.Procedures
|
|
|
|
|
.Include(x => x.Recipes)
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
database.Procedures.Remove(element);
|
|
|
|
|
database.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-28 20:10:45 +04:00
|
|
|
|
}
|
|
|
|
|
}
|