PIbd-21_MasenkinMS_Coursewo.../Hospital/HospitalDatabaseImplement/Models/Disease.cs

103 lines
2.8 KiB
C#
Raw Permalink Normal View History

2024-04-25 02:00:30 +04:00
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
{
/// <summary>
/// Сущность "Болезнь"
/// </summary>
public class Disease : IDiseaseModel
{
/// <summary>
/// Идентификатор
/// </summary>
public int Id { get; private set; }
/// <summary>
/// Название болезни
/// </summary>
[Required]
[MaxLength(30)]
public string Name { get; private set; } = string.Empty;
/// <summary>
/// Симптомы заболевания
/// </summary>
[MaxLength(100)]
public string? Symptoms { get; private set; }
/// <summary>
/// Идентификатор рецепта
/// </summary>
[ForeignKey("RecipeId")]
public int RecipeId { get; private set; }
/// <summary>
/// Сущность "Рецепт"
/// </summary>
public virtual Recipe Recipe { get; private set; } = new();
/// <summary>
/// Создать сущность
/// </summary>
/// <param name="context"></param>
/// <param name="model"></param>
/// <returns></returns>
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)
};
}
/// <summary>
/// Изменить сущность
/// </summary>
/// <param name="model"></param>
2024-05-27 22:25:54 +04:00
public void Update(HospitalDatabase context, DiseaseBindingModel model)
2024-04-25 02:00:30 +04:00
{
if (model == null)
{
return;
}
Name = model.Name;
Symptoms = model.Symptoms;
2024-05-27 22:25:54 +04:00
RecipeId = model.RecipeId;
Recipe = context.Recipes
.First(x => x.Id == model.RecipeId);
}
2024-04-25 02:00:30 +04:00
/// <summary>
/// Получить модель представления
/// </summary>
public DiseaseViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Symptoms = Symptoms,
RecipeId = RecipeId
};
}
}