PIbd-22_Filippov_D.S._Cours.../VeterinaryRestApi/Controllers/MedicationController.cs
2024-05-30 07:33:38 +04:00

93 lines
2.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Mvc;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.BusinessLogicContracts;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.ViewModels;
using VeterinaryDatabaseImplement.Models;
namespace VeterinaryRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MedicationController : Controller
{
private readonly ILogger _logger;
private readonly IMedicationLogic _medication;
public MedicationController(ILogger<MedicationController> logger, IMedicationLogic medication)
{
_logger = logger;
_medication = medication;
}
[HttpGet]
public MedicationViewModel? GetMedication(int medicationId)
{
try
{
return _medication.ReadElement(new MedicationSearchModel
{
Id = medicationId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения медикамента по id={Id}", medicationId);
throw;
}
}
[HttpGet]
public List<MedicationViewModel> GetMedications(int doctorId)
{
try
{
return _medication.ReadList(new MedicationSearchModel { DoctorId = doctorId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка медикаментов");
throw;
}
}
[HttpPost]
public bool CreateMedication(MedicationBindingModel model)
{
try
{
return _medication.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось создать медикамент");
throw;
}
}
[HttpPost]
public bool UpdateMedication(MedicationBindingModel model)
{
try
{
return _medication.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось обновить магазин");
throw;
}
}
[HttpPost]
public bool DeleteMedication(MedicationBindingModel model)
{
try
{
return _medication.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления магазина");
throw;
}
}
}
}