diff --git a/Makefile b/Makefile index 968d1b8..61cf8ae 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ all: api/*.cpp - g++ api/main.cpp api/csv.cpp api/isbn-interaction.cpp -o manager + g++ api/main.cpp api/isbn-interaction.cpp -lsqlite3 -o manager mkdir -p bin mv manager bin/ clean: diff --git a/api/csv.cpp b/api/csv.cpp deleted file mode 100644 index cf225a0..0000000 --- a/api/csv.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "csv.h" -#include - -std::string strip_quotes(std::string input) -{ - if (input[0] != '"') - return input; - return input.substr(1, input.size() - 2); - -} - -int get_next_comma(std::string input) -{ - int i; - bool in_string = false; - for (i = 0; i < input.size(); i++) { - if (input[i] == '"') - in_string = !in_string; - if (input[i] == ',' && !in_string) - return i; - } - return -1; -} - -std::vector get_csv_as_vector(std::string input) -{ - std::vector output; - int next_comma; - std::string current_string; - - while ((next_comma = get_next_comma(input)) != -1) { - output.push_back(strip_quotes(input.substr(0, next_comma))); - - input = input.substr(next_comma + 1, input.size()); - } - output.push_back(input); - - return output; -} diff --git a/api/csv.h b/api/csv.h deleted file mode 100644 index 2344b48..0000000 --- a/api/csv.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef CSV_H -#define CSV_H - -#include -#include - -std::string strip_quotes(std::string input); - -int get_next_comma(std::string input); - -std::vector get_csv_as_vector(std::string input); - - - -#endif diff --git a/api/isbn-interaction.cpp b/api/isbn-interaction.cpp index 8ec6e5e..c196199 100644 --- a/api/isbn-interaction.cpp +++ b/api/isbn-interaction.cpp @@ -1,15 +1,13 @@ #include #include #include +#include #include #include #include #include "isbn-interaction.h" -#include "csv.h" - -#define BOOK_FILENAME "books.csv" std::string book_vec_to_json(std::vector headers, std::vector book) { int i; @@ -23,38 +21,63 @@ std::string book_vec_to_json(std::vector headers, std::vector get_db_headers(sqlite3* db) +{ + int i; + std::vector res; + sqlite3_stmt* getmsgs; + + sqlite3_prepare(db, "SELECT * FROM books;", -1, &getmsgs, NULL); + sqlite3_step(getmsgs); + for (i = 0; i < sqlite3_column_count(getmsgs); i++) + res.push_back(sqlite3_column_name(getmsgs, i)); + + sqlite3_finalize(getmsgs); + return res; } std::string get_all_books() { - std::ifstream file; - int file_exists; - std::string line, total_lines; + int rc, i; + sqlite3* db; + char* text; + std::string total_lines; std::vector book_vec; std::vector header_vec; + std::ifstream file("books.db"); + if (!file.is_open()) + return "No books found!\n"; + file.close(); - file.open(BOOK_FILENAME); + rc = sqlite3_open("books.db", &db); + if (rc != SQLITE_OK) { + fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); - if (!file.good()) { - fprintf(stderr, "Theres no books available!\n"); - return "No books saved!"; + exit(EXIT_FAILURE); } - std::getline(file, line); - - // this contains the headers so that we can fill the json file - header_vec = get_csv_as_vector(line); + total_lines += "{\"books\": ["; - total_lines = "{ \"books\": ["; - while (std::getline(file, line)) { - book_vec = get_csv_as_vector(line); - + header_vec = get_db_headers(db); + + sqlite3_stmt* getmsgs; + sqlite3_prepare(db, "SELECT * FROM books;", -1, &getmsgs, NULL); + while(sqlite3_step(getmsgs) == SQLITE_ROW) { + book_vec.clear(); + for (i = 0; i < sqlite3_column_count(getmsgs); i++) { + text = (char*) sqlite3_column_text(getmsgs, i); + book_vec.push_back(std::string(text)); + } total_lines += book_vec_to_json(header_vec, book_vec) + ","; } + sqlite3_finalize(getmsgs); + sqlite3_close(db); - // remove the trailing comma + // remove trailing comma total_lines.pop_back(); diff --git a/api/main.cpp b/api/main.cpp index 05ecb72..e0a5136 100644 --- a/api/main.cpp +++ b/api/main.cpp @@ -2,8 +2,6 @@ #include #include - -#include "csv.h" #include "isbn-interaction.h" std::string print_hello() @@ -37,5 +35,4 @@ int main() app.port(18080).run(); - } diff --git a/dev/install-dependencies.sh b/dev/install-dependencies.sh index 987264d..44ae258 100755 --- a/dev/install-dependencies.sh +++ b/dev/install-dependencies.sh @@ -5,7 +5,7 @@ cd ../ mkdir -p bin cd bin -wget https://github.com/SuperNovaa41/isbn-lookup/releases/download/0.0.4/isbn +wget https://github.com/SuperNovaa41/isbn-lookup/releases/download/0.0.5/isbn chmod +x isbn cd ../