diff --git a/Makefile b/Makefile index daca104..968d1b8 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ all: api/*.cpp - g++ api/main.cpp api/csv.cpp -o manager + g++ api/main.cpp api/csv.cpp api/isbn-interaction.cpp -o manager mkdir -p bin mv manager bin/ clean: diff --git a/README.md b/README.md index eb7d1da..4476116 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ TODO: ### Compiling -**Dependencies**: [ISBN-lookup](https://github.com/SuperNovaa41/isbn-lookup), [CrowCPP](https://crowcpp.org), .. im sure theres more +**Dependencies**: [ISBN-lookup](https://github.com/SuperNovaa41/isbn-lookup), [CrowCPP](https://crowcpp.org), wget, .. im sure theres more To compile the backend simple run `make` in the source directory. To compile the front end.... blah blah (maybe and HOPEFULLY hook it into the makefile) diff --git a/api/isbn-interaction.cpp b/api/isbn-interaction.cpp new file mode 100644 index 0000000..2b352a0 --- /dev/null +++ b/api/isbn-interaction.cpp @@ -0,0 +1,49 @@ +#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; + std::string out = "{"; + for (i = 0; i < book.size(); i++) { + out += "\"" + headers[i] + "\":"; + out += "\"" + book[i] + "\","; + } + return out + "}"; + +} + +std::string get_all_books() +{ + std::ifstream file; + int file_exists; + std::string line, total_lines; + std::vector book_vec; + std::vector header_vec; + + file.open(BOOK_FILENAME); + + if (!file.good()) { + fprintf(stderr, "Theres no books available!\n"); + return "No books saved!"; + } + + 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 = "{"; + while (std::getline(file, line)) { + book_vec = get_csv_as_vector(line); + total_lines += book_vec_to_json(header_vec, book_vec) + ","; + } + + return total_lines + "}"; +} diff --git a/api/isbn-interaction.h b/api/isbn-interaction.h new file mode 100644 index 0000000..4128782 --- /dev/null +++ b/api/isbn-interaction.h @@ -0,0 +1,23 @@ +#ifndef ISBN_INTERACTION_H +#define ISBN_INTERACTION_H + +/** + * std::string book_vec_to_json + * std::vector headers - Vector of strings that contain the type information of the data + * std::vector book - Vector of strings that contain the data + * + * Helper function that converts a vector of book data into a json format + */ +std::string book_vec_to_json(std::vector headers, std::vector book); + +/** + * std::string get_all_books + * + * This will return all the currently saved books. + */ +std::string get_all_books(); + + + + +#endif diff --git a/api/main.cpp b/api/main.cpp index e9769b6..f0ac25f 100644 --- a/api/main.cpp +++ b/api/main.cpp @@ -4,54 +4,11 @@ #include #include "csv.h" - -#define BOOK_FILENAME "books.csv" - -std::string book_vec_to_json(std::vector headers, std::vector book) -{ - int i; - std::string out = "{"; - for (i = 0; i < book.size(); i++) { - out += "\"" + headers[i] + "\":"; - out += "\"" + book[i] + "\","; - } - return out + "}"; - -} - -std::string get_all_books() -{ - std::ifstream file; - int file_exists; - std::string line, total_lines; - std::vector book_vec; - std::vector header_vec; - - file.open(BOOK_FILENAME); - - if (!file.good()) { - fprintf(stderr, "Theres no books available!\n"); - return "No books saved!"; - } - - 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 = "{"; - while (std::getline(file, line)) { - book_vec = get_csv_as_vector(line); - total_lines += book_vec_to_json(header_vec, book_vec) + ","; - } - - return total_lines + "}"; -} +#include "isbn-interaction.h" std::string print_hello() { - return "Hello, world!"; + return "server is running!"; } int main()