2024-04-28 16:24:52 +04:00
|
|
|
|
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 Course : ICourseModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public int DaysCount { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public int PillsPerDay { get; set; }
|
|
|
|
|
public string Comment { get; set; } = string.Empty;
|
|
|
|
|
[ForeignKey("CourseId")]
|
|
|
|
|
public virtual List<CourseDiagnose> Diagnoses { get; set; } = new();
|
|
|
|
|
private Dictionary<int, IDiagnoseModel>? _courseDiagnoses = null;
|
2024-04-29 21:47:21 +04:00
|
|
|
|
[NotMapped]
|
2024-04-28 16:24:52 +04:00
|
|
|
|
public Dictionary<int, IDiagnoseModel> CourseDiagnoses
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_courseDiagnoses == null)
|
|
|
|
|
{
|
|
|
|
|
_courseDiagnoses = Diagnoses.ToDictionary(
|
|
|
|
|
courseDiagnose => courseDiagnose.DiagnoseId,
|
|
|
|
|
courseDiagnose => courseDiagnose.Diagnose as IDiagnoseModel
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return _courseDiagnoses;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 16:29:57 +04:00
|
|
|
|
public static Course Create(PolyclinicDatabase context, CourseBindingModel model)
|
2024-04-28 16:24:52 +04:00
|
|
|
|
{
|
|
|
|
|
return new Course()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
DaysCount = model.DaysCount,
|
|
|
|
|
PillsPerDay = model.PillsPerDay,
|
|
|
|
|
Comment = model.Comment,
|
|
|
|
|
Diagnoses = model.CourseDiagnoses.Select(courseDiagnose => new CourseDiagnose
|
|
|
|
|
{
|
|
|
|
|
Diagnose = context.Diagnoses.First(diagnose => diagnose.Id == courseDiagnose.Key)
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(CourseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
DaysCount = model.DaysCount;
|
|
|
|
|
PillsPerDay = model.PillsPerDay;
|
|
|
|
|
Comment = model.Comment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CourseViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
2024-04-28 16:27:00 +04:00
|
|
|
|
DaysCount = DaysCount,
|
|
|
|
|
PillsPerDay = PillsPerDay,
|
|
|
|
|
Comment = Comment,
|
2024-04-28 16:24:52 +04:00
|
|
|
|
CourseDiagnoses = CourseDiagnoses
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-28 16:29:57 +04:00
|
|
|
|
public void UpdateDiagnoses(PolyclinicDatabase context, CourseBindingModel model)
|
2024-04-28 16:24:52 +04:00
|
|
|
|
{
|
2024-04-29 20:47:31 +04:00
|
|
|
|
var courseDiagnoses = context.CourseDiagnoses.Where(rec => rec.CourseId == model.Id).ToList();
|
|
|
|
|
if (courseDiagnoses != null && courseDiagnoses.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
// удалили те, которых нет в модели
|
|
|
|
|
context.CourseDiagnoses.RemoveRange(courseDiagnoses
|
|
|
|
|
.Where(rec => !model.CourseDiagnoses.ContainsKey(rec.DiagnoseId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var course = context.Courses.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.CourseDiagnoses)
|
|
|
|
|
{
|
|
|
|
|
context.CourseDiagnoses.Add(new CourseDiagnose
|
|
|
|
|
{
|
|
|
|
|
Course = course,
|
|
|
|
|
Diagnose = context.Diagnoses.First(x => x.Id == pc.Key),
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_courseDiagnoses = null;
|
2024-04-28 16:24:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|