Claude opus authored everything to make the user and task work. First iteration
This commit is contained in:
67
Provider/main/api/users/add.cpp
Normal file
67
Provider/main/api/users/add.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
// POST /api/users — Create a new user
|
||||
// Body: {"name": "Alice"}
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
#include "types.hpp"
|
||||
#include "user.hpp"
|
||||
|
||||
|
||||
internal esp_err_t api_users_post_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
|
||||
char buf[128];
|
||||
int received = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
||||
if (received <= 0)
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Empty body");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
buf[received] = '\0';
|
||||
|
||||
cJSON *body = cJSON_Parse(buf);
|
||||
if (!body)
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
cJSON *name_item = cJSON_GetObjectItem(body, "name");
|
||||
if (!cJSON_IsString(name_item) || strlen(name_item->valuestring) == 0)
|
||||
{
|
||||
cJSON_Delete(body);
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing 'name'");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
user_t *user = add_user(name_item->valuestring);
|
||||
cJSON_Delete(body);
|
||||
|
||||
if (!user)
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
|
||||
"User limit reached");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
cJSON *resp = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(resp, "id", user->id);
|
||||
cJSON_AddStringToObject(resp, "name", user->name);
|
||||
|
||||
const char *json = cJSON_PrintUnformatted(resp);
|
||||
httpd_resp_sendstr(req, json);
|
||||
|
||||
free((void *)json);
|
||||
cJSON_Delete(resp);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
internal const httpd_uri_t api_users_post_uri = {.uri = "/api/users",
|
||||
.method = HTTP_POST,
|
||||
.handler =
|
||||
api_users_post_handler,
|
||||
.user_ctx = NULL};
|
||||
41
Provider/main/api/users/list.cpp
Normal file
41
Provider/main/api/users/list.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
// GET /api/users — List all active users
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
#include "types.hpp"
|
||||
#include "user.hpp"
|
||||
|
||||
|
||||
internal esp_err_t api_users_get_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
|
||||
cJSON *arr = cJSON_CreateArray();
|
||||
|
||||
for (int i = 0; i < MAX_USERS; i++)
|
||||
{
|
||||
if (g_Users[i].active)
|
||||
{
|
||||
cJSON *obj = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(obj, "id", g_Users[i].id);
|
||||
cJSON_AddStringToObject(obj, "name", g_Users[i].name);
|
||||
cJSON_AddItemToArray(arr, obj);
|
||||
}
|
||||
}
|
||||
|
||||
const char *json = cJSON_PrintUnformatted(arr);
|
||||
httpd_resp_sendstr(req, json);
|
||||
|
||||
free((void *)json);
|
||||
cJSON_Delete(arr);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
internal const httpd_uri_t api_users_get_uri = {.uri = "/api/users",
|
||||
.method = HTTP_GET,
|
||||
.handler =
|
||||
api_users_get_handler,
|
||||
.user_ctx = NULL};
|
||||
47
Provider/main/api/users/remove.cpp
Normal file
47
Provider/main/api/users/remove.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
// DELETE /api/users?id=N — Delete a user and cascade-delete their tasks
|
||||
|
||||
#include "esp_http_server.h"
|
||||
|
||||
#include "api/tasks/store.hpp"
|
||||
#include "api/users/store.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
internal esp_err_t api_users_delete_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
|
||||
char query[32] = {};
|
||||
if (httpd_req_get_url_query_str(req, query, sizeof(query)) != ESP_OK)
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing query param 'id'");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
char id_str[8] = {};
|
||||
if (httpd_query_key_value(query, "id", id_str, sizeof(id_str)) != ESP_OK)
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing query param 'id'");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
uint8 id = (uint8)atoi(id_str);
|
||||
|
||||
// Cascade: remove all tasks belonging to this user
|
||||
remove_tasks_for_user(id);
|
||||
|
||||
if (!remove_user(id))
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "User not found");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
httpd_resp_sendstr(req, "{\"status\":\"ok\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
internal const httpd_uri_t api_users_delete_uri = {.uri = "/api/users",
|
||||
.method = HTTP_DELETE,
|
||||
.handler =
|
||||
api_users_delete_handler,
|
||||
.user_ctx = NULL};
|
||||
56
Provider/main/api/users/store.cpp
Normal file
56
Provider/main/api/users/store.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
// User data store: CRUD helpers and seed data
|
||||
|
||||
#include "api/users/store.hpp"
|
||||
|
||||
// Find a user by ID, returns nullptr if not found
|
||||
internal user_t *find_user(uint8 id)
|
||||
{
|
||||
for (int i = 0; i < MAX_USERS; i++)
|
||||
{
|
||||
if (g_Users[i].active && g_Users[i].id == id)
|
||||
{
|
||||
return &g_Users[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Add a user, returns pointer to new user or nullptr if full
|
||||
internal user_t *add_user(const char *name)
|
||||
{
|
||||
for (int i = 0; i < MAX_USERS; i++)
|
||||
{
|
||||
if (!g_Users[i].active)
|
||||
{
|
||||
g_Users[i].id = g_NextUserId++;
|
||||
strlcpy(g_Users[i].name, name, sizeof(g_Users[i].name));
|
||||
g_Users[i].active = true;
|
||||
return &g_Users[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Remove a user by ID, returns true if found and removed
|
||||
internal bool remove_user(uint8 id)
|
||||
{
|
||||
for (int i = 0; i < MAX_USERS; i++)
|
||||
{
|
||||
if (g_Users[i].active && g_Users[i].id == id)
|
||||
{
|
||||
g_Users[i].active = false;
|
||||
g_Users[i].id = 0;
|
||||
g_Users[i].name[0] = '\0';
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Populate dummy users on boot for development iteration
|
||||
internal void seed_users()
|
||||
{
|
||||
add_user("Alice");
|
||||
add_user("Bob");
|
||||
add_user("Charlie");
|
||||
}
|
||||
11
Provider/main/api/users/store.hpp
Normal file
11
Provider/main/api/users/store.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.hpp"
|
||||
#include "user.hpp"
|
||||
|
||||
|
||||
// Data store operations for users
|
||||
internal user_t *find_user(uint8 id);
|
||||
internal user_t *add_user(const char *name);
|
||||
internal bool remove_user(uint8 id);
|
||||
internal void seed_users();
|
||||
6
Provider/main/api/users/unity.cpp
Normal file
6
Provider/main/api/users/unity.cpp
Normal file
@@ -0,0 +1,6 @@
|
||||
// clang-format off
|
||||
#include "api/users/store.cpp"
|
||||
#include "api/users/list.cpp"
|
||||
#include "api/users/add.cpp"
|
||||
#include "api/users/remove.cpp"
|
||||
// clang-format on
|
||||
Reference in New Issue
Block a user