Skip to content

Commit 57315bf

Browse files
committed
Add extra count methods for all entities and remote Querable thats not needed
1 parent 564d3fd commit 57315bf

18 files changed

+182
-9
lines changed

NorthwindCRUD/Controllers/CategoriesController.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ public ActionResult<PagedResultDto<CategoryDto>> GetCategoriesWithPage(
102102
}
103103
}
104104

105+
/// <summary>
106+
/// Retrieves the total number of categories.
107+
/// </summary>
108+
/// <returns>Total count of categories as an integer.</returns>
109+
[HttpGet("GetCategoriesCount")]
110+
public ActionResult<int> GetCategoriesCount()
111+
{
112+
try
113+
{
114+
var count = categoryService.GetAllAsQueryable().Count();
115+
return Ok(count);
116+
}
117+
catch (Exception error)
118+
{
119+
logger.LogError(error.Message);
120+
return StatusCode(500);
121+
}
122+
}
123+
105124
[HttpGet("{id}")]
106125
public ActionResult<CategoryDto> GetById(int id)
107126
{

NorthwindCRUD/Controllers/CustomersController.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ public ActionResult<PagedResultDto<CustomerDto>> GetCustomersWithPage(
102102
}
103103
}
104104

105+
/// <summary>
106+
/// Retrieves the total number of customers.
107+
/// </summary>
108+
/// <returns>Total count of customers as an integer.</returns>
109+
[HttpGet("GetCustomersCount")]
110+
public ActionResult<int> GetCustomersCount()
111+
{
112+
try
113+
{
114+
var count = customerService.GetAllAsQueryable().Count();
115+
return Ok(count);
116+
}
117+
catch (Exception error)
118+
{
119+
logger.LogError(error.Message);
120+
return StatusCode(500);
121+
}
122+
}
123+
105124
[HttpGet("{id}")]
106125
public ActionResult<CustomerDto> GetById(string id)
107126
{

NorthwindCRUD/Controllers/EmployeesController.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,27 @@ public ActionResult<PagedResultDto<EmployeeDto>> GetPagedEmployeesWithPage(
104104
}
105105
}
106106

107+
108+
/// <summary>
109+
/// Retrieves the total number of employees.
110+
/// </summary>
111+
/// <returns>Total count of employees as an integer.</returns>
112+
[HttpGet("GetEmployeesCount")]
113+
public ActionResult<int> GetEmployeesCount()
114+
{
115+
try
116+
{
117+
var count = employeeService.GetAllAsQueryable().Count();
118+
return Ok(count);
119+
}
120+
catch (Exception error)
121+
{
122+
logger.LogError(error.Message);
123+
return StatusCode(500);
124+
}
125+
}
126+
127+
107128
[HttpGet("{id}")]
108129
public ActionResult<EmployeeDto> GetById(int id)
109130
{

NorthwindCRUD/Controllers/OrdersController.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ public ActionResult<PagedResultDto<OrderDto>> GetPagedOrdersWithPage(
109109
}
110110
}
111111

112+
/// <summary>
113+
/// Retrieves the total number of orders.
114+
/// </summary>
115+
/// <returns>Total count of orders as an integer.</returns>
116+
[HttpGet("GetOrdersCount")]
117+
public ActionResult<int> GetOrdersCount()
118+
{
119+
try
120+
{
121+
var count = orderService.GetAllAsQueryable().Count();
122+
return Ok(count);
123+
}
124+
catch (Exception error)
125+
{
126+
logger.LogError(error.Message);
127+
return StatusCode(500);
128+
}
129+
}
130+
112131
[HttpGet("{id}")]
113132
public ActionResult<OrderDto> GetById(int id)
114133
{

NorthwindCRUD/Controllers/ProductsController.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ public ActionResult<PagedResultDto<ProductDto>> GetPagedProductsWithPage(
108108
}
109109
}
110110

111+
/// <summary>
112+
/// Retrieves the total number of products.
113+
/// </summary>
114+
/// <returns>Total count of products as an integer.</returns>
115+
[HttpGet("GetProductsCount")]
116+
public ActionResult<int> GetProductsCount()
117+
{
118+
try
119+
{
120+
var count = productService.GetAllAsQueryable().Count();
121+
return Ok(count);
122+
}
123+
catch (Exception error)
124+
{
125+
logger.LogError(error.Message);
126+
return StatusCode(500);
127+
}
128+
}
129+
111130
[HttpGet("{id}")]
112131
public ActionResult<ProductDto> GetById(int id)
113132
{

NorthwindCRUD/Controllers/RegionsController.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ public ActionResult<PagedResultDto<RegionDto>> GetPagedRegionsWithPage(
103103
}
104104
}
105105

106+
/// <summary>
107+
/// Retrieves the total number of regions.
108+
/// </summary>
109+
/// <returns>Total count of regions as an integer.</returns>
110+
[HttpGet("GetRegionsCount")]
111+
public ActionResult<int> GetRegionsCount()
112+
{
113+
try
114+
{
115+
var count = regionService.GetAllAsQueryable().Count();
116+
return Ok(count);
117+
}
118+
catch (Exception error)
119+
{
120+
logger.LogError(error.Message);
121+
return StatusCode(500);
122+
}
123+
}
124+
106125
[HttpGet("{id}")]
107126
public ActionResult<RegionDto> GetById(int id)
108127
{

NorthwindCRUD/Controllers/ShippersController.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ public ActionResult<PagedResultDto<ShipperDto>> GetPagedShippersWithPage(
103103
}
104104
}
105105

106+
/// <summary>
107+
/// Retrieves the total number of shippers.
108+
/// </summary>
109+
/// <returns>Total count of shippers as an integer.</returns>
110+
[HttpGet("GetShippersCount")]
111+
public ActionResult<int> GetShippersCount()
112+
{
113+
try
114+
{
115+
var count = shipperService.GetAllAsQueryable().Count();
116+
return Ok(count);
117+
}
118+
catch (Exception error)
119+
{
120+
logger.LogError(error.Message);
121+
return StatusCode(500);
122+
}
123+
}
124+
106125
[HttpGet("{id}")]
107126
public ActionResult<ShipperDto> GetById(int id)
108127
{

NorthwindCRUD/Controllers/SuppliersController.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ public ActionResult<PagedResultDto<SupplierDto>> GetPagedSuppliersWithPage(
103103
}
104104
}
105105

106+
/// <summary>
107+
/// Retrieves the total number of suppliers.
108+
/// </summary>
109+
/// <returns>Total count of suppliers as an integer.</returns>
110+
[HttpGet("GetSuppliersCount")]
111+
public ActionResult<int> GetSuppliersCount()
112+
{
113+
try
114+
{
115+
var count = supplierService.GetAllAsQueryable().Count();
116+
return Ok(count);
117+
}
118+
catch (Exception error)
119+
{
120+
logger.LogError(error.Message);
121+
return StatusCode(500);
122+
}
123+
}
124+
106125
[HttpGet("{id}")]
107126
public ActionResult<SupplierDto> GetById(int id)
108127
{

NorthwindCRUD/Controllers/TerritoriesController.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ public ActionResult<PagedResultDto<TerritoryDto>> GetPagedTerritoriesWithPage(
105105
}
106106
}
107107

108+
/// <summary>
109+
/// Retrieves the total number of territories.
110+
/// </summary>
111+
/// <returns>Total count of territories as an integer.</returns>
112+
[HttpGet("GetTerritoriesCount")]
113+
public ActionResult<int> GetTerritoriesCount()
114+
{
115+
try
116+
{
117+
var count = territoryService.GetAllAsQueryable().Count();
118+
return Ok(count);
119+
}
120+
catch (Exception error)
121+
{
122+
logger.LogError(error.Message);
123+
return StatusCode(500);
124+
}
125+
}
126+
108127
[HttpGet("{id}")]
109128
public ActionResult<TerritoryDto> GetById(string id)
110129
{

NorthwindCRUD/Services/CategoryService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public CategoryDb[] GetAll()
2020

2121
public IQueryable<CategoryDb> GetAllAsQueryable()
2222
{
23-
return this.dataContext.Categories.AsQueryable();
23+
return this.dataContext.Categories;
2424
}
2525

2626
public CategoryDb? GetById(int id)

0 commit comments

Comments
 (0)