-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderController.cs
More file actions
78 lines (63 loc) · 1.88 KB
/
OrderController.cs
File metadata and controls
78 lines (63 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using BusinessLayer.DTOs.OrderDTOs;
using BusinessLayer.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;
namespace WebAPI.Controllers;
[Route("api/[controller]")]
[ApiController]
public class OrderController : ControllerBase
{
private readonly IOrderService _orderService;
public OrderController(IOrderService orderService)
{
_orderService = orderService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<ResponseOrderDto>>> GetOrders(
[FromQuery] GetManyOrderDto getManyOrdersDto
)
{
var orders = await _orderService.GetAllAsync(getManyOrdersDto);
return Ok(orders);
}
[HttpGet("{id}")]
public async Task<ActionResult<ResponseOrderDto>> GetOrder(int id)
{
var responseOrderData = await _orderService.GetOrderByIdAsync(id);
if (responseOrderData == null)
{
return NotFound();
}
return responseOrderData;
}
// TODO: Protect for admin access only
[HttpPut]
public async Task<IActionResult> PutOrder([FromBody] UpdateOrderDto updateOrderDto)
{
var oldUpdatedOrder = await _orderService.UpdateOrderAsync(updateOrderDto);
if (oldUpdatedOrder == null)
{
return NotFound();
}
return Ok(oldUpdatedOrder);
}
[HttpPost]
public async Task<ActionResult<ResponseOrderDto>> PostOrder([FromBody] AddOrderDto addOrderDto)
{
var newOrder = await _orderService.AddOrderAsync(addOrderDto);
if (newOrder == null)
{
return NotFound();
}
return Ok(newOrder);
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteOrder(int id)
{
var deletedOrder = await _orderService.DeleteOrderByIdAsync(id);
if (deletedOrder == null)
{
return NotFound();
}
return Ok(deletedOrder);
}
}