ProductsController.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using DutchTreat.Data;
  2. using DutchTreat.Data.Entities;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Logging;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace DutchTreat.Controllers
  11. {
  12. [Route("api/[Controller]")]
  13. [ApiController]
  14. [Produces("application/json")]
  15. public class ProductsController : Controller
  16. {
  17. private readonly IDutchRepository _repository;
  18. private readonly ILogger<ProductsController> _logger;
  19. public ProductsController(IDutchRepository repository, ILogger<ProductsController> logger)
  20. {
  21. _repository = repository;
  22. _logger = logger;
  23. }
  24. [HttpGet]
  25. [ProducesResponseType(200)]
  26. [ProducesResponseType(400)]
  27. public ActionResult<IEnumerable<Product>> Get()
  28. {
  29. try
  30. {
  31. return Ok(_repository.GetAllProducts());
  32. }
  33. catch (Exception ex)
  34. {
  35. _logger.LogError($"Failed to get products: {ex}");
  36. return BadRequest("Failed to get products");
  37. }
  38. }
  39. }
  40. }