93 lines
1.9 KiB
C#
93 lines
1.9 KiB
C#
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;
|
||
}
|
||
}
|
||
}
|
||
}
|