using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using VeterinaryClinicBusinessLogics.BusinessLogics; using VeterinaryClinicContracts.BindingModels; using VeterinaryClinicContracts.BusinessLogicsContracts; using VeterinaryClinicContracts.SearchModels; using VeterinaryClinicDataModels.Models; using VeterinaryClinicWebApp.Models; namespace VeterinaryClinicWebApp.Controllers { public class VisitController : Controller { private readonly ILogger _logger; private readonly IVisitLogic _visitLogic; private readonly IAnimalLogic _animalLogic; private readonly IServiceLogic _serviceLogic; public VisitController(ILogger logger, IVisitLogic visitLogic, IAnimalLogic animalLogic, IServiceLogic serviceLogic) { _logger = logger; _visitLogic = visitLogic; _animalLogic = animalLogic; _serviceLogic = serviceLogic; } /// /// Вывести список визитов /// public IActionResult Visits() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(_visitLogic.ReadList(new VisitSearchModel { UserId = APIClient.User.Id, })); } /// /// Создать визит /// [HttpGet] public IActionResult CreateVisit() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Animals = _animalLogic.ReadList(new AnimalSearchModel { UserId = APIClient.User.Id, }); ViewBag.Services = _serviceLogic.ReadList(null); return View(); } [HttpPost] public void CreateVisit(DateTime dateVisit, List animal, List service) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (dateVisit == DateTime.MinValue || animal == null || service == null) { throw new Exception("Введены не все данные!"); } Dictionary visitAnimals = new Dictionary(); foreach (var animalId in animal) { visitAnimals.Add(animalId, _animalLogic.ReadElement(new AnimalSearchModel { Id = animalId })!); } Dictionary visitServices = new Dictionary(); foreach (var serviceId in service) { visitServices.Add(serviceId, _serviceLogic.ReadElement(new ServiceSearchModel { Id = serviceId })!); } _visitLogic.Create(new VisitBindingModel { Date = dateVisit, VisitAnimals = visitAnimals, VisitServices = visitServices, UserId = APIClient.User.Id }); Response.Redirect("/Visit/Visits"); } /// /// Редактировать визит /// [HttpGet] public IActionResult UpdateVisit(int id) { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Animals = _animalLogic.ReadList(new AnimalSearchModel { UserId = APIClient.User.Id, }); ViewBag.Services = _serviceLogic.ReadList(null); return View(_visitLogic.ReadElement(new VisitSearchModel { Id = id })); } [HttpPost] public void UpdateVisit(int id, DateTime dateVisit, List animal, List service) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (dateVisit == DateTime.MinValue || animal == null || service == null) { throw new Exception("Введены не все данные!"); } Dictionary visitAnimals = new Dictionary(); foreach (var animalId in animal) { visitAnimals.Add(animalId, _animalLogic.ReadElement(new AnimalSearchModel { Id = animalId })!); } Dictionary visitServices = new Dictionary(); foreach (var serviceId in service) { visitServices.Add(serviceId, _serviceLogic.ReadElement(new ServiceSearchModel { Id = serviceId })!); } _visitLogic.Update(new VisitBindingModel { Id = id, Date = dateVisit, VisitAnimals = visitAnimals, VisitServices = visitServices, UserId = APIClient.User.Id }); Response.Redirect("/Visit/Visits"); } /// /// Удалить визит /// [HttpPost] public void DeleteVisit(int id) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } _visitLogic.Delete(new VisitBindingModel { Id = id }); Response.Redirect("/Visit/Visits"); } } }