| | | 1 | | #include "inventory_handler.h" |
| | | 2 | | #include "../../database/api_handlers.h" |
| | | 3 | | #include "../../database/cJSON.h" |
| | | 4 | | #include <string.h> |
| | | 5 | | #include <stdlib.h> |
| | | 6 | | #include <stdio.h> |
| | | 7 | | |
| | 1 | 8 | | char *serialize_inventory_to_json(void) { |
| | 1 | 9 | | cJSON *root = cJSON_CreateObject(); |
| | 1 | 10 | | cJSON *item_array = cJSON_CreateArray(); |
| | 1 | 11 | | cJSON *item1 = cJSON_CreateObject(); |
| | 1 | 12 | | cJSON_AddNumberToObject(item1, "id", 1); |
| | 1 | 13 | | cJSON_AddStringToObject(item1, "name", "Motor Oil"); |
| | 1 | 14 | | cJSON_AddNumberToObject(item1, "quantity", 25); |
| | 1 | 15 | | cJSON_AddItemToArray(item_array, item1); |
| | 1 | 16 | | cJSON_AddItemToObject(root, "inventory", item_array); |
| | 1 | 17 | | char *json = cJSON_Print(root); |
| | 1 | 18 | | cJSON_Delete(root); |
| | 1 | 19 | | return json; |
| | | 20 | | } |
| | | 21 | | |
| | 2 | 22 | | void handle_inventory_request(HttpRequest *req, HttpResponse *res) { |
| | 2 | 23 | | if (strcmp(req->method, "GET") == 0) { |
| | 1 | 24 | | char *json = serialize_inventory_to_json(); |
| | 1 | 25 | | res->status_code = 200; |
| | 1 | 26 | | strcpy(res->content_type, "application/json"); |
| | 1 | 27 | | strncpy(res->body, json, 8191); |
| | 1 | 28 | | free(json); |
| | | 29 | | } else { |
| | 1 | 30 | | res->status_code = 405; // Method Not Allowed |
| | 1 | 31 | | strcpy(res->body, "{\"error\": \"Method not supported for inventory\"}"); |
| | | 32 | | } |
| | 2 | 33 | | } |