Browse Source

create tables

master
alistair 3 years ago
parent
commit
85cbc765f1
  1. 2
      CMakeLists.txt
  2. 83
      telegram_bot.cpp

2
CMakeLists.txt

@ -29,6 +29,7 @@ project(echobot-submodule) @@ -29,6 +29,7 @@ project(echobot-submodule)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
set(Boost_USE_MULTITHREADED ON)
find_package(SQLite3 REQUIRED)
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
@ -42,4 +43,5 @@ target_link_libraries(telegram_bog PRIVATE TgBot ${CMAKE_THREAD_LIBS_INIT} @@ -42,4 +43,5 @@ target_link_libraries(telegram_bog PRIVATE TgBot ${CMAKE_THREAD_LIBS_INIT}
${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES}
nlohmann_json::nlohmann_json)
target_link_libraries(telegram_bog PRIVATE cpr::cpr)
target_link_libraries(telegram_bog sqlite3)

83
telegram_bot.cpp

@ -17,6 +17,8 @@ @@ -17,6 +17,8 @@
#include <tgbot/tgbot.h>
#include <cpr/cpr.h>
#include <optional>
#include <sqlite3.h>
#include <cstdlib>
using namespace TgBot;
using json = nlohmann::json;
@ -170,21 +172,88 @@ class spotify { @@ -170,21 +172,88 @@ class spotify {
};
class db {
std::string filepath;
json data;
class songdb {
/* need one table for every chat: keep it all in memory? */
/*
*
* song lists: [list id (autoincrement int), (int64) telegram group id, (string) list name ]
* - these are tied to telegram groups
*
* songs : [Song spotify ID (string), list id (foreign key)], votes tally (int), number of votes (int)
* - these are tied to song lists
*
*/
std::string filepath;
sqlite3 *db;
enum error_codes {
NOT_FOUND,
ALREADY_ADDED
};
static int
callback(void *NotUsed, int argc, char **argv, char **azColName)
{
return 0;
}
bool
setup_tables()
{
char *errmsg;
int err;
std::string create_songlist_table =
"CREATE TABLE IF NOT EXISTS songlists ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"groupid INT NOT NULL, "
"name VARCHAR(200) NOT NULL "
");";
std::string create_tracks_table =
"CREATE TABLE IF NOT EXISTS tracks ("
"spotifyid VARCHAR(200) PRIMARY KEY, "
"list INT NOT NULL, "
"vote_total INT NOT NULL,"
"vote_count INT NOT NULL,"
"FOREIGN KEY (list)"
" REFERENCES songlists (id)"
");";
err = sqlite3_exec(db, create_songlist_table.c_str(), NULL, 0, &errmsg);
if (err != SQLITE_OK) {
std::cout << "SQLite Error " << create_songlist_table << "\n" << errmsg << std::endl;
exit (1);
}
err = sqlite3_exec(db, create_tracks_table.c_str(), NULL, 0, &errmsg);
if (err != SQLITE_OK) {
std::cout << "SQLite Error: " << create_tracks_table << "\n" << errmsg << std::endl;
exit (1);
}
return false;
}
public:
db (std::string filepath): filepath(filepath) {
std::ifstream conffile (filepath);
conffile >> data;
songdb (std::string filepath): filepath(filepath) {
int err = sqlite3_open(filepath.c_str(), &db);
if (err) {
std::cout << "Failed to open database: " << sqlite3_errmsg(db);
sqlite3_close(db);
exit(1);
}
setup_tables();
}
bool add_song(std::string spotify_track_id) {
@ -222,6 +291,8 @@ int main() { @@ -222,6 +291,8 @@ int main() {
}
}
kare::songdb data {"test.db"};
std::string teletoken {teletok};
kare::spotify *s;

Loading…
Cancel
Save