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 ServiceController : Controller { private readonly ILogger _logger; private readonly IServiceLogic _serviceLogic; private readonly IMedicationLogic _medicationLogic; public ServiceController(ILogger logger, IServiceLogic serviceLogic, IMedicationLogic medicationLogic) { _logger = logger; _serviceLogic = serviceLogic; _medicationLogic = medicationLogic; } /// /// Вывести список услуг /// [HttpGet] public IActionResult Services() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(_serviceLogic.ReadList(null)); } /// /// Создать услугу /// [HttpGet] public IActionResult CreateService() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Medications = _medicationLogic.ReadList(null); return View(); } [HttpPost] public void CreateService(string name, int cost, int medication) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(name) || cost <= 0 || medication == 0) { throw new Exception("Введены не все данные!"); } _serviceLogic.Create(new ServiceBindingModel { Name = name, Cost = cost, MedicationId = medication }); Response.Redirect("/Service/Services"); } /// /// Редактировать услугу /// [HttpGet] public IActionResult UpdateService(int id) { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Medications = _medicationLogic.ReadList(null); return View(_serviceLogic.ReadElement(new ServiceSearchModel { Id = id })); } [HttpPost] public void UpdateService(int id, string name, int cost, int medication) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(name) || cost <= 0 || medication == 0) { throw new Exception("Введены не все данные!"); } _serviceLogic.Update(new ServiceBindingModel { Id = id, Name = name, Cost = cost, MedicationId = medication }); Response.Redirect("/Service/Services"); } /// /// Удалить услугу /// [HttpPost] public void DeleteService(int id) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } _serviceLogic.Delete(new ServiceBindingModel { Id = id }); Response.Redirect("/Service/Services"); } } }