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

82 lines
2.2 KiB
C#
Raw Normal View History

2024-04-30 23:14:53 +04:00
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,
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;
}
public VaccinationViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
DateInjection = DateInjection,
UserId = UserId,
UserFullName = User.FullName,
AnimalId = Animal.Id,
AnimalBreed = Animal.Breed
};
}
}