медицине сторадж, любиоме

This commit is contained in:
ValAnn 2024-04-29 16:33:02 +04:00
parent 01996f74f1
commit c43bf6c66d

View File

@ -10,45 +10,26 @@ using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using Microsoft.EntityFrameworkCore;
using HospitalDatabaseImplement.Models;
namespace HospitalDatabaseImplement.Implements
{
public class MedicineStorage : IMedicineStorage
{
public MedicineViewModel? GetElement(MedicineSearchModel model)
public List<MedicineViewModel> GetFullList()
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new HospitalDatabase();
return context.Medicines
.Include(x => x.Pharmacist)
.Include(x => x.Procedures)
.ThenInclude(x => x.Recipe)
.FirstOrDefault(x => ((!string.IsNullOrEmpty(model.Name) && x.FIO == model.Name)
|| (model.Id.HasValue && x.Id == model.Id)))?.GetViewModel;
.Select(x => x.GetViewModel).ToList();
}
public List<MedicineViewModel> GetFilteredList(MedicineSearchModel model)
{
using var context = new HospitalDatabase();
if (!string.IsNullOrEmpty(model.Name))
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return context.Medicines
.Include(x => x.Doctor)
.Include(x => x.Recipes)
.ThenInclude(x => x.Recipe)
.Where(x => (x.Id == model.Id)).ToList()
.Select(x => x.GetViewModel).ToList();
}
else if (model.DoctorId.HasValue)
{
return context.Medicines
.Include(x => x.Doctor)
.Include(x => x.Recipes)
.ThenInclude(x => x.Recipe)
.Where(x => x.DoctorId == model.DoctorId)
.Where(x => x.Name.Contains(model.Name)).ToList()
.Select(x => x.GetViewModel).ToList();
}
else
@ -57,10 +38,67 @@ namespace HospitalDatabaseImplement.Implements
}
}
namespace HospitalDatabaseImplement.Implementss
{
public class MedicineStorage
{
public MedicineViewModel? GetElement(MedicineSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new HospitalDatabase();
return context.Medicines
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public MedicineViewModel? Insert(MedicineBindingModel model)
{
using var context = new HospitalDatabase();
var newMedicine = Medicine.Create(model);
if (newMedicine == null)
{
return null;
}
context.Medicines.Add(newMedicine);
context.SaveChanges();
return newMedicine.GetViewModel;
}
public MedicineViewModel? Update(MedicineBindingModel model)
{
using var context = new HospitalDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var medicine = context.Medicines
.FirstOrDefault(rec => rec.Id == model.Id);
if (medicine == null)
{
return null;
}
medicine.Update(model);
context.SaveChanges();
transaction.Commit();
return medicine.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public MedicineViewModel? Delete(MedicineBindingModel model)
{
using var context = new HospitalDatabase();
var element = context.Medicines
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Medicines.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}