Separating users from tasks + fixing weird ota bug

This commit is contained in:
2026-03-07 23:41:26 -05:00
parent 3fa879d007
commit ac95358561
14 changed files with 657 additions and 308 deletions

View File

@@ -6,7 +6,7 @@
#include "api/users/store.hpp"
// Find a task by ID, returns nullptr if not found
internal task_t *find_task(uint16 id)
task_t *find_task(uint16 id)
{
for (int i = 0; i < MAX_TASKS; i++)
{
@@ -19,7 +19,7 @@ internal task_t *find_task(uint16 id)
}
// 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)
task_t *add_task(uint8 user_id, const char *title, int64 due_date)
{
// Verify user exists
if (find_user(user_id) == nullptr)
@@ -44,7 +44,7 @@ internal task_t *add_task(uint8 user_id, const char *title, int64 due_date)
}
// Remove a task by ID, returns true if found and removed
internal bool remove_task(uint16 id)
bool remove_task(uint16 id)
{
for (int i = 0; i < MAX_TASKS; i++)
{
@@ -59,7 +59,7 @@ internal bool remove_task(uint16 id)
}
// Remove all tasks belonging to a user
internal void remove_tasks_for_user(uint8 user_id)
void remove_tasks_for_user(uint8 user_id)
{
for (int i = 0; i < MAX_TASKS; i++)
{
@@ -73,7 +73,7 @@ internal void remove_tasks_for_user(uint8 user_id)
// 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)
void sort_tasks_by_due_date(task_t **arr, int count)
{
for (int i = 1; i < count; i++)
{
@@ -90,7 +90,7 @@ internal void sort_tasks_by_due_date(task_t **arr, int count)
// Populate dummy tasks on boot for development iteration.
// Uses relative offsets from current time so due dates always make sense.
internal void seed_tasks()
void seed_tasks()
{
int64 now = (int64)(esp_timer_get_time() / 1000000);

View File

@@ -4,9 +4,9 @@
#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();
task_t *find_task(uint16 id);
task_t *add_task(uint8 user_id, const char *title, int64 due_date);
bool remove_task(uint16 id);
void remove_tasks_for_user(uint8 user_id);
void sort_tasks_by_due_date(task_t **arr, int count);
void seed_tasks();

View File

@@ -3,7 +3,7 @@
#include "api/users/store.hpp"
// Find a user by ID, returns nullptr if not found
internal user_t *find_user(uint8 id)
user_t *find_user(uint8 id)
{
for (int i = 0; i < MAX_USERS; i++)
{
@@ -16,7 +16,7 @@ internal user_t *find_user(uint8 id)
}
// Add a user, returns pointer to new user or nullptr if full
internal user_t *add_user(const char *name)
user_t *add_user(const char *name)
{
for (int i = 0; i < MAX_USERS; i++)
{
@@ -32,7 +32,7 @@ internal user_t *add_user(const char *name)
}
// Remove a user by ID, returns true if found and removed
internal bool remove_user(uint8 id)
bool remove_user(uint8 id)
{
for (int i = 0; i < MAX_USERS; i++)
{
@@ -47,8 +47,19 @@ internal bool remove_user(uint8 id)
return false;
}
// Update a user's name, returns pointer to user or nullptr if not found
user_t *update_user(uint8 id, const char *name)
{
user_t *user = find_user(id);
if (user)
{
strlcpy(user->name, name, sizeof(user->name));
}
return user;
}
// Populate dummy users on boot for development iteration
internal void seed_users()
void seed_users()
{
add_user("Alice");
add_user("Bob");

View File

@@ -3,9 +3,9 @@
#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();
user_t *find_user(uint8 id);
user_t *add_user(const char *name);
bool remove_user(uint8 id);
user_t *update_user(uint8 id, const char *name);
void seed_users();

View File

@@ -3,4 +3,5 @@
#include "api/users/list.cpp"
#include "api/users/add.cpp"
#include "api/users/remove.cpp"
#include "api/users/update.cpp"
// clang-format on

View File

@@ -0,0 +1,68 @@
// POST /api/users/update — Update an existing user's name
// Body: {"id": 1, "name": "Bob"}
#include "cJSON.h"
#include "esp_http_server.h"
#include "api/users/store.hpp"
#include "types.hpp"
#include "user.hpp"
internal esp_err_t api_users_update_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 *id_item = cJSON_GetObjectItem(body, "id");
cJSON *name_item = cJSON_GetObjectItem(body, "name");
if (!cJSON_IsNumber(id_item) || !cJSON_IsString(name_item) ||
strlen(name_item->valuestring) == 0)
{
cJSON_Delete(body);
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing 'id' or 'name'");
return ESP_FAIL;
}
user_t *user = update_user((uint8)id_item->valueint, name_item->valuestring);
cJSON_Delete(body);
if (!user)
{
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "User not found");
return ESP_FAIL;
}
cJSON *resp = cJSON_CreateObject();
cJSON_AddStringToObject(resp, "status", "ok");
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_update_uri = {.uri = "/api/users/update",
.method = HTTP_POST,
.handler =
api_users_update_handler,
.user_ctx = NULL};