Adds an endpoint to add books

- Changed the release version for ISBN-lookup because it was out of date
  and broke a feature
- Added an endpoint for adding books with robust error handling
This commit is contained in:
SuperNovaa41 2024-01-24 10:54:38 -05:00
parent ba8d911ddf
commit 00ac999983
4 changed files with 40 additions and 11 deletions

View File

@ -1,9 +1,10 @@
#include <cinttypes>
#include <string> #include <string>
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include "isbn-interaction.h" #include "isbn-interaction.h"
#include "csv.h" #include "csv.h"
@ -14,6 +15,7 @@ std::string book_vec_to_json(std::vector<std::string> headers, std::vector<std::
{ {
int i; int i;
std::string out = "{"; std::string out = "{";
for (i = 0; i < book.size(); i++) { for (i = 0; i < book.size(); i++) {
out += "\"" + headers[i] + "\":"; out += "\"" + headers[i] + "\":";
out += "\"" + book[i] + "\","; out += "\"" + book[i] + "\",";
@ -30,6 +32,7 @@ std::string get_all_books()
std::vector<std::string> book_vec; std::vector<std::string> book_vec;
std::vector<std::string> header_vec; std::vector<std::string> header_vec;
file.open(BOOK_FILENAME); file.open(BOOK_FILENAME);
if (!file.good()) { if (!file.good()) {
@ -45,25 +48,42 @@ std::string get_all_books()
total_lines = "{"; total_lines = "{";
while (std::getline(file, line)) { while (std::getline(file, line)) {
book_vec = get_csv_as_vector(line); book_vec = get_csv_as_vector(line);
total_lines += book_vec_to_json(header_vec, book_vec) + ","; total_lines += book_vec_to_json(header_vec, book_vec) + ",";
} }
return total_lines + "}"; return total_lines + "}";
} }
void add_new_book(std::string isbn) std::string add_new_book(std::string isbn)
{ {
pid_t pid; pid_t pid;
int exec_status;
std::string program_name = "./isbn"; std::string program_name = "./isbn";
std::string write = "w"; std::string write = "w";
const char* args[] = {program_name.c_str(), isbn.c_str(), write.c_str()}; char* args[] = {(char*) program_name.c_str(), (char*) isbn.c_str(), (char*) write.c_str(), NULL};
pid = fork(); pid = fork();
if (0 == pid) { // if (0 == pid) {
// this is the child execvp(args[0], args);
} exit(EXIT_SUCCESS);
} else if (pid < 0) {
perror("Adding book, failed to fork");
return "Book lookup failed!";
} else {
wait(&exec_status);
//TODO execvp the args blah blah if (!WIFEXITED(exec_status)) {
perror("ISBN exited unexpectedly!");
return "Book lookup failed!";
}
if (0 != WEXITSTATUS(exec_status)) {
return "Invalid ISBN submitted!";
}
return "Book added successfully!";
}
} }

View File

@ -17,6 +17,6 @@ std::string book_vec_to_json(std::vector<std::string> headers, std::vector<std::
*/ */
std::string get_all_books(); std::string get_all_books();
void add_new_book(std::string isbn); std::string add_new_book(std::string isbn);
#endif #endif

View File

@ -1,8 +1,8 @@
#include <crow.h> #include <crow.h>
#include <crow/app.h> #include <crow/app.h>
#include <fstream> #include <fstream>
#include "csv.h" #include "csv.h"
#include "isbn-interaction.h" #include "isbn-interaction.h"
@ -23,6 +23,15 @@ int main()
return crow::response(get_all_books()); return crow::response(get_all_books());
}); });
CROW_ROUTE(app, "/add/<string>")
([](std::string isbn)
{
return crow::response(add_new_book(isbn));
});
app.port(18080).run(); app.port(18080).run();
} }

View File

@ -3,7 +3,7 @@ make
cd bin/ cd bin/
wget https://github.com/SuperNovaa41/isbn-lookup/releases/download/release/isbn wget https://github.com/SuperNovaa41/isbn-lookup/releases/download/0.0.2/isbn
chmod +x isbn chmod +x isbn
cd .. cd ..