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