Enable authorization for ProductController and update GetProducts method to support optional ID query parameter #5

This commit is contained in:
Marek Lesko
2025-07-28 11:49:01 +00:00
parent 35d7160fdd
commit 50f0bb7f57

View File

@@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Authorization;
namespace Api.Controllers namespace Api.Controllers
{ {
[ApiController] [ApiController]
// [Authorize] [Authorize]
[Route("api/[controller]")] [Route("api/[controller]")]
public class ProductController : ControllerBase public class ProductController : ControllerBase
{ {
@@ -19,8 +19,15 @@ namespace Api.Controllers
// GET: api/Product // GET: api/Product
[HttpGet] [HttpGet]
public async Task<ActionResult<IEnumerable<Product>>> GetProducts() public async Task<ActionResult<IEnumerable<Product>>> GetProducts([FromQuery] int? id = null)
{ {
if (id.HasValue)
{
return await _context.Products
.Where(p => p.Id == id.Value)
.ToListAsync();
}
else
return await _context.Products.ToListAsync(); return await _context.Products.ToListAsync();
} }