Skip to content

Commit 7b98cdd

Browse files
authored
Merge pull request #24 from design-sparx/feat/4-orders
Implement granular permissions and enhance order management
2 parents 921e68c + d8df3de commit 7b98cdd

9 files changed

+274
-146
lines changed

Controllers/OrdersController.cs

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task<ActionResult<IEnumerable<OrderResponseDto>>> GetAllOrders()
3333
return BadRequest(response);
3434
}
3535

36-
return Ok(response.Data);
36+
return Ok(response);
3737
}
3838

3939
[HttpGet("{id}")]
@@ -46,7 +46,7 @@ public async Task<ActionResult<OrderResponseDto>> GetOrderById(Guid id)
4646
return NotFound(response);
4747
}
4848

49-
return Ok(response.Data);
49+
return Ok(response);
5050
}
5151

5252
[HttpPost]
@@ -87,7 +87,7 @@ public async Task<ActionResult<OrderResponseDto>> CreateOrder(CreateOrderDto cre
8787
return BadRequest(response);
8888
}
8989

90-
return CreatedAtAction(nameof(GetOrderById), new { id = order.Id }, response.Data);
90+
return CreatedAtAction(nameof(GetOrderById), new { id = order.Id }, response);
9191
}
9292

9393
[HttpPut("{id}")]
@@ -101,49 +101,20 @@ public async Task<IActionResult> UpdateOrder(Guid id, UpdateOrderDto updateOrder
101101
return NotFound(orderResponse);
102102
}
103103

104-
var order = await _orderService.GetByIdAsync(id);
104+
var existingOrder = orderResponse.Data;
105+
existingOrder.CustomerName = updateOrderDto.CustomerName;
106+
existingOrder.CustomerEmail = updateOrderDto.CustomerEmail;
107+
existingOrder.CustomerEmail = updateOrderDto.CustomerEmail;
108+
existingOrder.Status = updateOrderDto.Status;
109+
existingOrder.ShippingAddress = updateOrderDto.ShippingAddress;
110+
existingOrder.BillingAddress = updateOrderDto.BillingAddress;
111+
existingOrder.PaymentMethod = updateOrderDto.PaymentMethod;
112+
existingOrder.ModifiedById = updateOrderDto.ModifiedById;
113+
existingOrder.Modified = DateTime.UtcNow;
105114

106-
if (!order.Succeeded)
107-
{
108-
return NotFound(order);
109-
}
110-
111-
var existingOrder = await _orderService.GetByIdAsync(id);
112-
113-
if (!existingOrder.Succeeded)
114-
{
115-
return NotFound(existingOrder);
116-
}
117-
118-
var orderEntity = new Order
119-
{
120-
Id = id,
121-
122-
// Update customer information
123-
CustomerName = updateOrderDto.CustomerName,
124-
CustomerEmail = updateOrderDto.CustomerEmail,
125-
CustomerPhone = updateOrderDto.CustomerPhone,
126-
127-
OrderDate = existingOrder.Data.OrderDate,
128-
TotalAmount = existingOrder.Data.TotalAmount,
129-
Status = updateOrderDto.Status,
130-
ShippingAddress = updateOrderDto.ShippingAddress ?? existingOrder.Data.ShippingAddress,
131-
BillingAddress = updateOrderDto.BillingAddress ?? existingOrder.Data.BillingAddress,
132-
PaymentMethod = updateOrderDto.PaymentMethod ?? existingOrder.Data.PaymentMethod,
133-
Created = existingOrder.Data.Created,
134-
CreatedById = existingOrder.Data.CreatedById,
135-
Modified = DateTime.UtcNow,
136-
ModifiedById = updateOrderDto.ModifiedById
137-
};
138-
139-
var updateResponse = await _orderService.UpdateAsync(orderEntity);
140-
141-
if (!updateResponse.Succeeded)
142-
{
143-
return BadRequest(updateResponse);
144-
}
115+
await _orderService.UpdateAsync(existingOrder);
145116

146-
return Ok(updateResponse.Data);
117+
return Ok(existingOrder);
147118
}
148119

149120
[HttpDelete("{id}")]
@@ -177,7 +148,7 @@ public async Task<ActionResult<IEnumerable<OrderResponseDto>>> GetOrdersByCustom
177148
return BadRequest(response);
178149
}
179150

180-
return Ok(response.Data);
151+
return Ok(response);
181152
}
182153

183154
[HttpGet("status/{status}")]
@@ -190,7 +161,7 @@ public async Task<ActionResult<IEnumerable<OrderResponseDto>>> GetOrdersByStatus
190161
return BadRequest(response);
191162
}
192163

193-
return Ok(response.Data);
164+
return Ok(response);
194165
}
195166

196167
[HttpGet("customer-info")]
@@ -210,6 +181,6 @@ public async Task<ActionResult<CustomerInfo>> GetCustomerInfo()
210181
return BadRequest(response);
211182
}
212183

213-
return Ok(response.Data);
184+
return Ok(response);
214185
}
215186
}

Controllers/ProductCategoriesController.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
using AdminHubApi.Dtos.ProductCategory;
1+
using AdminHubApi.Constants;
2+
using AdminHubApi.Dtos.ProductCategory;
23
using AdminHubApi.Entities;
34
using AdminHubApi.Interfaces;
5+
using AdminHubApi.Security;
46
using Microsoft.AspNetCore.Authorization;
57
using Microsoft.AspNetCore.Mvc;
68

79
namespace AdminHubApi.Controllers;
810

911
[ApiController]
1012
[Route("api/product-categories")]
11-
[Authorize]
13+
[PermissionAuthorize(Permissions.ProductCategories.View)]
1214
public class ProductCategoriesController : ControllerBase
1315
{
1416
private readonly IProductCategoryService _productCategoryService;
@@ -43,6 +45,7 @@ public async Task<ActionResult<ProductCategory>> GetCategory(Guid id)
4345
}
4446

4547
[HttpPost]
48+
[PermissionAuthorize(Permissions.ProductCategories.Create)]
4649
public async Task<ActionResult<ProductCategory>> CreateCategory(CreateProductCategoryDto productCategoryDto)
4750
{
4851
var productCategory = new ProductCategory
@@ -67,6 +70,7 @@ public async Task<ActionResult<ProductCategory>> CreateCategory(CreateProductCat
6770
}
6871

6972
[HttpPut("{id}")]
73+
[PermissionAuthorize(Permissions.ProductCategories.Edit)]
7074
public async Task<IActionResult> UpdateCategory(Guid id, UpdateProductCategoryDto updateProductCategoryDto)
7175
{
7276
var productCategoryResponse = await _productCategoryService.GetByIdAsync(id);
@@ -89,6 +93,7 @@ public async Task<IActionResult> UpdateCategory(Guid id, UpdateProductCategoryDt
8993
}
9094

9195
[HttpDelete("{id}")]
96+
[PermissionAuthorize(Permissions.ProductCategories.Delete)]
9297
public async Task<IActionResult> DeleteCategory(Guid id)
9398
{
9499
var productCategoryResponse = await _productCategoryService.GetByIdAsync(id);

Controllers/ProductsController.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
using AdminHubApi.Dtos.Products;
1+
using AdminHubApi.Constants;
2+
using AdminHubApi.Dtos.Products;
23
using AdminHubApi.Entities;
34
using AdminHubApi.Interfaces;
5+
using AdminHubApi.Security;
46
using Microsoft.AspNetCore.Authorization;
57
using Microsoft.AspNetCore.Mvc;
68

79
namespace AdminHubApi.Controllers;
810

911
[ApiController]
1012
[Route("/api/products")]
13+
[PermissionAuthorize(Permissions.Products.View)]
1114
public class ProductsController : ControllerBase
1215
{
1316
private readonly IProductService _productService;
@@ -41,6 +44,7 @@ public async Task<ActionResult<ProductDto>> GetProductById(Guid id)
4144
}
4245

4346
[HttpPost]
47+
[PermissionAuthorize(Permissions.Products.Create)]
4448
public async Task<ActionResult> CreateProduct(CreateProductDto createProductDto)
4549
{
4650
var product = new Product
@@ -70,7 +74,7 @@ public async Task<ActionResult> CreateProduct(CreateProductDto createProductDto)
7074
}
7175

7276
[HttpPut("{id}")]
73-
[Authorize]
77+
[PermissionAuthorize(Permissions.Products.Edit)]
7478
public async Task<IActionResult> UpdateProduct(Guid id, UpdateProductDto updateProductDto)
7579
{
7680
var productResponse = await _productService.GetByIdAsync(id);
@@ -92,7 +96,7 @@ public async Task<IActionResult> UpdateProduct(Guid id, UpdateProductDto updateP
9296
}
9397

9498
[HttpDelete("{id}")]
95-
[Authorize]
99+
[PermissionAuthorize(Permissions.Products.Delete)]
96100
public async Task<IActionResult> DeleteProduct(Guid id)
97101
{
98102
var productResponse = await _productService.GetByIdAsync(id);

Interfaces/IOrderService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IOrderService
1111
Task<ApiResponse<IEnumerable<OrderResponseDto>>> GetByCustomerIdAsync(string customerId);
1212
Task<ApiResponse<IEnumerable<OrderResponseDto>>> GetByStatusAsync(OrderStatus status);
1313
Task<ApiResponse<OrderResponseDto>> CreateAsync(Order order, List<OrderItem> orderItems);
14-
Task<ApiResponse<OrderResponseDto>> UpdateAsync(Order order);
14+
Task<ApiResponse<OrderResponseDto>> UpdateAsync(OrderResponseDto orderResponseDto);
1515
Task<ApiResponse<bool>> DeleteAsync(Guid id);
1616
Task<ApiResponse<CustomerInfo>> GetCustomerInfoAsync(string customerId);
1717
}

Program.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using AdminHubApi.Interfaces;
77
using AdminHubApi.Repositories;
88
using AdminHubApi.Security;
9+
using AdminHubApi.Security.Permissions;
910
using AdminHubApi.Services;
1011
using Microsoft.AspNetCore.Authentication.JwtBearer;
1112
using Microsoft.AspNetCore.Authorization;
@@ -143,7 +144,10 @@ await tokenBlacklistRepository.IsTokenBlacklistedAsync(tokenId))
143144
// Register token cleanup background service
144145
builder.Services.AddHostedService<TokenCleanupService>();
145146

146-
// Custom Authorization Handler
147+
// Add permission message service
148+
builder.Services.AddSingleton<IPermissionMessageService, PermissionMessageService>();
149+
150+
// Register the custom authorization handler
147151
builder.Services.AddSingleton<IAuthorizationMiddlewareResultHandler, CustomAuthorizationMiddlewareResultHandler>();
148152

149153
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
@@ -186,12 +190,12 @@ await tokenBlacklistRepository.IsTokenBlacklistedAsync(tokenId))
186190
await NormalUserSeeder.SeedNormalUserAsync(app.Services);
187191
await ManagerUserSeeder.SeedManagerUserAsync(app.Services);
188192
}
189-
193+
190194
// Always update permissions to ensure new permissions are added
191195
logger.LogInformation("Updating role permissions...");
192196
await PermissionUpdateSeeder.UpdateRolePermissionsAsync(app.Services);
193197
logger.LogInformation("Role permissions updated successfully");
194-
198+
195199
logger.LogInformation("Updating user permissions...");
196200
await UserPermissionUpdateSeeder.UpdateUserPermissionsAsync(app.Services);
197201
logger.LogInformation("User permissions updated successfully");

0 commit comments

Comments
 (0)