Merge branch 'master' of github.com:SuperNovaa41/library-manager

This commit is contained in:
ShyProton 2024-01-22 15:15:15 -05:00
commit 7ddc34b791
5 changed files with 76 additions and 47 deletions

View File

@ -1,5 +1,5 @@
all: api/*.cpp 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 mkdir -p bin
mv manager bin/ mv manager bin/
clean: clean:

View File

@ -33,7 +33,7 @@ TODO:
### Compiling ### 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 backend simple run `make` in the source directory.
To compile the front end.... blah blah (maybe and HOPEFULLY hook it into the makefile) To compile the front end.... blah blah (maybe and HOPEFULLY hook it into the makefile)

49
api/isbn-interaction.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <string>
#include <vector>
#include <fstream>
#include "isbn-interaction.h"
#include "csv.h"
#define BOOK_FILENAME "books.csv"
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 + "}";
}
std::string get_all_books()
{
std::ifstream file;
int file_exists;
std::string line, total_lines;
std::vector<std::string> book_vec;
std::vector<std::string> 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 + "}";
}

23
api/isbn-interaction.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef ISBN_INTERACTION_H
#define ISBN_INTERACTION_H
/**
* std::string book_vec_to_json
* std::vector<std::string> headers - Vector of strings that contain the type information of the data
* std::vector<std::string> 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<std::string> headers, std::vector<std::string> book);
/**
* std::string get_all_books
*
* This will return all the currently saved books.
*/
std::string get_all_books();
#endif

View File

@ -4,54 +4,11 @@
#include <fstream> #include <fstream>
#include "csv.h" #include "csv.h"
#include "isbn-interaction.h"
#define BOOK_FILENAME "books.csv"
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 + "}";
}
std::string get_all_books()
{
std::ifstream file;
int file_exists;
std::string line, total_lines;
std::vector<std::string> book_vec;
std::vector<std::string> 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 + "}";
}
std::string print_hello() std::string print_hello()
{ {
return "Hello, world!"; return "server is running!";
} }
int main() int main()