[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,43 +80,43 @@ 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` char *tokenname = ArrayGet(tokens, i);
* function */
char *tokenname = ArrayGet(tokens, i);
RegTokenInfo *info = RegTokenGetInfo(db, tokenname); info = RegTokenGetInfo(db, tokenname);
HashMap *jsoninfo = RegTokenJSON(info); HashMap *jsoninfo = RegTokenJSON(info);
RegTokenClose(info);
RegTokenFree(info);
ArrayAdd(tokensarray, JsonValueObject(jsoninfo));
}
JsonSet(response, JsonValueArray(tokensarray), 1, "tokens");
DbListFree(tokens);
break;
}
info = RegTokenGetInfo(db, ArrayGet(path, 0));
response = RegTokenJSON(info);
RegTokenClose(info); RegTokenClose(info);
RegTokenFree(info); RegTokenFree(info);
ArrayAdd(tokensarray, JsonValueObject(jsoninfo)); break;
}
JsonSet(response, JsonValueArray(tokensarray), 1, "tokens");
DbListFree(tokens);
}
else if (method == HTTP_GET && ArraySize(path) == 1)
{
RegTokenInfo *info = RegTokenGetInfo(db, ArrayGet(path, 0));
response = RegTokenJSON(info);
RegTokenClose(info);
RegTokenFree(info);
} }
finish: finish:
UserUnlock(user); UserUnlock(user);