104 lines
2.4 KiB
C#
Raw Normal View History

2024-05-30 07:33:38 +04:00
using Microsoft.AspNetCore.Mvc;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.BusinessLogicContracts;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.ViewModels;
namespace VeterinaryRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ServiceController : Controller
{
private readonly ILogger _logger;
private readonly IServiceLogic _service;
public ServiceController(ILogger<ServiceController> logger, IServiceLogic service)
{
_logger = logger;
_service = service;
}
[HttpGet]
public Tuple<ServiceViewModel, List<Tuple<string, int>>>? GetService(int serviceId)
{
try
{
var elem = _service.ReadElement(new ServiceSearchModel { Id = serviceId });
if (elem == null)
return null;
var res = Tuple.Create(elem, elem.ServiceMedications.Select(x => Tuple.Create(x.Value.MedicationName, x.Value.Id)).ToList());
res.Item1.ServiceMedications = null!;
return res;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения услуги по id={Id}", serviceId);
throw;
}
}
[HttpGet]
public List<ServiceViewModel> GetServices(int? doctorId = null)
{
try
{
List<ServiceViewModel> res;
if (!doctorId.HasValue)
res = _service.ReadList(null);
else
res = _service.ReadList(new ServiceSearchModel { DoctorId = doctorId.Value });
foreach (var service in res)
service.ServiceMedications = null;
return res;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка услуг");
throw;
}
}
[HttpPost]
public bool CreateService(ServiceBindingModel model)
{
try
{
return _service.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось создать услугу");
throw;
}
}
[HttpPost]
public bool UpdateService(ServiceBindingModel model)
{
try
{
///model.ServiceMedications = null!;
return _service.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось обновить услугу");
throw;
}
}
[HttpPost]
public bool DeleteService(ServiceBindingModel model)
{
try
{
return _service.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления услуги");
throw;
}
}
}
}