using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using System.Xml.Linq; using VeterinaryClinicBusinessLogics.BusinessLogics; using VeterinaryClinicContracts.BindingModels; using VeterinaryClinicContracts.BusinessLogicsContracts; using VeterinaryClinicContracts.SearchModels; using VeterinaryClinicDatabaseImplement.Models; using VeterinaryClinicDataModels.Models; using VeterinaryClinicWebApp.Models; namespace VeterinaryClinicWebApp.Controllers { public class VaccinationController : Controller { private readonly ILogger _logger; private readonly IAnimalLogic _animalLogic; private readonly IVaccinationLogic _vaccinationLogic; public VaccinationController(ILogger logger, IVaccinationLogic vaccinationLogic, IAnimalLogic animalLogic) { _logger = logger; _vaccinationLogic = vaccinationLogic; _animalLogic = animalLogic; } /// /// Вывести список вакцинаций /// public IActionResult Vaccinations() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(_vaccinationLogic.ReadList(new VaccinationSearchModel { UserId = APIClient.User.Id, })); } /// /// Создать вакцинаю /// [HttpGet] public IActionResult CreateVaccination() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Animals = _animalLogic.ReadList(new AnimalSearchModel { UserId = APIClient.User.Id, }); return View(); } [HttpPost] public void CreateVaccination(string name, DateTime dateInjection, string validityPeriod, int animal) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(name) || dateInjection == DateTime.MinValue || string.IsNullOrEmpty(validityPeriod) || animal <= 0) { throw new Exception("Введены не все данные!"); } _vaccinationLogic.Create(new VaccinationBindingModel { Name = name, DateInjection = dateInjection, ValidityPeriod = validityPeriod, AnimalId = animal, UserId = APIClient.User.Id }); Response.Redirect("/Vaccination/Vaccinations"); } /// /// Редактировать вакцинацию /// [HttpGet] public IActionResult UpdateVaccination(int id) { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Animals = _animalLogic.ReadList(new AnimalSearchModel { UserId = APIClient.User.Id, }); return View(_vaccinationLogic.ReadElement(new VaccinationSearchModel { Id = id })); } [HttpPost] public void UpdateVaccination(int id, string name, DateTime dateInjection, string validityPeriod, int animal) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(name) || dateInjection == DateTime.MinValue || string.IsNullOrEmpty(validityPeriod) || animal <= 0) { throw new Exception("Введены не все данные!"); } _vaccinationLogic.Update(new VaccinationBindingModel { Id = id, Name = name, DateInjection = dateInjection, ValidityPeriod = validityPeriod, AnimalId = animal, UserId = APIClient.User.Id }); Response.Redirect("/Vaccination/Vaccinations"); } /// /// Удалить вакцинацию /// [HttpPost] public void DeleteVaccination(int id) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } _vaccinationLogic.Delete(new VaccinationBindingModel { Id = id }); Response.Redirect("/Vaccination/Vaccinations"); } } }