PIbd-21_Danilov_V.V._Course.../VeterinaryClinic/VeterinaryClinicDatabaseImplement/Models/Vaccination.cs
2024-05-29 11:23:13 +04:00

86 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.ViewModels;
using VeterinaryClinicDataModels.Models;
namespace VeterinaryClinicDatabaseImplement.Models
{
public class Vaccination: IVaccinationModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public DateTime DateInjection { get; set; } = DateTime.Now;
[Required]
public string ValidityPeriod { get; set; } = string.Empty;
[ForeignKey("UserId")]
public int UserId { get; set; }
public virtual User User { get; set; } = new();
[ForeignKey("AnimalId")]
public int AnimalId { get; set; }
public virtual Animal Animal { get; set; } = new();
public static Vaccination? Create(VeterinaryClinicDatabase context, VaccinationBindingModel model)
{
if (model == null)
{
return null;
}
return new Vaccination()
{
Id = model.Id,
Name = model.Name,
DateInjection = model.DateInjection,
ValidityPeriod = model.ValidityPeriod,
UserId = model.UserId,
User = context.Users
.First(x => x.Id == model.UserId),
AnimalId = model.AnimalId,
Animal = context.Animals
.First(x => x.Id == model.AnimalId)
};
}
public void Update(VaccinationBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
DateInjection = model.DateInjection;
ValidityPeriod = model.ValidityPeriod;
AnimalId = model.AnimalId;
}
public VaccinationViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
DateInjection = DateInjection,
ValidityPeriod = ValidityPeriod,
UserId = UserId,
UserFullName = User.FullName,
AnimalId = Animal.Id,
AnimalBreed = Animal.Breed
};
}
}