Separating users from tasks + fixing weird ota bug
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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
|
||||
|
||||
68
Provider/main/api/users/update.cpp
Normal file
68
Provider/main/api/users/update.cpp
Normal 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};
|
||||
Reference in New Issue
Block a user