PIbd-21_Danilov_V.V._Course.../VeterinaryClinic/VeterinaryClinicWebApp/Controllers/ServiceController.cs
Владимир Данилов 787f1685c7 CRUD
2024-05-29 16:14:54 +04:00

143 lines
3.3 KiB
C#

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<ServiceController> _logger;
private readonly IServiceLogic _serviceLogic;
private readonly IMedicationLogic _medicationLogic;
public ServiceController(ILogger<ServiceController> logger, IServiceLogic serviceLogic, IMedicationLogic medicationLogic)
{
_logger = logger;
_serviceLogic = serviceLogic;
_medicationLogic = medicationLogic;
}
/// <summary>
/// Вывести список услуг
/// </summary>
[HttpGet]
public IActionResult Services()
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
return View(_serviceLogic.ReadList(null));
}
/// <summary>
/// Создать услугу
/// </summary>
[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");
}
/// <summary>
/// Редактировать услугу
/// </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");
}
/// <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");
}
}
}