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

132 lines
3.0 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
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)
{
_logger = logger;
_medicationLogic = medicationLogic;
}
/// <summary>
/// Вывести список лекарств
/// </summary>
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");
}
return View();
}
[HttpPost]
public void CreateMedication(string name, string description)
{
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");
}
/// <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");
}
/// <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");
}
}
}