97 lines
3.3 KiB
C#
97 lines
3.3 KiB
C#
using PolyclinicContracts.BindingModels;
|
|
using PolyclinicContracts.ViewModels;
|
|
using PolyclinicDataModels.Models;
|
|
using SecuritySystemDatabaseImplement;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
namespace PolyclinicDatabaseImplement.Models
|
|
{
|
|
public class Recipe : IRecipeModel
|
|
{
|
|
public int Id { get; set; }
|
|
|
|
[Required]
|
|
public int ProceduresCount { get; set; }
|
|
|
|
public string? Comment { get; set; } = string.Empty;
|
|
public int? CourseId { get; set; }
|
|
public virtual Course? Course { get; set; }
|
|
|
|
private Dictionary<int, IProcedureModel>? _recipeProcedures = null;
|
|
|
|
[ForeignKey("RecipeId")]
|
|
public virtual List<RecipeProcedure> Procedures { get; set; } = new();
|
|
|
|
[NotMapped]
|
|
public Dictionary<int, IProcedureModel> RecipeProcedures
|
|
{
|
|
get
|
|
{
|
|
if (_recipeProcedures == null)
|
|
{
|
|
_recipeProcedures = Procedures.ToDictionary(recPC => recPC.ProcedureId, recPC => (recPC.Procedure as IProcedureModel));
|
|
}
|
|
return _recipeProcedures;
|
|
}
|
|
}
|
|
|
|
public static Recipe? Create(PolyclinicDatabase database, RecipeBindingModel bindingModel)
|
|
{
|
|
return new Recipe()
|
|
{
|
|
Id = bindingModel.Id,
|
|
ProceduresCount = bindingModel.ProceduresCount,
|
|
Comment = bindingModel.Comment,
|
|
CourseId = bindingModel.CourseId,
|
|
Procedures = bindingModel.RecipeProcedures.Select(x => new RecipeProcedure
|
|
{
|
|
Procedure = database.Procedures.First(y => y.Id == x.Key),
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public void Update(RecipeBindingModel bindingModel)
|
|
{
|
|
ProceduresCount = bindingModel.ProceduresCount;
|
|
Comment = bindingModel.Comment;
|
|
CourseId = bindingModel.CourseId;
|
|
}
|
|
|
|
public RecipeViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
ProceduresCount = ProceduresCount,
|
|
Comment = Comment,
|
|
CourseId = CourseId ?? null,
|
|
};
|
|
|
|
public void UpdateProcedures(PolyclinicDatabase database, RecipeBindingModel bindingModel)
|
|
{
|
|
if (database.Procedures.Count() == 0)
|
|
{
|
|
return;
|
|
}
|
|
var RecipeProcedures = database.RecipeProcedures.Where(x => x.ProcedureId == bindingModel.Id).ToList();
|
|
|
|
if (RecipeProcedures != null)
|
|
{
|
|
// удалили те, которых нет в модели
|
|
database.RecipeProcedures.RemoveRange(RecipeProcedures);
|
|
database.SaveChanges();
|
|
}
|
|
var Procedure = database.Procedures.First(x => x.Id == bindingModel.Id);
|
|
foreach (var pc in bindingModel.RecipeProcedures)
|
|
{
|
|
database.RecipeProcedures.Add(new RecipeProcedure
|
|
{
|
|
Procedure = Procedure,
|
|
Recipe = database.Recipes.First(x => x.Id == pc.Key)
|
|
});
|
|
database.SaveChanges();
|
|
}
|
|
_recipeProcedures = null;
|
|
}
|
|
}
|
|
}
|