вроде как добавила реализацию хранилища для медикаментов

This commit is contained in:
Елена Бакальская 2024-04-29 21:10:25 +04:00
parent 8e6cdea8fe
commit 4bf51abf26
3 changed files with 97 additions and 2 deletions

View File

@ -1,8 +1,99 @@
using PolyclinicContracts.StoragesContracts;
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 MedicamentStorage : IMedicamentStorage
{
public List<MedicamentViewModel> GetFullList()
{
using var context = new PolyclinicDatabase();
return context.Medicaments
.Include(x => x.Symptom)
.Include(x => x.Procedure)
.Select(x => x.GetViewModel)
.ToList();
}
public List<MedicamentViewModel> GetFilteredList(MedicamentSearchModel model)
{
using var context = new PolyclinicDatabase();
if (!model.Id.HasValue || !string.IsNullOrEmpty(model.Name))
{
return new();
}
return context.Medicaments
.Where(x => x.Id == model.Id || model.Name == x.Name)
.Include(x => x.Symptom)
.Include(x => x.Procedure)
.Select(x => x.GetViewModel)
.ToList();
}
public MedicamentViewModel? GetElement(MedicamentSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new PolyclinicDatabase();
return context.Medicaments
.Include(x => x.Symptom)
.Include(x => x.Procedure)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public MedicamentViewModel? Insert(MedicamentBindingModel model)
{
using var context = new PolyclinicDatabase();
var newMedicament = Medicament.Create(model);
if(newMedicament == null)
{
return null;
}
context.Medicaments.Add(newMedicament);
context.SaveChanges();
return context.Medicaments
.Include(x => x.Symptom)
.Include(x => x.Procedure)
.FirstOrDefault(x => x.Id == newMedicament.Id)?.GetViewModel;
}
public MedicamentViewModel? Update(MedicamentBindingModel model)
{
using var context = new PolyclinicDatabase();
var medicament = context.Medicaments.FirstOrDefault(x => x.Id == model.Id);
if (medicament == null)
{
return null;
}
medicament.Update(model);
context.SaveChanges();
return context.Medicaments
.Include(x => x.Symptom)
.Include(x => x.Procedure)
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public MedicamentViewModel? Delete(MedicamentBindingModel model)
{
using var context = new PolyclinicDatabase();
var element = context.Medicaments.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Medicaments.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -22,7 +22,10 @@ namespace PolyclinicDatabaseImplement.Models
[Required]
public int SymptomId { get; set; }
public static Medicament Create(PolyclinicDatabase database, MedicamentBindingModel bindingModel)
public virtual Symptom? Symptom { get; set; }
public virtual Procedure? Procedure { get; set; }
public static Medicament Create(MedicamentBindingModel bindingModel)
{
return new Medicament()
{

View File

@ -19,6 +19,7 @@ namespace SecuritySystemDatabaseImplement
public virtual DbSet<CourseDiagnose> CourseDiagnoses { set; get; }
public virtual DbSet<Recipe> Recipes { set; get; }
public virtual DbSet<Procedure> Procedures { set; get; }
public virtual DbSet<Medicament> Medicaments { set; get; }
public virtual DbSet<ProcedureRecipe> ProcedureRecipes { set; get; }
}
}