PIbd-22_Filippov_D.S._Cours.../VeterinaryDatabaseImplement/Models/Medication.cs

90 lines
2.1 KiB
C#
Raw Normal View History

2024-05-30 07:33:38 +04:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.ViewModels;
using VeterinaryDataModels.Models;
namespace VeterinaryDatabaseImplement.Models
{
public class Medication : IMedicationModel
{
public int Id { get; private set; }
[Required]
public string MedicationName { get; private set; } = string.Empty;
[Required]
public double Price { get; set; }
[Required]
public int DoctorId { get; private set; }
[ForeignKey("MedicationId")]
public virtual List<Pet> Pets { get; set; } = new();
[ForeignKey("MedicationId")]
public virtual List<DrugService> DrugServices { get; set; } = new();
[ForeignKey("MedicationId")]
public virtual List<DrugMedication> DrugMedications { get; set; } = new();
[ForeignKey("MedicationId")]
public virtual List<ServiceMedication> ServiceMedications { get; set; } = new();
public static Medication? Create(VeterinaryDatabase context, MedicationBindingModel model)
{
if (model == null)
{
return null;
}
/*var justMedications = model.MedicationPets.Select(x => new MedicationPet
{
Pet = context.Pets.First(y => y.Id == x.Key)
}).ToList();*/
return new Medication()
{
Id = model.Id,
MedicationName = model.MedicationName,
//Pets = justMedications,
DoctorId = model.DoctorId,
Price = model.Price
};
}
public static Medication Create(MedicationViewModel model)
{
return new Medication
{
Id = model.Id,
MedicationName = model.MedicationName,
DoctorId = model.DoctorId,
Price = model.Price
};
}
public void Update(MedicationBindingModel model)
{
if (model == null)
{
return;
}
MedicationName = model.MedicationName;
Price = model.Price;
}
public MedicationViewModel GetViewModel => new()
{
Id = Id,
MedicationName = MedicationName,
Price = Price,
//MedicationPets = MedicationPets,
DoctorId = DoctorId
};
}
}