From fe223728fb89a37f7ce43fd34fc61fb594f8bf74 Mon Sep 17 00:00:00 2001 From: pedrofnseca Date: Wed, 30 Aug 2023 07:19:43 -0300 Subject: [PATCH] refactor: refactor && add routes user --- README.md | 6 ++-- bin/main | Bin 17640 -> 21768 bytes src/handler.h | 2 +- src/user_handler.h | 84 ++++++++++++++++++++++++++------------------- src/utils.h | 18 ++++++++-- 5 files changed, 69 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 54efd62..4010653 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,8 @@ This RESTful web server provides the following endpoints (please note that this - **GET /users:** Retrieve a list of users. - **GET /users/{id}:** Retrieve detailed information about a specific user. -- **POST /users:** Create a new user (not yet implemented). -- **PUT /users/{id}:** Update information for a specific user (not yet implemented). -- **DELETE /users/{id}:** Delete a user (not yet implemented). +- **POST /users:** Create a new user. +- **PUT /users/{id}:** Update information for a specific user. +- **DELETE /users/{id}:** Delete a user. These endpoints will serve as a starting point for interacting with the API once the development is complete. You can use tools like `curl` or web browsers to perform requests and test the server's functionality. Each endpoint adheres to the principles of a RESTful API, providing clear and predictable behavior. diff --git a/bin/main b/bin/main index 8fcfcb401fa116e1a4b833b8caffeb470591cee0..b5c78087e49c34e03aa18619f28448e05409b468 100755 GIT binary patch delta 5960 zcmai2eN`2%CTcf~W*x4Jd0KDFP;vm{c1US%r!$S(e1))NB+n zZjclmJt6He$3K#mr1n$~rpL62#g7D%#)v&hN_rB-7~>)&@!M6SulLT(+kI?v+Bs+E zzWcksd*{x~o!PhRaf@5u61y{W@{;W{PPq<82(+nE;JOItB(&sG^MA>2f*Gz#tzQV3 z`iq&pzha`S4r01YgA3$EF)ET%hd6bpg1jYulDcy?V+P8-3>Nm1S9R5@uYVYFOZR;A z@8*5qYX5i7=xblx&7Al8XSOHCdt3Xq1$6|8)qPQ+ujpKSs4yODPc(ZN48UtS9#+ll zDqq!ys~C8azsazo5Zg z(=~>z8UwF4YVFqG1`WSQgU4v_JsRAo!Fx4$EW_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?PQYEo$_o2;e zvZv)spg$8GTazbyy_x8C$m`EUscZA5(SLvjvrr9qb}5d_M#n;)E*srmyG)%eaW?d` k$eAd=!YTvxXmds0v=ey}y#bx9$cv7m1tjw5)T-qF0Sk057XSbN diff --git a/src/handler.h b/src/handler.h index c13e3b1..dbcfc11 100644 --- a/src/handler.h +++ b/src/handler.h @@ -31,7 +31,7 @@ enum MHD_Result default_handler(void *cls, struct MHD_Connection *connection, co }; } - else if (is_valid_route(url_str, "/users")) { + else if (validate_route(url_str, "/users")) { response_api = user_router(url_str, method_str, upload_data); } diff --git a/src/user_handler.h b/src/user_handler.h index 2abdd49..223f86f 100644 --- a/src/user_handler.h +++ b/src/user_handler.h @@ -7,57 +7,71 @@ HTTP_response get_all(const char *url) { char *result = executeQueryToJson(query); - if (result == NULL) { + return validate_result(result); +} + +HTTP_response get_one(const char *user_id) { + if (user_id == NULL) { return (HTTP_response){ - .body = simple_message("Internal server error"), - .status = INTERNAL_SERVER_ERROR + .body = simple_message("No user id provided"), + .status = BAD_REQUEST }; } - return (HTTP_response){ - .body = result, - .status = OK - }; -} - -HTTP_response get_one(const char *user_id) { char query[64]; snprintf(query, sizeof(query), "SELECT * FROM users WHERE id = %s", user_id); char *result = executeQueryToJson(query); - if (result == NULL) { + return validate_result(result); +} + +HTTP_response create(const char *body) { + if (body == NULL) { return (HTTP_response){ - .body = simple_message("Internal server error"), - .status = INTERNAL_SERVER_ERROR + .body = simple_message("No body provided"), + .status = BAD_REQUEST }; } - return (HTTP_response){ - .body = result, - .status = OK - }; -} + char query[256]; + snprintf(query, sizeof(query), "INSERT INTO users (name, email) VALUES ('%s', '%s')", "name", "email"); -HTTP_response create(const char *body) { - return (HTTP_response){ - .body = simple_message("create"), - .status = OK - }; + char *result = executeQueryToJson(query); + + return validate_result(result); } HTTP_response update(const char *user_id, const char *body) { - return (HTTP_response){ - .body = simple_message("update"), - .status = OK - }; + if (user_id == NULL) { + return (HTTP_response){ + .body = simple_message("No user id provided"), + .status = BAD_REQUEST + }; + } + + if (body == NULL) { + return (HTTP_response){ + .body = simple_message("No body provided"), + .status = BAD_REQUEST + }; + } + + char query[256]; + snprintf(query, sizeof(query), "UPDATE users SET name = '%s', email = '%s' WHERE id = %s", "name", "email", user_id); + + char *result = executeQueryToJson(query); + + return validate_result(result); } HTTP_response drop(const char *user_id) { - return (HTTP_response){ - .body = simple_message("drop"), - .status = OK - }; + char query[64]; + snprintf(query, sizeof(query), "DELETE FROM users WHERE id = %s", user_id); + + char *result = executeQueryToJson(query); + + return validate_result(result); } HTTP_response user_router(const char *url, const char *method, const char *body){ @@ -66,7 +80,7 @@ HTTP_response user_router(const char *url, const char *method, const char *body) user_id += strlen("/users/"); } - if (is_valid_method(method, "GET")) { + if (validate_method(method, "GET")) { if(user_id == NULL){ return get_all(url); } else { @@ -74,15 +88,15 @@ HTTP_response user_router(const char *url, const char *method, const char *body) } } - if (is_valid_method(method, "POST")) { + if (validate_method(method, "POST")) { return create(body); } - if (is_valid_method(method, "PUT")) { + if (validate_method(method, "PUT")) { return update(user_id, body); } - if (is_valid_method(method, "DELETE")) { + if (validate_method(method, "DELETE")) { return drop(user_id); } diff --git a/src/utils.h b/src/utils.h index f92a703..245bc05 100644 --- a/src/utils.h +++ b/src/utils.h @@ -35,10 +35,24 @@ char *simple_message(const char *message_str) { return formatted_message; } -bool is_valid_route(const char *url, char *route) { +bool validate_route(const char *url, char *route) { return strstr(url, route) != NULL; } -bool is_valid_method(const char *method, char *valid_method) { +bool validate_method(const char *method, char *valid_method) { return strcmp(method, valid_method) == 0; +} + +HTTP_response validate_result(char *result) { + if (result == NULL) { + return (HTTP_response){ + .body = simple_message("Internal server error"), + .status = INTERNAL_SERVER_ERROR + }; + } + + return (HTTP_response){ + .body = result, + .status = OK + }; } \ No newline at end of file