Updated the task view and backend to handle more like a routine

This commit is contained in:
2026-03-08 22:10:46 -04:00
parent 4161ff9513
commit 9d3a277f45
14 changed files with 405 additions and 147 deletions

View File

@@ -1,5 +1,6 @@
// POST /api/tasks — Create a new task
// Body: {"user_id":1, "title":"...", "due_date":1741369200}
// Body: {"user_id":1, "title":"...", "due_date":1741369200, "period":0,
// "recurrence":0}
#include "cJSON.h"
#include "esp_http_server.h"
@@ -31,19 +32,26 @@ internal esp_err_t api_tasks_post_handler(httpd_req_t *req)
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");
cJSON *period_item = cJSON_GetObjectItem(body, "period");
cJSON *recurrence_item = cJSON_GetObjectItem(body, "recurrence");
if (!cJSON_IsNumber(user_id_item) || !cJSON_IsString(title_item) ||
!cJSON_IsNumber(due_date_item))
if (!cJSON_IsNumber(user_id_item) || !cJSON_IsString(title_item))
{
cJSON_Delete(body);
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
"Missing user_id, title, or due_date");
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing user_id or title");
return ESP_FAIL;
}
int64 due_date =
cJSON_IsNumber(due_date_item) ? (int64)due_date_item->valuedouble : 0;
uint8 period = cJSON_IsNumber(period_item) ? (uint8)period_item->valueint
: (uint8)PERIOD_MORNING;
uint8 recurrence =
cJSON_IsNumber(recurrence_item) ? (uint8)recurrence_item->valueint : 0;
task_t *task =
add_task((uint8)user_id_item->valueint, title_item->valuestring,
(int64)due_date_item->valuedouble);
add_task((uint8)user_id_item->valueint, title_item->valuestring, due_date,
period, recurrence);
cJSON_Delete(body);
if (!task)
@@ -58,6 +66,8 @@ internal esp_err_t api_tasks_post_handler(httpd_req_t *req)
cJSON_AddNumberToObject(resp, "user_id", task->user_id);
cJSON_AddStringToObject(resp, "title", task->title);
cJSON_AddNumberToObject(resp, "due_date", (double)task->due_date);
cJSON_AddNumberToObject(resp, "period", task->period);
cJSON_AddNumberToObject(resp, "recurrence", task->recurrence);
cJSON_AddBoolToObject(resp, "completed", task->completed);
const char *json = cJSON_PrintUnformatted(resp);

View File

@@ -41,6 +41,8 @@ internal esp_err_t api_tasks_get_handler(httpd_req_t *req)
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_AddNumberToObject(obj, "period", g_Tasks[i].period);
cJSON_AddNumberToObject(obj, "recurrence", g_Tasks[i].recurrence);
cJSON_AddBoolToObject(obj, "completed", g_Tasks[i].completed);
cJSON_AddItemToArray(arr, obj);
}

View File

@@ -19,7 +19,8 @@ task_t *find_task(uint16 id)
}
// Add a task, returns pointer to new task or nullptr if full
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, uint8 period,
uint8 recurrence)
{
// Verify user exists
if (find_user(user_id) == nullptr)
@@ -35,6 +36,8 @@ task_t *add_task(uint8 user_id, const char *title, int64 due_date)
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].period = period;
g_Tasks[i].recurrence = recurrence;
g_Tasks[i].completed = false;
g_Tasks[i].active = true;
return &g_Tasks[i];
@@ -94,18 +97,19 @@ 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
// Alice's tasks (user_id = 1) — mix of one-off and recurring
add_task(1, "Buy groceries", now + 86400, PERIOD_MORNING);
add_task(1, "Review PR #42", now + 3600, PERIOD_AFTERNOON);
add_task(1, "Book dentist appointment", now + 172800, PERIOD_MORNING);
add_task(1, "Update resume", now + 604800, PERIOD_EVENING);
// 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
// Bob's tasks (user_id = 2) — some recurring routines
add_task(2, "Morning standup", 0, PERIOD_MORNING, 0x1F); // Mon-Fri
add_task(2, "Deploy staging", now + 43200, PERIOD_AFTERNOON);
add_task(2, "Write unit tests", now + 259200, PERIOD_MORNING);
// Charlie's tasks (user_id = 3)
add_task(3, "Water plants", now + 1800); // +30 min
add_task(3, "Call plumber", now + 86400); // +1 day
// Charlie's tasks (user_id = 3) — kid routine examples
add_task(3, "Breakfast", 0, PERIOD_MORNING, 0x1F); // Mon-Fri
add_task(3, "Homework", 0, PERIOD_AFTERNOON, 0x15); // Mon+Wed+Fri
add_task(3, "Bath time", 0, PERIOD_EVENING, 0x7F); // Every day
}

View File

@@ -5,7 +5,8 @@
// Data store operations for tasks
task_t *find_task(uint16 id);
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,
uint8 period = PERIOD_MORNING, uint8 recurrence = 0);
bool remove_task(uint16 id);
void remove_tasks_for_user(uint8 user_id);
void sort_tasks_by_due_date(task_t **arr, int count);

View File

@@ -1,4 +1,4 @@
// GET /api/tasks/upcoming — Top 3 upcoming tasks per user (for Dashboard)
// GET /api/tasks/upcoming — Today's tasks per user, grouped by period
#include "cJSON.h"
#include "esp_http_server.h"
@@ -20,36 +20,26 @@ internal esp_err_t api_tasks_upcoming_handler(httpd_req_t *req)
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
// Include: recurring tasks (any day) and one-off tasks
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++)
for (int t = 0; t < MAX_TASKS; t++)
{
if (!g_Tasks[t].active || g_Tasks[t].completed ||
g_Tasks[t].user_id != g_Users[u].id)
continue;
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_AddNumberToObject(t_obj, "id", g_Tasks[t].id);
cJSON_AddStringToObject(t_obj, "title", g_Tasks[t].title);
cJSON_AddNumberToObject(t_obj, "due_date", (double)g_Tasks[t].due_date);
cJSON_AddNumberToObject(t_obj, "period", g_Tasks[t].period);
cJSON_AddNumberToObject(t_obj, "recurrence", g_Tasks[t].recurrence);
cJSON_AddBoolToObject(t_obj, "completed", g_Tasks[t].completed);
cJSON_AddItemToArray(tasks_arr, t_obj);
}

View File

@@ -1,6 +1,6 @@
// POST /api/tasks/update — Modify a task
// Body: {"id":1, "title":"...", "due_date":..., "completed":true}
// All fields except "id" are optional
// Body: {"id":1, "title":"...", "due_date":..., "period":0, "recurrence":0,
// "completed":true} All fields except "id" are optional
#include "cJSON.h"
#include "esp_http_server.h"
@@ -58,6 +58,18 @@ internal esp_err_t api_tasks_update_handler(httpd_req_t *req)
task->due_date = (int64)due_date_item->valuedouble;
}
cJSON *period_item = cJSON_GetObjectItem(body, "period");
if (cJSON_IsNumber(period_item))
{
task->period = (uint8)period_item->valueint;
}
cJSON *recurrence_item = cJSON_GetObjectItem(body, "recurrence");
if (cJSON_IsNumber(recurrence_item))
{
task->recurrence = (uint8)recurrence_item->valueint;
}
cJSON *completed_item = cJSON_GetObjectItem(body, "completed");
if (cJSON_IsBool(completed_item))
{