113 lines
3.9 KiB
C#
Raw Normal View History

2023-04-05 23:24:58 +04:00
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
2023-04-07 00:53:55 +04:00
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.Identity.Client;
2023-04-05 23:24:58 +04:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
2023-04-07 00:53:55 +04:00
using System.Dynamic;
2023-04-05 23:24:58 +04:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDataBaseImplements.Models
{
public class Recipes : IRecipesModel
{
public int Id { get; private set; }
[Required]
public string Dose { get; private set; } = string.Empty;
[Required]
public DateTime Date { get; private set; } = DateTime.Now;
[Required]
2023-04-07 00:53:55 +04:00
public int ClientId { get; set; }
2023-04-05 23:24:58 +04:00
[Required]
public string ModeOfApplication { get; private set; } = string.Empty;
[Required]
public int SymptomsId { get; private set; }
private Dictionary<int, IProceduresModel>? _recipeProcedures = null;
2023-04-05 23:24:58 +04:00
[NotMapped]
public Dictionary<int, IProceduresModel> RecipeProcedures
2023-04-05 23:24:58 +04:00
{
get
{
if (_recipeProcedures == null)
{
2023-05-20 06:49:33 +04:00
_recipeProcedures = Procedures.ToDictionary(recPC => recPC.ProcedureId, recPC =>
recPC.Procedure as IProceduresModel);
2023-04-05 23:24:58 +04:00
}
return _recipeProcedures;
}
}
2023-05-20 06:49:33 +04:00
[ForeignKey("RecipesId")]
2023-04-05 23:24:58 +04:00
public virtual List<RecipesProcedures> Procedures { get; set; } = new();
public Client Client { get; set; }
public Symptoms Symptoms { get; set; }
2023-05-20 06:49:33 +04:00
public static Recipes? Create(RecipesBindingModel model)
{
if (model == null)
{
return null;
}
2023-04-05 23:24:58 +04:00
return new Recipes()
{
Id = model.Id,
SymptomsId = model.SymptomsId,
2023-05-20 06:49:33 +04:00
Dose = model.Dose,
ClientId = model.ClientId,
ModeOfApplication = model.ModeOfApplication
2023-04-05 23:24:58 +04:00
};
}
public static Recipes Create(RecipesViewModel model)
{
return new Recipes()
{
Id = model.Id,
SymptomsId = model.SymptomsId,
Dose = model.Dose,
ClientId = model.ClientId,
ModeOfApplication = model.ModeOfApplication
};
}
public void Update(RecipesBindingModel model)
2023-04-05 23:24:58 +04:00
{
Date = model.Date;
Dose = model.Dose;
ModeOfApplication = model.ModeOfApplication;
2023-04-05 23:24:58 +04:00
}
public RecipesViewModel GetViewModel => new()
2023-04-05 23:24:58 +04:00
{
Id = Id,
Date = Date,
ClientId = ClientId,
ModeOfApplication = ModeOfApplication,
Dose = Dose,
SymptomsId = SymptomsId
};
public void UpdateProcedures(HospitalDatabase context, RecipesBindingModel model)
{
2023-04-07 00:53:55 +04:00
var recipeProcedures = context.RecipesProcedures.Where(rec => rec.RecipesId == model.Id).ToList();
if (recipeProcedures != null && recipeProcedures.Count > 0)
{ // удалили те, которых нет в модели
context.RecipesProcedures.RemoveRange(recipeProcedures.Where(rec
=> !model.RecipeProcedures.ContainsKey(rec.ProcedureId)));
context.SaveChanges();
}
var recipe = context.Recipes.First(x => x.Id == Id);
foreach (var pc in model.RecipeProcedures)
{
context.RecipesProcedures.Add(new RecipesProcedures
{
2023-04-07 00:53:55 +04:00
Recipe = recipe,
Procedure = context.Procedures.First(x => x.Id == pc.Key)
});
context.SaveChanges();
}
_recipeProcedures = null;
}
2023-04-05 23:24:58 +04:00
}
}