PIbd-21_Danilov_V.V._Course.../VeterinaryClinic/VeterinaryClinicWebApp/Controllers/VaccinationController.cs

142 lines
3.5 KiB
C#
Raw Normal View History

2024-05-01 23:10:05 +04:00
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;
2024-05-01 23:10:05 +04:00
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)
2024-05-01 23:10:05 +04:00
{
_logger = logger;
_vaccinationLogic = vaccinationLogic;
_animalLogic = animalLogic;
2024-05-01 23:10:05 +04:00
}
/// <summary>
/// Вывести список вакцинаций
/// </summary>
2024-05-01 23:10:05 +04:00
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,
});
2024-05-01 23:10:05 +04:00
return View();
}
[HttpPost]
public void CreateVaccination(string name, DateTime dateInjection, string validityPeriod, int animal)
2024-05-01 23:10:05 +04:00
{
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");
2024-05-01 23:10:05 +04:00
}
/// <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>
2024-05-01 23:10:05 +04:00
}
}