2024-04-28 20:05:31 +04:00
|
|
|
|
using PolyclinicContracts.BindingModels;
|
|
|
|
|
using PolyclinicContracts.ViewModels;
|
|
|
|
|
using PolyclinicDataModels.Models;
|
|
|
|
|
using SecuritySystemDatabaseImplement;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2024-04-30 16:20:00 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2024-04-28 16:24:52 +04:00
|
|
|
|
|
|
|
|
|
namespace PolyclinicDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Recipe : IRecipeModel
|
|
|
|
|
{
|
2024-04-28 20:05:31 +04:00
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public int ProceduresCount { get; set; }
|
|
|
|
|
|
2024-05-28 19:59:22 +04:00
|
|
|
|
public string? Comment { get; set; } = string.Empty;
|
2024-05-01 00:29:07 +04:00
|
|
|
|
public int? CourseId { get; set; }
|
2024-05-29 02:59:50 +04:00
|
|
|
|
public virtual Course? Course { get; set; }
|
2024-04-28 20:05:31 +04:00
|
|
|
|
|
2024-04-30 16:20:00 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 19:59:22 +04:00
|
|
|
|
public static Recipe? Create(PolyclinicDatabase database, RecipeBindingModel bindingModel)
|
2024-04-28 20:05:31 +04:00
|
|
|
|
{
|
|
|
|
|
return new Recipe()
|
|
|
|
|
{
|
|
|
|
|
Id = bindingModel.Id,
|
|
|
|
|
ProceduresCount = bindingModel.ProceduresCount,
|
|
|
|
|
Comment = bindingModel.Comment,
|
2024-04-30 22:51:17 +04:00
|
|
|
|
CourseId = bindingModel.CourseId,
|
2024-04-30 16:20:00 +04:00
|
|
|
|
Procedures = bindingModel.RecipeProcedures.Select(x => new RecipeProcedure
|
|
|
|
|
{
|
2024-05-28 16:55:59 +04:00
|
|
|
|
Procedure = database.Procedures.First(y => y.Id == x.Key),
|
2024-04-30 16:20:00 +04:00
|
|
|
|
}).ToList()
|
2024-04-28 20:05:31 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(RecipeBindingModel bindingModel)
|
|
|
|
|
{
|
|
|
|
|
ProceduresCount = bindingModel.ProceduresCount;
|
|
|
|
|
Comment = bindingModel.Comment;
|
2024-04-30 22:51:17 +04:00
|
|
|
|
CourseId = bindingModel.CourseId;
|
2024-04-28 20:05:31 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RecipeViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
ProceduresCount = ProceduresCount,
|
2024-04-30 22:51:17 +04:00
|
|
|
|
Comment = Comment,
|
2024-05-29 02:59:50 +04:00
|
|
|
|
CourseId = CourseId ?? null,
|
2024-04-28 20:05:31 +04:00
|
|
|
|
};
|
2024-04-30 16:20:00 +04:00
|
|
|
|
|
|
|
|
|
public void UpdateProcedures(PolyclinicDatabase database, RecipeBindingModel bindingModel)
|
|
|
|
|
{
|
|
|
|
|
var RecipeProcedures = database.RecipeProcedures.Where(x => x.ProcedureId == bindingModel.Id).ToList();
|
|
|
|
|
|
|
|
|
|
if (RecipeProcedures != null)
|
|
|
|
|
{
|
|
|
|
|
// удалили те, которых нет в модели
|
2024-05-28 16:55:59 +04:00
|
|
|
|
database.RecipeProcedures.RemoveRange(RecipeProcedures);
|
2024-04-30 16:20:00 +04:00
|
|
|
|
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)
|
|
|
|
|
});
|
2024-05-28 15:03:28 +04:00
|
|
|
|
database.SaveChanges();
|
2024-04-30 16:20:00 +04:00
|
|
|
|
}
|
|
|
|
|
_recipeProcedures = null;
|
|
|
|
|
}
|
2024-04-28 16:24:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|