1
+ using Microsoft . AspNetCore . Mvc ;
2
+ using Microsoft . AspNetCore . OData . Query ;
3
+ using Microsoft . AspNetCore . OData . Routing . Controllers ;
4
+ using ODataV4Adaptor . Server . Models ;
5
+
6
+ namespace OdataV4Adaptor . Server . Controllers
7
+ {
8
+
9
+ public class OrdersController : Controller
10
+ {
11
+ /// <summary>
12
+ /// Retrieves all orders.
13
+ /// </summary>
14
+ /// <returns>The collection of orders.</returns>
15
+ [ HttpGet ]
16
+ [ EnableQuery ]
17
+ public IActionResult Get ( )
18
+ {
19
+ var data = OrdersDetails . GetAllRecords ( ) . AsQueryable ( ) ;
20
+ return Ok ( data ) ;
21
+ }
22
+
23
+ /// <summary>
24
+ /// Inserts a new order to the collection.
25
+ /// </summary>
26
+ /// <param name="addRecord">The order to be inserted.</param>
27
+ /// <returns>It returns the newly inserted record detail.</returns>
28
+ [ HttpPost ]
29
+ [ EnableQuery ]
30
+ public IActionResult Post ( [ FromBody ] OrdersDetails addRecord )
31
+ {
32
+ if ( addRecord == null )
33
+ {
34
+ return BadRequest ( "Null order" ) ;
35
+ }
36
+ OrdersDetails . GetAllRecords ( ) . Insert ( 0 , addRecord ) ;
37
+ return Json ( addRecord ) ;
38
+ }
39
+
40
+ /// <summary>
41
+ /// Updates an existing order.
42
+ /// </summary>
43
+ /// <param name="key">The ID of the order to update.</param>
44
+ /// <param name="updateRecord">The updated order details.</param>
45
+ /// <returns>It returns the updated order details.</returns>
46
+ [ HttpPatch ( "{key}" ) ]
47
+ public IActionResult Patch ( int key , [ FromBody ] OrdersDetails updateRecord )
48
+ {
49
+ if ( updateRecord == null )
50
+ {
51
+ return BadRequest ( "No records" ) ;
52
+ }
53
+ var existingOrder = OrdersDetails . GetAllRecords ( ) . FirstOrDefault ( order => order . OrderID == key ) ;
54
+ if ( existingOrder != null )
55
+ {
56
+ // If the order exists, update its properties
57
+ existingOrder . CustomerID = updateRecord . CustomerID ?? existingOrder . CustomerID ;
58
+ existingOrder . EmployeeID = updateRecord . EmployeeID ?? existingOrder . EmployeeID ;
59
+ existingOrder . ShipCountry = updateRecord . ShipCountry ?? existingOrder . ShipCountry ;
60
+ }
61
+ return Json ( updateRecord ) ;
62
+ }
63
+
64
+ /// <summary>
65
+ /// Deletes an order.
66
+ /// </summary>
67
+ /// <param name="key">The ID of the order to delete.</param>
68
+ /// <returns>It returns the deleted record detail</returns>
69
+ [ HttpDelete ( "{key}" ) ]
70
+ public IActionResult Delete ( int key )
71
+ {
72
+ var deleteRecord = OrdersDetails . GetAllRecords ( ) . FirstOrDefault ( order => order . OrderID == key ) ;
73
+ if ( deleteRecord != null )
74
+ {
75
+ OrdersDetails . GetAllRecords ( ) . Remove ( deleteRecord ) ;
76
+ }
77
+ return Json ( deleteRecord ) ;
78
+ }
79
+ }
80
+ }
0 commit comments