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

65 lines
1.6 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 PurchaseController : Controller
{
private readonly ILogger _logger;
private readonly IPurchaseLogic _purchase;
public PurchaseController(ILogger<PurchaseController> logger, IPurchaseLogic purchase)
{
_logger = logger;
_purchase = purchase;
}
[HttpGet]
public Tuple<PurchaseViewModel, List<string>>? GetPurchase(int purchaseId)
{
try
{
var elem = _purchase.ReadElement(new PurchaseSearchModel { Id = purchaseId });
if (elem == null)
return null;
return Tuple.Create(elem, elem.PurchasePet.Select(x => x.Value.PetName).ToList());
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения прививки по id={Id}", purchaseId);
throw;
}
}
[HttpGet]
public List<PurchaseViewModel>? GetPurchases(int? ownerId)
{
try
{
return _purchase.ReadList(new PurchaseSearchModel { OwnerId = ownerId});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка покупок");
throw;
}
}
[HttpPost]
public bool CreatePurchase(PurchaseBindingModel model)
{
try
{
return _purchase.CreatePurchase(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось создать прививку");
throw;
}
}
}
}