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