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

93 lines
1.9 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 PetController : Controller
{
private readonly ILogger _logger;
private readonly IPetLogic _pet;
public PetController(ILogger<PetController> logger, IPetLogic pet)
{
_logger = logger;
_pet = pet;
}
[HttpGet]
public List<PetViewModel> GetPets(int ownerId)
{
try
{
return _pet.ReadList(new PetSearchModel { OwnerId = ownerId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка животных");
throw;
}
}
[HttpGet]
public Tuple<PetViewModel>? GetPet(int petId)
{
try
{
var elem = _pet.ReadElement(new PetSearchModel { Id = petId });
if (elem == null)
return null;
return Tuple.Create(elem);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения животного по id={Id}", petId);
throw;
}
}
[HttpPost]
public bool CreatePet(PetBindingModel model)
{
try
{
return _pet.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось создать животного");
throw;
}
}
[HttpPost]
public bool UpdatePet(PetBindingModel model)
{
try
{
return _pet.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось обновить животное");
throw;
}
}
[HttpPost]
public bool DeletePet(PetBindingModel model)
{
try
{
return _pet.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления животного");
throw;
}
}
}
}