From 5103a2eacd47cd8cf2324cd2fc1c48311a4112a4 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Wed, 12 Jun 2024 12:44:11 +0200 Subject: rest: allow the same center name in center update Exclude centerid of the center we update from the search of centers with the same name. Report when other center have the same name as the one in the payload of the update request. --- src/rest.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/rest.c b/src/rest.c index 7ffebbf..3d881a8 100644 --- a/src/rest.c +++ b/src/rest.c @@ -1812,11 +1812,11 @@ static int og_cmd_post_center_update(json_t *element, struct og_msg_params *params) { const char *key, *msglog; + uint32_t center_id; struct og_dbi *dbi; dbi_result result; json_t *value; int err = 0; - unsigned int center_id; json_object_foreach(element, key, value) { if (!strcmp(key, "name")) { @@ -1865,8 +1865,9 @@ static int og_cmd_post_center_update(json_t *element, dbi_result_free(result); result = dbi_conn_queryf(dbi->conn, - "SELECT nombrecentro FROM centros WHERE nombrecentro='%s'", - params->name); + "SELECT idcentro FROM centros " + "WHERE nombrecentro='%s' and idcentro<>'%u'", + params->name, center_id); if (!result) { dbi_conn_error(dbi->conn, &msglog); syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", @@ -1874,9 +1875,10 @@ static int og_cmd_post_center_update(json_t *element, og_dbi_close(dbi); return -1; } - if (dbi_result_get_numrows(result) != 0) { - syslog(LOG_ERR, "a center with that name already exists: %s\n", - params->name); + if (dbi_result_next_row(result)) { + center_id = dbi_result_get_uint(result, "idcentro"); + syslog(LOG_ERR, "the center with id %u already has the name: %s\n", + center_id, params->name); dbi_result_free(result); og_dbi_close(dbi); return -1; -- cgit v1.2.3-18-g5258