Skip to content

Commit 6426a69

Browse files
task(*): add authorize attribute to getall and getcount methods without parameters for employees and products
1 parent 0061d70 commit 6426a69

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

NorthwindCRUD/Controllers/EmployeesController.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ public ActionResult<EmployeeDto[]> GetAll()
4444
}
4545
}
4646

47+
[HttpGet("GetAllAuthorized")]
48+
[Authorize]
49+
public ActionResult<OrderDto[]> GetAllAuthorized()
50+
{
51+
try
52+
{
53+
var employees = this.employeeService.GetAll();
54+
return Ok(this.mapper.Map<EmployeeDb[], EmployeeDto[]>(employees));
55+
}
56+
catch (Exception error)
57+
{
58+
logger.LogError(error.Message);
59+
return StatusCode(500);
60+
}
61+
}
62+
4763
/// <summary>
4864
/// Fetches all employees or a page of employees based on the provided parameters.
4965
/// </summary>
@@ -123,6 +139,26 @@ public ActionResult<CountResultDto> GetEmployeesCount()
123139
}
124140
}
125141

142+
/// <summary>
143+
/// Retrieves the total number of employees.
144+
/// </summary>
145+
/// <returns>Total count of employees as an integer.</returns>
146+
[HttpGet("GetEmployeesCountAuthorized")]
147+
[Authorize]
148+
public ActionResult<CountResultDto> GetEmployeesCountAuthorized()
149+
{
150+
try
151+
{
152+
var count = employeeService.GetAllAsQueryable().Count();
153+
return new CountResultDto() { Count = count };
154+
}
155+
catch (Exception error)
156+
{
157+
logger.LogError(error.Message);
158+
return StatusCode(500);
159+
}
160+
}
161+
126162
[HttpGet("{id}")]
127163
public ActionResult<EmployeeDto> GetById(int id)
128164
{

NorthwindCRUD/Controllers/ProductsController.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ public ActionResult<ProductDto[]> GetAll()
4646
}
4747
}
4848

49+
[HttpGet("GetAllAuthorized")]
50+
[Authorize]
51+
public ActionResult<OrderDto[]> GetAllAuthorized()
52+
{
53+
try
54+
{
55+
var products = this.productService.GetAll();
56+
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(products));
57+
}
58+
catch (Exception error)
59+
{
60+
logger.LogError(error.Message);
61+
return StatusCode(500);
62+
}
63+
}
64+
4965
/// <summary>
5066
/// Fetches all products or a page of products based on the provided parameters.
5167
/// </summary>
@@ -125,6 +141,25 @@ public ActionResult<CountResultDto> GetProductsCount()
125141
}
126142
}
127143

144+
/// <summary>
145+
/// Retrieves the total number of products.
146+
/// </summary>
147+
/// <returns>Total count of products as an integer.</returns>
148+
[HttpGet("GetProductsCountAuthorized")]
149+
public ActionResult<CountResultDto> GetProductsCountAuthorized()
150+
{
151+
try
152+
{
153+
var count = productService.GetAllAsQueryable().Count();
154+
return new CountResultDto() { Count = count };
155+
}
156+
catch (Exception error)
157+
{
158+
logger.LogError(error.Message);
159+
return StatusCode(500);
160+
}
161+
}
162+
128163
[HttpGet("{id}")]
129164
public ActionResult<ProductDto> GetById(int id)
130165
{

0 commit comments

Comments
 (0)