PIbd-21_Danilov_V.V._Course.../VeterinaryClinic/VeterinaryClinicWebApp/Controllers/VaccinationController.cs
2024-05-29 11:23:13 +04:00

142 lines
3.5 KiB
C#

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<VaccinationController> _logger;
private readonly IAnimalLogic _animalLogic;
private readonly IVaccinationLogic _vaccinationLogic;
public VaccinationController(ILogger<VaccinationController> logger, IVaccinationLogic vaccinationLogic, IAnimalLogic animalLogic)
{
_logger = logger;
_vaccinationLogic = vaccinationLogic;
_animalLogic = animalLogic;
}
/// <summary>
/// Вывести список вакцинаций
/// </summary>
public IActionResult Vaccinations()
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
return View(_vaccinationLogic.ReadList(new VaccinationSearchModel
{
UserId = APIClient.User.Id,
}));
}
/// <summary>
/// Создать животного
/// </summary>
[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");
}
/// <summary>
/// Редактировать животного
/// </summary>
[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");
}
/// <summary>
/// Удалить животного
/// </summary>
}
}