| | | 1 | | #include "inventory_service.h" |
| | | 2 | | #include "db_connection.h" |
| | | 3 | | #include "logger.h" |
| | | 4 | | #include <stdio.h> |
| | | 5 | | #include <stdlib.h> |
| | | 6 | | #include <string.h> |
| | | 7 | | |
| | | 8 | | #ifndef TEST_MODE |
| | | 9 | | |
| | 3 | 10 | | int get_all_inventory(InventoryItem *out_items, int max_items) { |
| | 3 | 11 | | if (!out_items || max_items <= 0) |
| | 1 | 12 | | return 0; |
| | | 13 | | |
| | 2 | 14 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 15 | | |
| | 2 | 16 | | if (!conn_wrapper) |
| | 1 | 17 | | return 0; |
| | | 18 | | |
| | 1 | 19 | | const char *query = |
| | | 20 | | "SELECT id, part_name, sku, quantity, min_stock_level, unit_cost, " |
| | | 21 | | "to_char(last_restocked_at, 'YYYY-MM-DD HH24:MI:SS') " |
| | | 22 | | "FROM inventory ORDER BY id ASC;"; |
| | 1 | 23 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | | 24 | | |
| | 1 | 25 | | if (PQresultStatus(res) != PGRES_TUPLES_OK) { |
| | 0 | 26 | | LOG_ERROR("Failed to fetch inventory: %s", |
| | | 27 | | PQerrorMessage(conn_wrapper->pg_conn)); |
| | 0 | 28 | | PQclear(res); |
| | 0 | 29 | | db_pool_release(conn_wrapper); |
| | 0 | 30 | | return 0; |
| | | 31 | | } |
| | | 32 | | |
| | 1 | 33 | | int rows = PQntuples(res); |
| | 1 | 34 | | int count = (rows < max_items) ? rows : max_items; |
| | | 35 | | |
| | 3 | 36 | | for (int i = 0; i < count; i++) { |
| | 2 | 37 | | out_items[i].id = atoi(PQgetvalue(res, i, 0)); |
| | 2 | 38 | | strncpy(out_items[i].part_name, PQgetvalue(res, i, 1), 99); |
| | 2 | 39 | | out_items[i].part_name[99] = '\0'; |
| | 2 | 40 | | strncpy(out_items[i].sku, PQgetvalue(res, i, 2), 49); |
| | 2 | 41 | | out_items[i].sku[49] = '\0'; |
| | 2 | 42 | | out_items[i].quantity = atoi(PQgetvalue(res, i, 3)); |
| | 2 | 43 | | out_items[i].min_stock_level = atoi(PQgetvalue(res, i, 4)); |
| | 2 | 44 | | out_items[i].unit_cost = atof(PQgetvalue(res, i, 5)); |
| | 2 | 45 | | strncpy(out_items[i].last_restocked_at, |
| | 2 | 46 | | PQgetvalue(res, i, 6), 31); |
| | 2 | 47 | | out_items[i].last_restocked_at[31] = '\0'; |
| | | 48 | | } |
| | | 49 | | |
| | 1 | 50 | | PQclear(res); |
| | 1 | 51 | | db_pool_release(conn_wrapper); |
| | 1 | 52 | | return count; |
| | | 53 | | } |
| | | 54 | | |
| | 1 | 55 | | bool update_inventory_quantity(int part_id, int change) { |
| | 1 | 56 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 57 | | |
| | 1 | 58 | | if (!conn_wrapper) |
| | 0 | 59 | | return false; |
| | | 60 | | |
| | | 61 | | char query[256]; |
| | 1 | 62 | | snprintf(query, sizeof(query), |
| | | 63 | | "UPDATE inventory SET quantity = quantity + (%d) WHERE id = %d;", |
| | | 64 | | change, part_id); |
| | 1 | 65 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | 1 | 66 | | bool success = (PQresultStatus(res) == PGRES_COMMAND_OK); |
| | | 67 | | |
| | 1 | 68 | | if (!success) { |
| | 0 | 69 | | LOG_ERROR("Failed to update inventory: %s", |
| | | 70 | | PQerrorMessage(conn_wrapper->pg_conn)); |
| | | 71 | | } |
| | | 72 | | |
| | 1 | 73 | | PQclear(res); |
| | 1 | 74 | | db_pool_release(conn_wrapper); |
| | 1 | 75 | | return success; |
| | | 76 | | } |
| | | 77 | | |
| | 1 | 78 | | int get_low_stock_items(InventoryItem *out_items, int max_items) { |
| | 1 | 79 | | if (!out_items || max_items <= 0) |
| | 0 | 80 | | return 0; |
| | | 81 | | |
| | 1 | 82 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 83 | | |
| | 1 | 84 | | if (!conn_wrapper) |
| | 0 | 85 | | return 0; |
| | | 86 | | |
| | 1 | 87 | | const char *query = |
| | | 88 | | "SELECT id, part_name, sku, quantity, min_stock_level, unit_cost, " |
| | | 89 | | "to_char(last_restocked_at, 'YYYY-MM-DD HH24:MI:SS') " |
| | | 90 | | "FROM inventory WHERE quantity < min_stock_level;"; |
| | 1 | 91 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | | 92 | | |
| | 1 | 93 | | if (PQresultStatus(res) != PGRES_TUPLES_OK) { |
| | 0 | 94 | | PQclear(res); |
| | 0 | 95 | | db_pool_release(conn_wrapper); |
| | 0 | 96 | | return 0; |
| | | 97 | | } |
| | | 98 | | |
| | 1 | 99 | | int rows = PQntuples(res); |
| | 1 | 100 | | int count = (rows < max_items) ? rows : max_items; |
| | | 101 | | |
| | 3 | 102 | | for (int i = 0; i < count; i++) { |
| | 2 | 103 | | out_items[i].id = atoi(PQgetvalue(res, i, 0)); |
| | 2 | 104 | | strncpy(out_items[i].part_name, PQgetvalue(res, i, 1), 99); |
| | 2 | 105 | | out_items[i].part_name[99] = '\0'; |
| | 2 | 106 | | strncpy(out_items[i].sku, PQgetvalue(res, i, 2), 49); |
| | 2 | 107 | | out_items[i].sku[49] = '\0'; |
| | 2 | 108 | | out_items[i].quantity = atoi(PQgetvalue(res, i, 3)); |
| | 2 | 109 | | out_items[i].min_stock_level = atoi(PQgetvalue(res, i, 4)); |
| | 2 | 110 | | out_items[i].unit_cost = atof(PQgetvalue(res, i, 5)); |
| | 2 | 111 | | strncpy(out_items[i].last_restocked_at, |
| | 2 | 112 | | PQgetvalue(res, i, 6), 31); |
| | 2 | 113 | | out_items[i].last_restocked_at[31] = '\0'; |
| | | 114 | | } |
| | | 115 | | |
| | 1 | 116 | | PQclear(res); |
| | 1 | 117 | | db_pool_release(conn_wrapper); |
| | 1 | 118 | | return count; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | #endif // TEST_MODE |
| | | 122 | | |
| | | 123 | | |
| | | 124 | | // ==================================================================== |
| | | 125 | | // MOCK IMPLEMENTATIONS FOR TESTING |
| | | 126 | | // ==================================================================== |
| | | 127 | | |
| | | 128 | | #ifdef TEST_MODE |
| | | 129 | | |
| | | 130 | | int get_all_inventory(InventoryItem *items, int max_count) { |
| | | 131 | | if (!items || max_count <= 0) |
| | | 132 | | return 0; |
| | | 133 | | |
| | | 134 | | items[0].id = 1; |
| | | 135 | | strcpy(items[0].part_name, "Motor Oil"); |
| | | 136 | | strcpy(items[0].sku, "MO-001"); |
| | | 137 | | items[0].quantity = 50; |
| | | 138 | | items[0].min_stock_level = 10; |
| | | 139 | | items[0].unit_cost = 25.5; |
| | | 140 | | strcpy(items[0].last_restocked_at, "2025-01-01"); |
| | | 141 | | return 1; |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | bool update_inventory_quantity(int part_id, int change) { |
| | | 145 | | (void)part_id; |
| | | 146 | | (void)change; |
| | | 147 | | return true; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | int get_low_stock_items(InventoryItem *items, int max_count) { |
| | | 151 | | if (!items || max_count <= 0) |
| | | 152 | | return 0; |
| | | 153 | | |
| | | 154 | | items[0].id = 2; |
| | | 155 | | items[0].quantity = 5; |
| | | 156 | | items[0].min_stock_level = 10; |
| | | 157 | | return 1; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | #endif |