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

132 lines
3.0 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 VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
2024-05-01 23:10:05 +04:00
using VeterinaryClinicWebApp.Models;
namespace VeterinaryClinicWebApp.Controllers
{
public class MedicationController : Controller
{
private readonly ILogger<MedicationController> _logger;
private readonly IMedicationLogic _medicationLogic;
public MedicationController(ILogger<MedicationController> logger, IMedicationLogic medicationLogic)
2024-05-01 23:10:05 +04:00
{
_logger = logger;
_medicationLogic = medicationLogic;
2024-05-01 23:10:05 +04:00
}
/// <summary>
/// Вывести список лекарств
/// </summary>
2024-05-01 23:10:05 +04:00
public IActionResult Medications()
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
return View(_medicationLogic.ReadList(null));
}
/// <summary>
/// Создать лекарство
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreateMedication()
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
2024-05-01 23:10:05 +04:00
return View();
}
[HttpPost]
public void CreateMedication(string name, string description)
2024-05-01 23:10:05 +04:00
{
if (APIClient.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(description))
{
throw new Exception("Введены не все данные!");
}
_medicationLogic.Create(new MedicationBindingModel
{
Name = name,
Description = description
});
Response.Redirect("/Medication/Medications");
2024-05-01 23:10:05 +04:00
}
/// <summary>
/// Редактировать лекарство
/// </summary>
[HttpGet]
public IActionResult UpdateMedication(int id)
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
return View(_medicationLogic.ReadElement(new MedicationSearchModel
{
Id = id
}));
}
[HttpPost]
public void UpdateMedication(int id, string name, string description)
{
if (APIClient.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(description))
{
throw new Exception("Введены не все данные!");
}
_medicationLogic.Update(new MedicationBindingModel
{
Id = id,
Name = name,
Description = description
});
Response.Redirect("/Medication/Medications");
}
2024-05-29 16:14:54 +04:00
/// <summary>
/// Удалить лекарство
/// </summary>
[HttpPost]
public void DeleteMedication(int id)
{
if (APIClient.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_medicationLogic.Delete(new MedicationBindingModel
{
Id = id
});
Response.Redirect("/Medication/Medications");
}
}
2024-05-01 23:10:05 +04:00
}