Skip to content

Commit

Permalink
refactor: refactor && add routes user
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroFnseca committed Aug 30, 2023
1 parent 2a4bf9b commit fe22372
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 41 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Binary file modified bin/main
Binary file not shown.
2 changes: 1 addition & 1 deletion src/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
84 changes: 49 additions & 35 deletions src/user_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -66,23 +80,23 @@ 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 {
return get_one(user_id);
}
}

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);
}

Expand Down
18 changes: 16 additions & 2 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}

0 comments on commit fe22372

Please sign in to comment.