[MOD] Use switch instead of if/else if

This commit is contained in:
lda 2023-11-01 18:04:23 +01:00
parent 688c1d3b9e
commit 7ec10703cd
Signed by: lda
GPG key ID: 6898757653ABE3E6

View file

@ -43,7 +43,14 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
HttpRequestMethod method = HttpRequestMethodGet(args->context); HttpRequestMethod method = HttpRequestMethodGet(args->context);
if ((method != HTTP_GET)) Array *tokensarray;
Array *tokens;
RegTokenInfo *info;
size_t i;
if (method != HTTP_GET)
{ {
char *msg = "Route only supports GET for now."; char *msg = "Route only supports GET for now.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
@ -73,23 +80,23 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
goto finish; goto finish;
} }
if (method == HTTP_GET && ArraySize(path) == 0) switch (method)
{ {
Array *tokensarray = ArrayCreate(); case HTTP_GET:
if (ArraySize(path) == 0)
{
tokensarray = ArrayCreate();
/* Get all registration tokens */ /* Get all registration tokens */
Array *tokens = DbList(db, 2, "tokens", "registration"); tokens = DbList(db, 2, "tokens", "registration");
size_t i;
response = HashMapCreate(); response = HashMapCreate();
for (i = 0; i < ArraySize(tokens); i++) for (i = 0; i < ArraySize(tokens); i++)
{ {
/* TODO: Move this inside it's own `RegTokenJSON`
* function */
char *tokenname = ArrayGet(tokens, i); char *tokenname = ArrayGet(tokens, i);
RegTokenInfo *info = RegTokenGetInfo(db, tokenname); info = RegTokenGetInfo(db, tokenname);
HashMap *jsoninfo = RegTokenJSON(info); HashMap *jsoninfo = RegTokenJSON(info);
@ -101,15 +108,15 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
JsonSet(response, JsonValueArray(tokensarray), 1, "tokens"); JsonSet(response, JsonValueArray(tokensarray), 1, "tokens");
DbListFree(tokens); DbListFree(tokens);
break;
} }
else if (method == HTTP_GET && ArraySize(path) == 1) info = RegTokenGetInfo(db, ArrayGet(path, 0));
{
RegTokenInfo *info = RegTokenGetInfo(db, ArrayGet(path, 0));
response = RegTokenJSON(info); response = RegTokenJSON(info);
RegTokenClose(info); RegTokenClose(info);
RegTokenFree(info); RegTokenFree(info);
break;
} }
finish: finish:
UserUnlock(user); UserUnlock(user);