157 lines
3.9 KiB
C#
157 lines
3.9 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>
|
|
[HttpPost]
|
|
public void DeleteVaccination(int id)
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
throw new Exception("Необходимо авторизоваться!");
|
|
}
|
|
|
|
_vaccinationLogic.Delete(new VaccinationBindingModel
|
|
{
|
|
Id = id
|
|
});
|
|
|
|
Response.Redirect("/Vaccination/Vaccinations");
|
|
}
|
|
}
|
|
}
|