Claude opus authored everything to make the user and task work. First iteration
This commit is contained in:
@@ -2,7 +2,7 @@ idf_component_register(SRCS "main.cpp"
|
||||
# Needed as we use minimal build
|
||||
PRIV_REQUIRES esp_http_server esp_eth
|
||||
esp_wifi nvs_flash esp_netif vfs
|
||||
json app_update esp_timer
|
||||
json app_update esp_timer esp_psram
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
if(CONFIG_CALENDINK_DEPLOY_WEB_PAGES)
|
||||
|
||||
76
Provider/main/api/tasks/add.cpp
Normal file
76
Provider/main/api/tasks/add.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
// POST /api/tasks — Create a new task
|
||||
// Body: {"user_id":1, "title":"...", "due_date":1741369200}
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
#include "todo.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
internal esp_err_t api_tasks_post_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
|
||||
char buf[256];
|
||||
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 *user_id_item = cJSON_GetObjectItem(body, "user_id");
|
||||
cJSON *title_item = cJSON_GetObjectItem(body, "title");
|
||||
cJSON *due_date_item = cJSON_GetObjectItem(body, "due_date");
|
||||
|
||||
if (!cJSON_IsNumber(user_id_item) || !cJSON_IsString(title_item) ||
|
||||
!cJSON_IsNumber(due_date_item))
|
||||
{
|
||||
cJSON_Delete(body);
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
|
||||
"Missing user_id, title, or due_date");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
task_t *task =
|
||||
add_task((uint8)user_id_item->valueint, title_item->valuestring,
|
||||
(int64)due_date_item->valuedouble);
|
||||
cJSON_Delete(body);
|
||||
|
||||
if (!task)
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
|
||||
"Task limit reached or invalid user");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
cJSON *resp = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(resp, "id", task->id);
|
||||
cJSON_AddNumberToObject(resp, "user_id", task->user_id);
|
||||
cJSON_AddStringToObject(resp, "title", task->title);
|
||||
cJSON_AddNumberToObject(resp, "due_date", (double)task->due_date);
|
||||
cJSON_AddBoolToObject(resp, "completed", task->completed);
|
||||
|
||||
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_tasks_post_uri = {.uri = "/api/tasks",
|
||||
.method = HTTP_POST,
|
||||
.handler =
|
||||
api_tasks_post_handler,
|
||||
.user_ctx = NULL};
|
||||
62
Provider/main/api/tasks/list.cpp
Normal file
62
Provider/main/api/tasks/list.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
// GET /api/tasks?user_id=N — List tasks for a specific user
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
#include "todo.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
internal esp_err_t api_tasks_get_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
|
||||
char query[64] = {};
|
||||
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 'user_id'");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
char user_id_str[8] = {};
|
||||
if (httpd_query_key_value(query, "user_id", user_id_str,
|
||||
sizeof(user_id_str)) != ESP_OK)
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
|
||||
"Missing query param 'user_id'");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
uint8 user_id = (uint8)atoi(user_id_str);
|
||||
|
||||
cJSON *arr = cJSON_CreateArray();
|
||||
|
||||
for (int i = 0; i < MAX_TASKS; i++)
|
||||
{
|
||||
if (g_Tasks[i].active && g_Tasks[i].user_id == user_id)
|
||||
{
|
||||
cJSON *obj = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(obj, "id", g_Tasks[i].id);
|
||||
cJSON_AddNumberToObject(obj, "user_id", g_Tasks[i].user_id);
|
||||
cJSON_AddStringToObject(obj, "title", g_Tasks[i].title);
|
||||
cJSON_AddNumberToObject(obj, "due_date", (double)g_Tasks[i].due_date);
|
||||
cJSON_AddBoolToObject(obj, "completed", g_Tasks[i].completed);
|
||||
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_tasks_get_uri = {.uri = "/api/tasks",
|
||||
.method = HTTP_GET,
|
||||
.handler =
|
||||
api_tasks_get_handler,
|
||||
.user_ctx = NULL};
|
||||
42
Provider/main/api/tasks/remove.cpp
Normal file
42
Provider/main/api/tasks/remove.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
// DELETE /api/tasks?id=N — Delete a task
|
||||
|
||||
#include "esp_http_server.h"
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
internal esp_err_t api_tasks_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;
|
||||
}
|
||||
|
||||
uint16 id = (uint16)atoi(id_str);
|
||||
|
||||
if (!remove_task(id))
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Task not found");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
httpd_resp_sendstr(req, "{\"status\":\"ok\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
internal const httpd_uri_t api_tasks_delete_uri = {.uri = "/api/tasks",
|
||||
.method = HTTP_DELETE,
|
||||
.handler =
|
||||
api_tasks_delete_handler,
|
||||
.user_ctx = NULL};
|
||||
111
Provider/main/api/tasks/store.cpp
Normal file
111
Provider/main/api/tasks/store.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
// Task data store: CRUD helpers, sorting, and seed data
|
||||
|
||||
#include "esp_timer.h"
|
||||
|
||||
#include "api/tasks/store.hpp"
|
||||
#include "api/users/store.hpp"
|
||||
|
||||
// Find a task by ID, returns nullptr if not found
|
||||
internal task_t *find_task(uint16 id)
|
||||
{
|
||||
for (int i = 0; i < MAX_TASKS; i++)
|
||||
{
|
||||
if (g_Tasks[i].active && g_Tasks[i].id == id)
|
||||
{
|
||||
return &g_Tasks[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Add a task, returns pointer to new task or nullptr if full
|
||||
internal task_t *add_task(uint8 user_id, const char *title, int64 due_date)
|
||||
{
|
||||
// Verify user exists
|
||||
if (find_user(user_id) == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (int i = 0; i < MAX_TASKS; i++)
|
||||
{
|
||||
if (!g_Tasks[i].active)
|
||||
{
|
||||
g_Tasks[i].id = g_NextTaskId++;
|
||||
g_Tasks[i].user_id = user_id;
|
||||
strlcpy(g_Tasks[i].title, title, sizeof(g_Tasks[i].title));
|
||||
g_Tasks[i].due_date = due_date;
|
||||
g_Tasks[i].completed = false;
|
||||
g_Tasks[i].active = true;
|
||||
return &g_Tasks[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Remove a task by ID, returns true if found and removed
|
||||
internal bool remove_task(uint16 id)
|
||||
{
|
||||
for (int i = 0; i < MAX_TASKS; i++)
|
||||
{
|
||||
if (g_Tasks[i].active && g_Tasks[i].id == id)
|
||||
{
|
||||
g_Tasks[i].active = false;
|
||||
g_Tasks[i].id = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove all tasks belonging to a user
|
||||
internal void remove_tasks_for_user(uint8 user_id)
|
||||
{
|
||||
for (int i = 0; i < MAX_TASKS; i++)
|
||||
{
|
||||
if (g_Tasks[i].active && g_Tasks[i].user_id == user_id)
|
||||
{
|
||||
g_Tasks[i].active = false;
|
||||
g_Tasks[i].id = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Simple insertion sort for small arrays — sort task pointers by due_date
|
||||
// ascending
|
||||
internal void sort_tasks_by_due_date(task_t **arr, int count)
|
||||
{
|
||||
for (int i = 1; i < count; i++)
|
||||
{
|
||||
task_t *key = arr[i];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && arr[j]->due_date > key->due_date)
|
||||
{
|
||||
arr[j + 1] = arr[j];
|
||||
j--;
|
||||
}
|
||||
arr[j + 1] = key;
|
||||
}
|
||||
}
|
||||
|
||||
// Populate dummy tasks on boot for development iteration.
|
||||
// Uses relative offsets from current time so due dates always make sense.
|
||||
internal void seed_tasks()
|
||||
{
|
||||
int64 now = (int64)(esp_timer_get_time() / 1000000);
|
||||
|
||||
// Alice's tasks (user_id = 1)
|
||||
add_task(1, "Buy groceries", now + 86400); // +1 day
|
||||
add_task(1, "Review PR #42", now + 3600); // +1 hour
|
||||
add_task(1, "Book dentist appointment", now + 172800); // +2 days
|
||||
add_task(1, "Update resume", now + 604800); // +7 days
|
||||
|
||||
// Bob's tasks (user_id = 2)
|
||||
add_task(2, "Fix login bug", now + 7200); // +2 hours
|
||||
add_task(2, "Deploy staging", now + 43200); // +12 hours
|
||||
add_task(2, "Write unit tests", now + 259200); // +3 days
|
||||
|
||||
// Charlie's tasks (user_id = 3)
|
||||
add_task(3, "Water plants", now + 1800); // +30 min
|
||||
add_task(3, "Call plumber", now + 86400); // +1 day
|
||||
}
|
||||
12
Provider/main/api/tasks/store.hpp
Normal file
12
Provider/main/api/tasks/store.hpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "todo.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
// Data store operations for tasks
|
||||
internal task_t *find_task(uint16 id);
|
||||
internal task_t *add_task(uint8 user_id, const char *title, int64 due_date);
|
||||
internal bool remove_task(uint16 id);
|
||||
internal void remove_tasks_for_user(uint8 user_id);
|
||||
internal void sort_tasks_by_due_date(task_t **arr, int count);
|
||||
internal void seed_tasks();
|
||||
8
Provider/main/api/tasks/unity.cpp
Normal file
8
Provider/main/api/tasks/unity.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
// clang-format off
|
||||
#include "api/tasks/store.cpp"
|
||||
#include "api/tasks/add.cpp"
|
||||
#include "api/tasks/list.cpp"
|
||||
#include "api/tasks/remove.cpp"
|
||||
#include "api/tasks/upcoming.cpp"
|
||||
#include "api/tasks/update.cpp"
|
||||
// clang-format on
|
||||
72
Provider/main/api/tasks/upcoming.cpp
Normal file
72
Provider/main/api/tasks/upcoming.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
// GET /api/tasks/upcoming — Top 3 upcoming tasks per user (for Dashboard)
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
#include "todo.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
internal esp_err_t api_tasks_upcoming_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
cJSON *users_arr = cJSON_AddArrayToObject(root, "users");
|
||||
|
||||
for (int u = 0; u < MAX_USERS; u++)
|
||||
{
|
||||
if (!g_Users[u].active)
|
||||
continue;
|
||||
|
||||
// Collect incomplete tasks for this user
|
||||
task_t *user_tasks[MAX_TASKS];
|
||||
int count = 0;
|
||||
|
||||
for (int t = 0; t < MAX_TASKS; t++)
|
||||
{
|
||||
if (g_Tasks[t].active && g_Tasks[t].user_id == g_Users[u].id &&
|
||||
!g_Tasks[t].completed)
|
||||
{
|
||||
user_tasks[count++] = &g_Tasks[t];
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by due_date ascending
|
||||
sort_tasks_by_due_date(user_tasks, count);
|
||||
|
||||
// Build user object with top 3
|
||||
cJSON *user_obj = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(user_obj, "id", g_Users[u].id);
|
||||
cJSON_AddStringToObject(user_obj, "name", g_Users[u].name);
|
||||
|
||||
cJSON *tasks_arr = cJSON_AddArrayToObject(user_obj, "tasks");
|
||||
int limit = count < 3 ? count : 3;
|
||||
for (int i = 0; i < limit; i++)
|
||||
{
|
||||
cJSON *t_obj = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(t_obj, "id", user_tasks[i]->id);
|
||||
cJSON_AddStringToObject(t_obj, "title", user_tasks[i]->title);
|
||||
cJSON_AddNumberToObject(t_obj, "due_date",
|
||||
(double)user_tasks[i]->due_date);
|
||||
cJSON_AddBoolToObject(t_obj, "completed", user_tasks[i]->completed);
|
||||
cJSON_AddItemToArray(tasks_arr, t_obj);
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(users_arr, user_obj);
|
||||
}
|
||||
|
||||
const char *json = cJSON_PrintUnformatted(root);
|
||||
httpd_resp_sendstr(req, json);
|
||||
|
||||
free((void *)json);
|
||||
cJSON_Delete(root);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
internal const httpd_uri_t api_tasks_upcoming_uri = {
|
||||
.uri = "/api/tasks/upcoming",
|
||||
.method = HTTP_GET,
|
||||
.handler = api_tasks_upcoming_handler,
|
||||
.user_ctx = NULL};
|
||||
77
Provider/main/api/tasks/update.cpp
Normal file
77
Provider/main/api/tasks/update.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
// POST /api/tasks/update — Modify a task
|
||||
// Body: {"id":1, "title":"...", "due_date":..., "completed":true}
|
||||
// All fields except "id" are optional
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
#include "todo.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
internal esp_err_t api_tasks_update_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
|
||||
char buf[256];
|
||||
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 *id_item = cJSON_GetObjectItem(body, "id");
|
||||
if (!cJSON_IsNumber(id_item))
|
||||
{
|
||||
cJSON_Delete(body);
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing 'id'");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
task_t *task = find_task((uint16)id_item->valueint);
|
||||
if (!task)
|
||||
{
|
||||
cJSON_Delete(body);
|
||||
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Task not found");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
// Update fields if present
|
||||
cJSON *title_item = cJSON_GetObjectItem(body, "title");
|
||||
if (cJSON_IsString(title_item))
|
||||
{
|
||||
strlcpy(task->title, title_item->valuestring, sizeof(task->title));
|
||||
}
|
||||
|
||||
cJSON *due_date_item = cJSON_GetObjectItem(body, "due_date");
|
||||
if (cJSON_IsNumber(due_date_item))
|
||||
{
|
||||
task->due_date = (int64)due_date_item->valuedouble;
|
||||
}
|
||||
|
||||
cJSON *completed_item = cJSON_GetObjectItem(body, "completed");
|
||||
if (cJSON_IsBool(completed_item))
|
||||
{
|
||||
task->completed = cJSON_IsTrue(completed_item);
|
||||
}
|
||||
|
||||
cJSON_Delete(body);
|
||||
|
||||
httpd_resp_sendstr(req, "{\"status\":\"ok\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
internal const httpd_uri_t api_tasks_update_uri = {.uri = "/api/tasks/update",
|
||||
.method = HTTP_POST,
|
||||
.handler =
|
||||
api_tasks_update_handler,
|
||||
.user_ctx = NULL};
|
||||
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
|
||||
@@ -19,6 +19,8 @@
|
||||
#include "api/ota/status.cpp"
|
||||
#include "api/system/info.cpp"
|
||||
#include "api/system/reboot.cpp"
|
||||
#include "api/tasks/unity.cpp"
|
||||
#include "api/users/unity.cpp"
|
||||
|
||||
internal const char *TAG = "HTTP_SERVER";
|
||||
|
||||
@@ -170,7 +172,8 @@ internal esp_err_t static_file_handler(httpd_req_t *req)
|
||||
internal esp_err_t cors_options_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Methods",
|
||||
"GET, POST, DELETE, OPTIONS");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type");
|
||||
httpd_resp_set_status(req, "204 No Content");
|
||||
httpd_resp_send(req, NULL, 0);
|
||||
@@ -216,7 +219,7 @@ internal httpd_handle_t start_webserver(void)
|
||||
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.uri_match_fn = httpd_uri_match_wildcard;
|
||||
config.max_uri_handlers = 10; // We have info, reboot, options, and static
|
||||
config.max_uri_handlers = 20;
|
||||
|
||||
httpd_handle_t server = NULL;
|
||||
ESP_LOGI(TAG, "Starting HTTP Server on port: '%d'", config.server_port);
|
||||
@@ -238,6 +241,20 @@ internal httpd_handle_t start_webserver(void)
|
||||
httpd_register_uri_handler(server, &api_ota_firmware_uri);
|
||||
httpd_register_uri_handler(server, &api_ota_bundle_uri);
|
||||
|
||||
// Register todo list API routes
|
||||
httpd_register_uri_handler(server, &api_users_get_uri);
|
||||
httpd_register_uri_handler(server, &api_users_post_uri);
|
||||
httpd_register_uri_handler(server, &api_users_delete_uri);
|
||||
httpd_register_uri_handler(server, &api_tasks_upcoming_uri);
|
||||
httpd_register_uri_handler(server, &api_tasks_get_uri);
|
||||
httpd_register_uri_handler(server, &api_tasks_post_uri);
|
||||
httpd_register_uri_handler(server, &api_tasks_update_uri);
|
||||
httpd_register_uri_handler(server, &api_tasks_delete_uri);
|
||||
|
||||
// Populate dummy data for development
|
||||
seed_users();
|
||||
seed_tasks();
|
||||
|
||||
#ifdef CONFIG_CALENDINK_DEPLOY_WEB_PAGES
|
||||
// Register static file handler last as a catch-all wildcard if deployed
|
||||
httpd_uri_t static_get_uri = {.uri = "/*",
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// SDK
|
||||
#include "esp_log.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_psram.h"
|
||||
#include "esp_system.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
@@ -11,6 +12,7 @@
|
||||
#include "nvs_flash.h"
|
||||
#include "soc/gpio_num.h"
|
||||
|
||||
|
||||
// Project headers
|
||||
#include "appstate.hpp"
|
||||
#include "types.hpp"
|
||||
@@ -28,6 +30,8 @@ extern "C" void app_main()
|
||||
{
|
||||
printf("Hello, Calendink OTA! [V1.1]\n");
|
||||
|
||||
printf("PSRAM size: %d bytes\n", esp_psram_get_size());
|
||||
|
||||
httpd_handle_t web_server = NULL;
|
||||
|
||||
esp_err_t err = nvs_flash_init();
|
||||
|
||||
20
Provider/main/todo.hpp
Normal file
20
Provider/main/todo.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "types.hpp"
|
||||
#include "user.hpp"
|
||||
|
||||
struct task_t
|
||||
{
|
||||
uint16 id; // Auto-assigned (1–65535, 0 = empty slot)
|
||||
uint8 user_id; // Owner (matches user_t.id)
|
||||
char title[64]; // Task description
|
||||
int64 due_date; // Unix timestamp (seconds)
|
||||
bool completed; // Done flag
|
||||
bool active; // Slot in use
|
||||
};
|
||||
|
||||
constexpr int MAX_TASKS = 32;
|
||||
internal task_t g_Tasks[MAX_TASKS] = {};
|
||||
internal uint16 g_NextTaskId = 1;
|
||||
16
Provider/main/user.hpp
Normal file
16
Provider/main/user.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
struct user_t
|
||||
{
|
||||
uint8 id; // Auto-assigned (1–255, 0 = empty slot)
|
||||
char name[32]; // Display name
|
||||
bool active; // Slot in use
|
||||
};
|
||||
|
||||
constexpr int MAX_USERS = 8;
|
||||
internal user_t g_Users[MAX_USERS] = {};
|
||||
internal uint8 g_NextUserId = 1;
|
||||
Reference in New Issue
Block a user