library-manager/api/main.cpp

72 lines
1.3 KiB
C++
Raw Normal View History

2024-01-18 13:36:24 -05:00
#include <crow.h>
#include <crow/app.h>
#include <fstream>
#include "csv.h"
#define BOOK_FILENAME "books.csv"
2024-01-18 14:42:49 -05:00
std::string book_vec_to_json(std::vector<std::string> headers, std::vector<std::string> book)
{
int i;
std::string out = "{";
for (i = 0; i < book.size(); i++) {
out += "\"" + headers[i] + "\":";
out += "\"" + book[i] + "\",";
}
return out + "}";
}
2024-01-18 13:36:24 -05:00
std::string get_all_books()
{
std::ifstream file;
int file_exists;
std::string line, total_lines;
2024-01-18 14:42:49 -05:00
std::vector<std::string> book_vec;
std::vector<std::string> header_vec;
2024-01-18 13:36:24 -05:00
file.open(BOOK_FILENAME);
if (!file.good()) {
fprintf(stderr, "Theres no books available!\n");
return "No books saved!";
}
2024-01-18 14:42:49 -05:00
std::getline(file, line);
// this contains the headers so that we can fill the json file
header_vec = get_csv_as_vector(line);
2024-01-18 13:36:24 -05:00
2024-01-18 14:42:49 -05:00
total_lines = "{";
2024-01-18 13:36:24 -05:00
while (std::getline(file, line)) {
book_vec = get_csv_as_vector(line);
2024-01-18 14:42:49 -05:00
total_lines += book_vec_to_json(header_vec, book_vec) + ",";
2024-01-18 13:36:24 -05:00
}
2024-01-18 14:42:49 -05:00
return total_lines + "}";
2024-01-18 13:36:24 -05:00
}
std::string print_hello()
{
return "Hello, world!";
}
int main()
{
crow::SimpleApp app;
CROW_ROUTE(app, "/")([](){
return crow::response(print_hello());
});
CROW_ROUTE(app, "/books")([](){
return crow::response(get_all_books());
});
app.port(18080).run();
}