using HospitalContracts.BindingModels; using HospitalContracts.ViewModels; using HospitalDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HospitalDatabaseImplement.Models { /// /// Сущность "Болезнь" /// public class Disease : IDiseaseModel { /// /// Идентификатор /// public int Id { get; private set; } /// /// Название болезни /// [Required] [MaxLength(30)] public string Name { get; private set; } = string.Empty; /// /// Симптомы заболевания /// [MaxLength(100)] public string? Symptoms { get; private set; } /// /// Идентификатор рецепта /// [ForeignKey("RecipeId")] public int RecipeId { get; private set; } /// /// Сущность "Рецепт" /// public virtual Recipe Recipe { get; private set; } = new(); /// /// Создать сущность /// /// /// /// public static Disease? Create(HospitalDatabase context, DiseaseBindingModel model) { if (model == null) { return null; } return new Disease() { Id = model.Id, Name = model.Name, Symptoms = model.Symptoms, RecipeId = model.RecipeId, Recipe = context.Recipes .First(x => x.Id == model.RecipeId) }; } /// /// Изменить сущность /// /// public void Update(HospitalDatabase context, DiseaseBindingModel model) { if (model == null) { return; } Name = model.Name; Symptoms = model.Symptoms; RecipeId = model.RecipeId; Recipe = context.Recipes .First(x => x.Id == model.RecipeId); } /// /// Получить модель представления /// public DiseaseViewModel GetViewModel => new() { Id = Id, Name = Name, Symptoms = Symptoms, RecipeId = RecipeId }; } }