158 lines
3.9 KiB
C#
158 lines
3.9 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using VeterinaryClinicContracts.BindingModels;
|
|
using VeterinaryClinicContracts.BusinessLogicsContracts;
|
|
using VeterinaryClinicContracts.SearchModels;
|
|
using VeterinaryClinicDataModels.Models;
|
|
|
|
namespace VeterinaryClinicWebApp.Controllers
|
|
{
|
|
public class AnimalController : Controller
|
|
{
|
|
private readonly ILogger<AnimalController> _logger;
|
|
|
|
private readonly IAnimalLogic _animalLogic;
|
|
|
|
private readonly IMedicationLogic _medicationLogic;
|
|
public AnimalController(ILogger<AnimalController> logger, IAnimalLogic animalLogic, IMedicationLogic medicationLogic)
|
|
{
|
|
_logger = logger;
|
|
_animalLogic = animalLogic;
|
|
_medicationLogic = medicationLogic;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Вывести список животных
|
|
/// </summary>
|
|
[HttpGet]
|
|
public IActionResult Animals()
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
|
|
return View(_animalLogic.ReadList(new AnimalSearchModel
|
|
{
|
|
UserId = APIClient.User.Id,
|
|
}));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Создать животного
|
|
/// </summary>
|
|
[HttpGet]
|
|
public IActionResult CreateAnimal()
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
|
|
ViewBag.Medications = _medicationLogic.ReadList(null);
|
|
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public void CreateAnimal(string type, string breed, int age, List<int> medication)
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
throw new Exception("Необходимо авторизоваться!");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(breed) || age < 0)
|
|
{
|
|
throw new Exception("Введены не все данные!");
|
|
}
|
|
|
|
Dictionary<int, IMedicationModel> animalMedication = new Dictionary<int, IMedicationModel>();
|
|
foreach (var medicationId in medication)
|
|
{
|
|
animalMedication.Add(medicationId, _medicationLogic.ReadElement(new MedicationSearchModel { Id = medicationId })!);
|
|
}
|
|
|
|
_animalLogic.Create(new AnimalBindingModel
|
|
{
|
|
Type = type,
|
|
Breed = breed,
|
|
Age = age,
|
|
AnimalMedications = animalMedication,
|
|
UserId = APIClient.User.Id
|
|
});
|
|
|
|
Response.Redirect("/Animal/Animals");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Редактировать животного
|
|
/// </summary>
|
|
[HttpGet]
|
|
public IActionResult UpdateAnimal(int id)
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
|
|
ViewBag.Medications = _medicationLogic.ReadList(null);
|
|
|
|
return View(_animalLogic.ReadElement(new AnimalSearchModel
|
|
{
|
|
Id = id
|
|
}));
|
|
}
|
|
|
|
[HttpPost]
|
|
public void UpdateAnimal(int id, string type, string breed, int age, List<int> medication)
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
throw new Exception("Необходимо авторизоваться!");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(breed) || age < 0)
|
|
{
|
|
throw new Exception("Введены не все данные!");
|
|
}
|
|
|
|
Dictionary<int, IMedicationModel> animalMedication = new Dictionary<int, IMedicationModel>();
|
|
foreach (var medicationId in medication)
|
|
{
|
|
animalMedication.Add(medicationId, _medicationLogic.ReadElement(new MedicationSearchModel { Id = medicationId })!);
|
|
}
|
|
|
|
_animalLogic.Update(new AnimalBindingModel
|
|
{
|
|
Id = id,
|
|
Type = type,
|
|
Breed = breed,
|
|
Age = age,
|
|
AnimalMedications = animalMedication,
|
|
UserId = APIClient.User.Id
|
|
});
|
|
|
|
Response.Redirect("/Animal/Animals");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удалить животного
|
|
/// </summary>
|
|
[HttpPost]
|
|
public void DeleteAnimal(int id)
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
throw new Exception("Необходимо авторизоваться!");
|
|
}
|
|
|
|
_animalLogic.Delete(new AnimalBindingModel
|
|
{
|
|
Id = id
|
|
});
|
|
|
|
Response.Redirect("/Animal/Animals");
|
|
}
|
|
}
|
|
}
|