11using AutoMapper ;
22using Microsoft . AspNetCore . Authorization ;
3+ using Microsoft . AspNetCore . Http . HttpResults ;
34using Microsoft . AspNetCore . Mvc ;
45using NorthWindAPI . Controllers . Models . Requests ;
56using NorthWindAPI . Controllers . Models . Responses ;
@@ -12,13 +13,15 @@ namespace NorthWindAPI.Controllers
1213 public class OrderController (
1314 IOrderService orderService ,
1415 ICustomerService customerService ,
16+ IProductService productService ,
1517 IUserService employeeService ,
1618 IMapper mapper ,
1719 ILogger < OrderController > logger
1820 ) : ControllerBase
1921 {
2022 private readonly IOrderService _orderService = orderService ;
2123 private readonly ICustomerService _customerService = customerService ;
24+ private readonly IProductService _productService = productService ;
2225 private readonly IUserService _employeeService = employeeService ;
2326
2427 private readonly IMapper _mapper = mapper ;
@@ -119,6 +122,9 @@ public async Task<ActionResult<OrderResponse>> Create(NewOrderRequest newOrder)
119122 {
120123 try
121124 {
125+ //Update product stock - subtract quantity ordered from units in stock
126+ await _productService . RemoveStock ( newOrder . OrderDetail ) ;
127+
122128 var order = await _orderService . ProcessNewOrder ( newOrder ) ;
123129
124130 if ( order == null )
@@ -136,9 +142,9 @@ public async Task<ActionResult<OrderResponse>> Create(NewOrderRequest newOrder)
136142
137143 return orderResponse ;
138144 }
139- catch
145+ catch ( Exception ex )
140146 {
141- return Forbid ( ) ;
147+ return Forbid ( ex . Message ) ;
142148 }
143149 }
144150
@@ -259,10 +265,20 @@ public async Task<ActionResult<ShipOptionResponse>> Carriers()
259265 [ ProducesResponseType ( typeof ( NoContentResult ) , StatusCodes . Status200OK ) ]
260266 [ ProducesResponseType ( typeof ( BadRequestResult ) , StatusCodes . Status400BadRequest ) ]
261267 [ ProducesResponseType ( typeof ( UnauthorizedResult ) , StatusCodes . Status401Unauthorized ) ]
268+ [ ProducesResponseType ( typeof ( NotFoundResult ) , StatusCodes . Status404NotFound ) ]
262269 [ Authorize ]
263270 [ HttpDelete ( "{id}" ) ]
264271 public async Task < ActionResult > Delete ( int id )
265272 {
273+ var order = await _orderService . FindOrder ( id ) ;
274+ if ( order == null )
275+ {
276+ return NotFound ( ) ;
277+ }
278+
279+ //Update product stock - add quantity ordered from order to be deleted
280+ await _productService . ReplaceStock ( order . Items ) ;
281+
266282 var result = await _orderService . RemoveOrder ( id ) ;
267283 if ( ! result )
268284 {
0 commit comments