PIbd-21_Danilov_V.V._Course.../VeterinaryClinic/VeterinaryClinicDatabaseImplement/Models/Service.cs

70 lines
1.8 KiB
C#
Raw Normal View History

2024-04-30 23:14:53 +04:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryClinicDataModels.Models;
using System.Numerics;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.ViewModels;
namespace VeterinaryClinicDatabaseImplement.Models
{
public class Service: IServiceModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public int Cost { get; set; }
[ForeignKey("MedicationId")]
public int MedicationId { get; set; }
public virtual Medication Medication { get; set; } = new();
public static Service? Create(VeterinaryClinicDatabase context, ServiceBindingModel model)
{
if (model == null)
{
return null;
}
return new Service()
{
Id = model.Id,
Name = model.Name,
Cost = model.Cost,
MedicationId = model.MedicationId,
Medication = context.Medications
.First(x => x.Id == model.MedicationId),
};
}
public void Update(ServiceBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
Cost = model.Cost;
MedicationId = model.MedicationId;
2024-04-30 23:14:53 +04:00
}
public ServiceViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Cost = Cost,
MedicationId = MedicationId,
MedicationName = Medication.Name
};
}
}