Separates the isbn-lookup interactions into its own file
This is prepping for adding more features, as i want the actual functions that are interaction with the `isbn` tool to be separate
This commit is contained in:
parent
9d764538d3
commit
e51dc21e67
2
Makefile
2
Makefile
@ -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:
|
||||||
|
49
api/isbn-interaction.cpp
Normal file
49
api/isbn-interaction.cpp
Normal 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
23
api/isbn-interaction.h
Normal 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
|
47
api/main.cpp
47
api/main.cpp
@ -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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user