-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e70328e
commit 2a4bf9b
Showing
14 changed files
with
283 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <libpq-fe.h> | ||
|
||
#define database "*****" | ||
#define user "*****" | ||
#define password "*****" | ||
#define host "*****" | ||
#define port "*****" | ||
|
||
char *executeQueryToJson(const char *query); | ||
|
||
char *formatResultAsJson(PGresult *result); | ||
|
||
char *executeQueryToJson(const char *query) { | ||
PGconn *conn = PQconnectdb( | ||
"user=" user | ||
" password=" password | ||
" dbname=" database | ||
" host=" host | ||
" port=" port | ||
); | ||
|
||
if (PQstatus(conn) != CONNECTION_OK) { | ||
fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); | ||
PQfinish(conn); | ||
return NULL; | ||
} | ||
|
||
PGresult *result = PQexec(conn, query); | ||
|
||
if (PQresultStatus(result) != PGRES_TUPLES_OK) { | ||
fprintf(stderr, "Query execution failed: %s", PQerrorMessage(conn)); | ||
PQclear(result); | ||
PQfinish(conn); | ||
return NULL; | ||
} | ||
|
||
char *jsonResult = formatResultAsJson(result); | ||
|
||
PQclear(result); | ||
PQfinish(conn); | ||
|
||
return jsonResult; | ||
} | ||
|
||
char *formatResultAsJson(PGresult *result) { | ||
int numFields = PQnfields(result); | ||
int numRows = PQntuples(result); | ||
|
||
// Calcula o tamanho total necessário para a string JSON -> para não estourar o buffer | ||
int totalSize = numRows * (2 + numFields * 256) + numRows - 1 + 3; | ||
// Cada campo contribui com pelo menos 256 caracteres, 2 para aspas e 1 para vírgula | ||
// numRows - 1 é para as vírgulas entre objetos JSON | ||
// 3 é para as chaves finais e terminador de string | ||
|
||
char *json = (char *)malloc(totalSize); | ||
json[0] = '\0'; | ||
|
||
if (numRows == 0) { | ||
strcat(json, "[]"); | ||
return json; | ||
} | ||
|
||
for (int i = 0; i < numRows; ++i) { | ||
strcat(json, "{"); | ||
for (int j = 0; j < numFields; ++j) { | ||
if (j > 0) strcat(json, ","); | ||
char *fieldName = PQfname(result, j); | ||
char *fieldValue = PQgetvalue(result, i, j); | ||
|
||
strcat(json, "\""); | ||
strncat(json, fieldName, totalSize - strlen(json) - 5); | ||
strcat(json, "\":\""); | ||
strncat(json, fieldValue, totalSize - strlen(json) - 5); | ||
strcat(json, "\""); | ||
} | ||
strcat(json, "}"); | ||
if (i < numRows - 1) strcat(json, ","); | ||
} | ||
|
||
return json; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "pg.h" | ||
#include <string.h> | ||
#include <stdio.h> | ||
|
||
HTTP_response get_all(const char *url) { | ||
const char *query = "SELECT * FROM users"; | ||
|
||
char *result = executeQueryToJson(query); | ||
|
||
if (result == NULL) { | ||
return (HTTP_response){ | ||
.body = simple_message("Internal server error"), | ||
.status = INTERNAL_SERVER_ERROR | ||
}; | ||
} | ||
|
||
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 (HTTP_response){ | ||
.body = simple_message("Internal server error"), | ||
.status = INTERNAL_SERVER_ERROR | ||
}; | ||
} | ||
|
||
return (HTTP_response){ | ||
.body = result, | ||
.status = OK | ||
}; | ||
} | ||
|
||
HTTP_response create(const char *body) { | ||
return (HTTP_response){ | ||
.body = simple_message("create"), | ||
.status = OK | ||
}; | ||
} | ||
|
||
HTTP_response update(const char *user_id, const char *body) { | ||
return (HTTP_response){ | ||
.body = simple_message("update"), | ||
.status = OK | ||
}; | ||
} | ||
|
||
HTTP_response drop(const char *user_id) { | ||
return (HTTP_response){ | ||
.body = simple_message("drop"), | ||
.status = OK | ||
}; | ||
} | ||
|
||
HTTP_response user_router(const char *url, const char *method, const char *body){ | ||
char *user_id = strstr(url, "/users/"); | ||
if (user_id != NULL) { | ||
user_id += strlen("/users/"); | ||
} | ||
|
||
if (is_valid_method(method, "GET")) { | ||
if(user_id == NULL){ | ||
return get_all(url); | ||
} else { | ||
return get_one(user_id); | ||
} | ||
} | ||
|
||
if (is_valid_method(method, "POST")) { | ||
return create(body); | ||
} | ||
|
||
if (is_valid_method(method, "PUT")) { | ||
return update(user_id, body); | ||
} | ||
|
||
if (is_valid_method(method, "DELETE")) { | ||
return drop(user_id); | ||
} | ||
|
||
return (HTTP_response){ | ||
.body = simple_message("Not implemented"), | ||
.status = NOT_IMPLEMENTED | ||
}; | ||
} |
Oops, something went wrong.