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

143 lines
3.3 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 VeterinaryClinicBusinessLogics.BusinessLogics;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
using VeterinaryClinicDataModels.Models;
2024-05-01 23:10:05 +04:00
using VeterinaryClinicWebApp.Models;
namespace VeterinaryClinicWebApp.Controllers
{
public class ServiceController : Controller
{
private readonly ILogger<ServiceController> _logger;
private readonly IServiceLogic _serviceLogic;
private readonly IMedicationLogic _medicationLogic;
public ServiceController(ILogger<ServiceController> logger, IServiceLogic serviceLogic, IMedicationLogic medicationLogic)
2024-05-01 23:10:05 +04:00
{
_logger = logger;
_serviceLogic = serviceLogic;
_medicationLogic = medicationLogic;
2024-05-01 23:10:05 +04:00
}
/// <summary>
2024-05-29 16:14:54 +04:00
/// Вывести список услуг
/// </summary>
[HttpGet]
2024-05-01 23:10:05 +04:00
public IActionResult Services()
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
return View(_serviceLogic.ReadList(null));
}
/// <summary>
2024-05-29 16:14:54 +04:00
/// Создать услугу
/// </summary>
[HttpGet]
public IActionResult CreateService()
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medications = _medicationLogic.ReadList(null);
2024-05-01 23:10:05 +04:00
return View();
}
[HttpPost]
public void CreateService(string name, int cost, int medication)
2024-05-01 23:10:05 +04:00
{
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");
2024-05-01 23:10:05 +04:00
}
/// <summary>
2024-05-29 16:14:54 +04:00
/// Редактировать услугу
/// </summary>
[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");
}
2024-05-29 16:14:54 +04:00
/// <summary>
/// Удалить услугу
/// </summary>
[HttpPost]
public void DeleteService(int id)
{
if (APIClient.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_serviceLogic.Delete(new ServiceBindingModel
{
Id = id
});
Response.Redirect("/Service/Services");
}
}
2024-05-01 23:10:05 +04:00
}