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