diff --git a/src/db.c b/src/db.c index d3e48fe..2b9f17f 100644 --- a/src/db.c +++ b/src/db.c @@ -47,11 +47,13 @@ void setup_db(sqlite3* db) { int rc; char* err_msg = 0; - char* sql; - sql = "CREATE TABLE IF NOT EXISTS books (isbn TEXT, title TEXT, authors TEXT, imageurl TEXT, year_of_publication YEAR, page_length UNSIGNED INT);"; - - rc = sqlite3_exec(db, sql, 0, 0, &err_msg); + rc = sqlite3_exec(db, + "CREATE TABLE IF NOT EXISTS books (id UNSIGNED INT PRIMARY KEY, \ + isbn TEXT, title TEXT, authors TEXT, imageurl TEXT, year_of_publication YEAR, \ + page_length UNSIGNED INT, subjects TEXT, date_added TEXT, date_completed TEXT, \ + progress UNSIGNED TINYINT, publication_date TEXT, subtitle TEXT);", + 0, 0, &err_msg); if (rc != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", err_msg); @@ -67,10 +69,24 @@ void setup_db(sqlite3* db) void add_to_db(book_t* book, sqlite3* db) { char* sql; - int asp_err, rc; + int asp_err, rc, entries = 0; char* err_msg = 0; - asp_err = asprintf(&sql, "INSERT INTO books (isbn, title, authors, imageurl, year_of_publication, page_length) VALUES(\"%s\", \"%s\", \"%s\", \"%s\", %d, %d);", book->isbn, book->title, book->authors, book->image_url, book->year_of_publication, book->page_len); + sqlite3_stmt* getmsg; + sqlite3_prepare(db, "SELECT * FROM BOOKS;", -1, &getmsg, NULL); + while (sqlite3_step(getmsg) == SQLITE_ROW) + entries++; + sqlite3_finalize(getmsg); + + entries++; // new id!! + + asp_err = asprintf(&sql, "INSERT INTO books \ + (id, isbn, title, authors, imageurl, year_of_publication, \ + page_length, subjects, date_added, publication_date, subtitle) \ + VALUES(%d, \"%s\", \"%s\", \"%s\", \"%s\", %d, %d, \"%s\", \"%s\", \"%s\", \"%s\");", + entries, book->isbn, book->title, book->authors, book->image_url, book->year_of_publication, + book->page_len, book->subjects, book->date_added, book->publication_date, book->subtitle); + if (-1 == asp_err) { fprintf(stderr, "asprintf failed!\n"); exit(EXIT_FAILURE); diff --git a/src/json.c b/src/json.c index d70f2c3..03c8500 100644 --- a/src/json.c +++ b/src/json.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "curl.h" #include "json.h" @@ -17,26 +18,26 @@ void check_valid_query(cJSON* numfound) exit(EXIT_FAILURE); } -void get_authors(cJSON* bookinfo, char* authors) +void get_list(cJSON* bookinfo, char* in_str, char* json_value) { - char* temp_author; + char* temp_str; size_t new_len; - cJSON* author_arr = cJSON_GetObjectItemCaseSensitive(bookinfo, "author_name")->child; + cJSON* str_arr = cJSON_GetObjectItemCaseSensitive(bookinfo, json_value)->child; - snprintf(authors, strlen(author_arr->valuestring) + 1, "%s", author_arr->valuestring); - author_arr = author_arr->next; + snprintf(in_str, strlen(str_arr->valuestring) + 1, "%s", str_arr->valuestring); + str_arr = str_arr->next; - while (NULL != author_arr) { + while (NULL != str_arr) { // The plus 1 is for the \0, the plus 2 is for the ", " - new_len = strlen(authors) + strlen(author_arr->valuestring) + 1 + 2; + new_len = strlen(in_str) + strlen(str_arr->valuestring) + 1 + 2; - temp_author = malloc(sizeof(char) * new_len); - snprintf(temp_author, new_len, "%s, %s", authors, author_arr->valuestring); - memcpy(authors, temp_author, new_len); + temp_str = malloc(sizeof(char) * new_len); + snprintf(temp_str, new_len, "%s, %s", in_str, str_arr->valuestring); + memcpy(in_str, temp_str, new_len); - free(temp_author); + free(temp_str); - author_arr = author_arr->next; + str_arr = str_arr->next; } } @@ -45,21 +46,57 @@ void get_image_link(cJSON* bookinfo, book_t* book) { int image_id = cJSON_GetObjectItemCaseSensitive(bookinfo, "cover_i")->valueint; - const char* begin = "https://covers.openlibrary.org/b/id/"; const char* end = "-L.jpg"; book->image_url = malloc(sizeof(char) * MAX_BUF_LEN); sprintf(book->image_url, "%s%d%s", begin, image_id, end); +} +void set_list_values(cJSON* bookinfo, book_t* book) +{ + char authors[MAX_BUF_LEN]; + char subjects[MAX_BUF_LEN]; + get_list(bookinfo, authors, "author_name"); + get_list(bookinfo, subjects, "subject"); + + // Need to malloc, because we need to copy authors into the book struct + book->authors = (char*) malloc(sizeof(char) * (strlen(authors) + 1)); + memcpy(book->authors, authors, strlen(authors) + 1); + + book->subjects = (char*) malloc(sizeof(char) * (strlen(subjects) + 1)); + memcpy(book->subjects, subjects, strlen(subjects) + 1); +} + +void set_actual_values(cJSON* bookinfo, book_t* book, char* isbn) +{ + book->isbn = isbn; + book->title = cJSON_GetObjectItemCaseSensitive(bookinfo, "title")->valuestring; + book->year_of_publication = cJSON_GetObjectItemCaseSensitive(bookinfo, "first_publish_year")->valueint; + book->page_len = cJSON_GetObjectItemCaseSensitive(bookinfo, "number_of_pages_median")->valueint; + book->subtitle = cJSON_GetObjectItemCaseSensitive(bookinfo, "subtitle")->valuestring; + book->publication_date = cJSON_GetObjectItemCaseSensitive(bookinfo, "publish_date")->child->valuestring; + + get_image_link(bookinfo, book); +} + +void set_current_date(book_t* book) +{ + char date[MAX_BUF_LEN]; + + time_t t; + time(&t); + + sprintf(date, "%s", ctime(&t)); + + book->date_added = (char*) malloc(sizeof(char) * (strlen(date) + 1)); + memcpy(book->date_added, date, strlen(date) + 1); } void parse_json(string* s, char* isbn, book_t* book) { - char authors[MAX_BUF_LEN]; - cJSON* json = cJSON_Parse(s->buf); if (NULL == json) { const char* error_ptr = cJSON_GetErrorPtr(); @@ -73,14 +110,7 @@ void parse_json(string* s, char* isbn, book_t* book) cJSON* bookinfo = cJSON_GetObjectItemCaseSensitive(json, "docs")->child; - book->isbn = isbn; - book->title = cJSON_GetObjectItemCaseSensitive(bookinfo, "title")->valuestring; - book->year_of_publication = cJSON_GetObjectItemCaseSensitive(bookinfo, "first_publish_year")->valueint; - book->page_len = cJSON_GetObjectItemCaseSensitive(bookinfo, "number_of_pages_median")->valueint; - - get_authors(bookinfo, authors); - get_image_link(bookinfo, book); - // Need to malloc, because we need to copy authors into the book struct - book->authors = (char*) malloc(sizeof(char) * (strlen(authors) + 1)); - memcpy(book->authors, authors, strlen(authors) + 1); + set_current_date(book); + set_actual_values(bookinfo, book, isbn); + set_list_values(bookinfo, book); } diff --git a/src/json.h b/src/json.h index 2d6aa27..4ba3f0e 100644 --- a/src/json.h +++ b/src/json.h @@ -10,6 +10,11 @@ typedef struct book_t { char* image_url; int year_of_publication; int page_len; + char* subjects; + char* date_added; + char* publication_date; + char* subtitle; + } book_t; /**