Skip to content

Commit cdefb35

Browse files
committed
Refactor update order logic and response handling
Updated the `UpdateAsync` method in `IOrderService` to accept `OrderResponseDto` instead of `Order`. Enhanced the update logic in `OrderService` to include property assignments for existing orders and improved error handling. Modified controllers to return full response objects instead of just the data for consistency.
1 parent d756b3b commit cdefb35

File tree

3 files changed

+43
-57
lines changed

3 files changed

+43
-57
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
}

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
}

Services/OrderService.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public async Task<ApiResponse<IEnumerable<OrderResponseDto>>> GetAllAsync()
4040
return new ApiResponse<IEnumerable<OrderResponseDto>>
4141
{
4242
Succeeded = true,
43-
Data = orderResponseDtos
43+
Data = orderResponseDtos,
44+
Message = "Orders retrieved",
4445
};
4546
}
4647
catch (Exception ex)
@@ -220,28 +221,42 @@ public async Task<ApiResponse<OrderResponseDto>> CreateAsync(Order order, List<O
220221
}
221222
}
222223

223-
public async Task<ApiResponse<OrderResponseDto>> UpdateAsync(Order order)
224+
public async Task<ApiResponse<OrderResponseDto>> UpdateAsync(OrderResponseDto orderResponseDto)
224225
{
225226
try
226227
{
227-
await _orderRepository.UpdateAsync(order);
228-
229-
var updatedOrder = await _orderRepository.GetByIdAsync(order.Id);
230-
228+
var existingOrder = await _orderRepository.GetByIdAsync(orderResponseDto.Id);
229+
230+
if (existingOrder == null)
231+
throw new KeyNotFoundException($"Order with id: {orderResponseDto.Id} was not found");
232+
233+
existingOrder.CustomerName = orderResponseDto.CustomerName;
234+
existingOrder.CustomerEmail = orderResponseDto.CustomerEmail;
235+
existingOrder.CustomerEmail = orderResponseDto.CustomerEmail;
236+
existingOrder.Status = orderResponseDto.Status;
237+
existingOrder.ShippingAddress = orderResponseDto.ShippingAddress;
238+
existingOrder.BillingAddress = orderResponseDto.BillingAddress;
239+
existingOrder.PaymentMethod = orderResponseDto.PaymentMethod;
240+
existingOrder.ModifiedById = orderResponseDto.ModifiedById;
241+
existingOrder.Modified = orderResponseDto.Modified;
242+
243+
await _orderRepository.UpdateAsync(existingOrder);
244+
231245
return new ApiResponse<OrderResponseDto>
232246
{
233247
Succeeded = true,
234-
Data = MapToOrderResponseDto(updatedOrder!)
248+
Data = MapToOrderResponseDto(existingOrder!),
249+
Message = "Updated order successfully"
235250
};
236251
}
237252
catch (Exception ex)
238253
{
239-
_logger.LogError(ex, "Error updating order {Id}", order.Id);
254+
_logger.LogError(ex, "Error updating order {Id}", orderResponseDto.Id);
240255

241256
return new ApiResponse<OrderResponseDto>
242257
{
243258
Succeeded = false,
244-
Message = $"Error updating order {order.Id}",
259+
Message = $"Error updating order {orderResponseDto.Id}",
245260
Errors = new List<string> { ex.Message }
246261
};
247262
}

0 commit comments

Comments
 (0)