| | | 1 | | #include "maintenance_handler.h" |
| | | 2 | | #include "../../database/api_handlers.h" |
| | | 3 | | #include <string.h> |
| | | 4 | | #include <stdlib.h> |
| | | 5 | | #include <stdio.h> |
| | | 6 | | #include "../../database/cJSON.h" |
| | | 7 | | |
| | 2 | 8 | | char *serialize_maintenance_to_json(void) { |
| | 2 | 9 | | cJSON *root = cJSON_CreateObject(); |
| | 2 | 10 | | cJSON *log_array = cJSON_CreateArray(); |
| | 2 | 11 | | cJSON *log1 = cJSON_CreateObject(); |
| | 2 | 12 | | cJSON_AddNumberToObject(log1, "id", 1); |
| | 2 | 13 | | cJSON_AddNumberToObject(log1, "machine_id", 1); |
| | 2 | 14 | | cJSON_AddStringToObject(log1, "description", "Routine oil change"); |
| | 2 | 15 | | cJSON_AddStringToObject(log1, "status", "completed"); |
| | 2 | 16 | | cJSON_AddItemToArray(log_array, log1); |
| | 2 | 17 | | cJSON_AddItemToObject(root, "maintenance_logs", log_array); |
| | 2 | 18 | | char *json = cJSON_Print(root); |
| | 2 | 19 | | cJSON_Delete(root); |
| | 2 | 20 | | return json; |
| | | 21 | | } |
| | | 22 | | |
| | 2 | 23 | | void handle_maintenance_request(HttpRequest *req, HttpResponse *res) { |
| | 2 | 24 | | if (strcmp(req->method, "GET") == 0) { |
| | 1 | 25 | | char *json = serialize_maintenance_to_json(); |
| | 1 | 26 | | res->status_code = 200; |
| | 1 | 27 | | strcpy(res->content_type, "application/json"); |
| | 1 | 28 | | strncpy(res->body, json, 8191); |
| | 1 | 29 | | free(json); |
| | | 30 | | } else { |
| | 1 | 31 | | res->status_code = 405; |
| | 1 | 32 | | strcpy(res->body, "{\"error\": \"Only GET supported for maintenance via modular API currently\"}"); |
| | | 33 | | } |
| | 2 | 34 | | } |