| | | 1 | | #include "machine_handler.h" |
| | | 2 | | #include "../../database/api_handlers.h" |
| | | 3 | | #include "../../database/machine_service.h" |
| | | 4 | | #include "../../database/cJSON.h" |
| | | 5 | | #include <string.h> |
| | | 6 | | #include <stdlib.h> |
| | | 7 | | #include <stdio.h> |
| | | 8 | | |
| | | 9 | | |
| | | 10 | | |
| | | 11 | | // Serialization is handled by api_handlers.c void serialize_machines_to_json is already defined there. |
| | | 12 | | |
| | 6 | 13 | | void handle_machine_request(HttpRequest *req, HttpResponse *res) { |
| | 6 | 14 | | if (strcmp(req->method, "GET") == 0) { |
| | 1 | 15 | | char *json = serialize_machines_to_json(); |
| | 1 | 16 | | res->status_code = 200; |
| | 1 | 17 | | strcpy(res->content_type, "application/json"); |
| | 1 | 18 | | strncpy(res->body, json, 8191); |
| | 1 | 19 | | res->body[8191] = '\0'; |
| | 1 | 20 | | free(json); |
| | 5 | 21 | | } else if (strcmp(req->method, "POST") == 0) { |
| | 5 | 22 | | cJSON *root = cJSON_Parse(req->body); |
| | | 23 | | |
| | 5 | 24 | | if (root) { |
| | 4 | 25 | | cJSON *name_item = cJSON_GetObjectItem(root, "name"); |
| | 4 | 26 | | cJSON *model_item = cJSON_GetObjectItem(root, "model"); |
| | 4 | 27 | | cJSON *location_item = cJSON_GetObjectItem(root, "location"); |
| | 4 | 28 | | cJSON *status_item = cJSON_GetObjectItem(root, "status"); |
| | | 29 | | |
| | 6 | 30 | | if (name_item && status_item) { |
| | 3 | 31 | | const char *name = name_item->valuestring; |
| | 3 | 32 | | const char *model = model_item ? model_item->valuestring : "Standard"; |
| | 3 | 33 | | const char *location = location_item ? location_item->valuestring : "Main Hall"; |
| | 3 | 34 | | const char *status = status_item->valuestring; |
| | | 35 | | // --- DUPLICATE CHECK --- |
| | | 36 | | Machine list[50]; |
| | 3 | 37 | | int count = get_all_machines(list, 50); |
| | 3 | 38 | | bool duplicate = false; |
| | | 39 | | |
| | 3 | 40 | | for (int i = 0; i < count; i++) { |
| | 1 | 41 | | if (strcmp(list[i].name, name) == 0) { |
| | 1 | 42 | | duplicate = true; |
| | 1 | 43 | | break; |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | 3 | 47 | | if (duplicate) { |
| | 1 | 48 | | res->status_code = 409; |
| | 1 | 49 | | strcpy(res->content_type, "application/json"); |
| | 1 | 50 | | strcpy(res->body, "{\"error\": \"A machine with this name already exists in the registry.\"}"); |
| | 1 | 51 | | cJSON_Delete(root); |
| | 1 | 52 | | return; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | // ------------------------ |
| | | 56 | | |
| | 2 | 57 | | if (add_machine(name, model, location, status)) { |
| | 1 | 58 | | res->status_code = 201; |
| | 1 | 59 | | strcpy(res->content_type, "application/json"); |
| | 1 | 60 | | strcpy(res->body, "{\"success\": true, \"message\": \"Machine onboarded successfully\"}"); |
| | | 61 | | } else { |
| | 1 | 62 | | res->status_code = 500; |
| | 1 | 63 | | strcpy(res->body, "{\"error\": \"Database error while onboarding machine\"}"); |
| | | 64 | | } |
| | | 65 | | } else { |
| | 1 | 66 | | res->status_code = 400; |
| | 1 | 67 | | strcpy(res->body, "{\"error\": \"Missing required fields: name, status\"}"); |
| | | 68 | | } |
| | | 69 | | |
| | 3 | 70 | | cJSON_Delete(root); |
| | | 71 | | } else { |
| | 1 | 72 | | res->status_code = 400; |
| | 1 | 73 | | strcpy(res->body, "{\"error\": \"Invalid JSON\"}"); |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | } |