| | | 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 | | |
| | | 8 | | // Using definition from database/api_handlers.c |
| | | 9 | | |
| | | 10 | | #include "../../database/inventory_service.h" |
| | | 11 | | |
| | 3 | 12 | | void handle_inventory_request(HttpRequest *req, HttpResponse *res) { |
| | 3 | 13 | | if (strcmp(req->method, "GET") == 0) { |
| | 1 | 14 | | char *json = serialize_inventory_to_json(); |
| | 1 | 15 | | res->status_code = 200; |
| | 1 | 16 | | strcpy(res->content_type, "application/json"); |
| | 1 | 17 | | strncpy(res->body, json, 8191); |
| | 1 | 18 | | res->body[8191] = '\0'; |
| | 1 | 19 | | free(json); |
| | 2 | 20 | | } else if (strcmp(req->method, "POST") == 0) { |
| | 1 | 21 | | cJSON *root = cJSON_Parse(req->body); |
| | | 22 | | |
| | 1 | 23 | | if (root) { |
| | 1 | 24 | | cJSON *name_item = cJSON_GetObjectItem(root, "part_name"); |
| | 1 | 25 | | cJSON *sku_item = cJSON_GetObjectItem(root, "sku"); |
| | 1 | 26 | | cJSON *qty_item = cJSON_GetObjectItem(root, "quantity"); |
| | 1 | 27 | | cJSON *min_item = cJSON_GetObjectItem(root, "min_stock"); |
| | 1 | 28 | | cJSON *cost_item = cJSON_GetObjectItem(root, "cost"); |
| | | 29 | | |
| | 2 | 30 | | if (name_item && sku_item) { |
| | 1 | 31 | | const char *name = name_item->valuestring; |
| | 1 | 32 | | const char *sku = sku_item->valuestring; |
| | 1 | 33 | | int qty = qty_item ? qty_item->valueint : 0; |
| | 1 | 34 | | int min = min_item ? min_item->valueint : 5; |
| | 1 | 35 | | double cost = cost_item ? cost_item->valuedouble : 0.0; |
| | | 36 | | // --- DUPLICATE CHECK (SKU) --- |
| | | 37 | | InventoryItem items[100]; |
| | 1 | 38 | | int count = get_all_inventory(items, 100); |
| | 1 | 39 | | bool duplicate = false; |
| | | 40 | | |
| | 2 | 41 | | for (int i = 0; i < count; i++) { |
| | 1 | 42 | | if (strcmp(items[i].sku, sku) == 0) { |
| | 0 | 43 | | duplicate = true; |
| | 0 | 44 | | break; |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | 1 | 48 | | if (duplicate) { |
| | 0 | 49 | | res->status_code = 409; |
| | 0 | 50 | | strcpy(res->content_type, "application/json"); |
| | 0 | 51 | | strcpy(res->body, "{\"error\": \"An item with this SKU already exists in inventory.\"}"); |
| | 0 | 52 | | cJSON_Delete(root); |
| | 0 | 53 | | return; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // ----------------------------- |
| | | 57 | | |
| | 1 | 58 | | if (add_inventory_item(name, sku, qty, min, cost)) { |
| | 1 | 59 | | res->status_code = 201; |
| | 1 | 60 | | strcpy(res->content_type, "application/json"); |
| | 1 | 61 | | strcpy(res->body, "{\"success\": true, \"message\": \"Inventory item added successfully\"}"); |
| | | 62 | | } else { |
| | 0 | 63 | | res->status_code = 500; |
| | 0 | 64 | | strcpy(res->body, "{\"error\": \"Database error while adding inventory item\"}"); |
| | | 65 | | } |
| | | 66 | | } else { |
| | 0 | 67 | | res->status_code = 400; |
| | 0 | 68 | | strcpy(res->body, "{\"error\": \"Missing required fields: part_name, sku\"}"); |
| | | 69 | | } |
| | | 70 | | |
| | 1 | 71 | | cJSON_Delete(root); |
| | | 72 | | } else { |
| | 0 | 73 | | res->status_code = 400; |
| | 0 | 74 | | strcpy(res->body, "{\"error\": \"Invalid JSON\"}"); |
| | | 75 | | } |
| | | 76 | | } else { |
| | 1 | 77 | | res->status_code = 405; |
| | 1 | 78 | | strcpy(res->body, "{\"error\": \"Method not supported\"}"); |
| | | 79 | | } |
| | | 80 | | } |