Doing some refactoring and some skeleton work for downloading songs
This commit is contained in:
+3
-3
File diff suppressed because one or more lines are too long
@@ -1,106 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/12/19.
|
||||
//
|
||||
|
||||
#include "APIRepository.h"
|
||||
|
||||
#include <SQLiteCpp/Database.h>
|
||||
|
||||
namespace repository { namespace local {
|
||||
APIRepository::APIRepository()
|
||||
{
|
||||
m_tableName = apiInfoTable();
|
||||
}
|
||||
|
||||
|
||||
model::APIInfo APIRepository::retrieveAPIInfo(const std::string& path)
|
||||
{
|
||||
model::APIInfo apiInfo;
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(path);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
|
||||
std::string queryString("SELECT * FROM ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" LIMIT 1");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
auto result = query.executeStep();
|
||||
apiInfo.uri = query.getColumn(1).getString();
|
||||
apiInfo.version = query.getColumn(2).getInt();
|
||||
|
||||
return apiInfo;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return apiInfo;
|
||||
}
|
||||
|
||||
|
||||
bool APIRepository::doesAPIInfoTableExist(const std::string& path)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(path);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
|
||||
return db.tableExists(m_tableName);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void APIRepository::createAPiInfoTable(const std::string& path)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(path);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
|
||||
std::string queryString("CREATE TABLE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Id INTEGER PRIMARY KEY, Uri TEXT, Version INT)");
|
||||
db.exec(queryString);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void APIRepository::deleteAPIInfo(const model::APIInfo& apiInfo, const std::string& path)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(path);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
|
||||
std::string queryString("DELETE FROM ");
|
||||
queryString.append(m_tableName);
|
||||
|
||||
db.exec(queryString);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void APIRepository::saveAPIInfo(const model::APIInfo& apiInfo, const std::string& path)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(path);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
std::string queryString("INSERT INTO ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Uri, Version) VALUES (?, ?)");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, apiInfo.uri);
|
||||
query.bind(2, apiInfo.version);
|
||||
|
||||
query.exec();
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string APIRepository::apiInfoTable() noexcept { return "APIInfo"; }
|
||||
}}
|
||||
@@ -1,32 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/12/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_APIREPOSITORY_H
|
||||
#define MEAR_APIREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "BaseRepository.h"
|
||||
#include "model/APIInfo.h"
|
||||
|
||||
namespace repository { namespace local {
|
||||
class APIRepository: public BaseRepository {
|
||||
public:
|
||||
APIRepository();
|
||||
|
||||
model::APIInfo retrieveAPIInfo(const std::string&);
|
||||
|
||||
[[deprecated("use the base class function")]]
|
||||
bool doesAPIInfoTableExist(const std::string&);
|
||||
|
||||
void createAPiInfoTable(const std::string&);
|
||||
void deleteAPIInfo(const model::APIInfo&, const std::string&);
|
||||
void saveAPIInfo(const model::APIInfo&, const std::string&);
|
||||
private:
|
||||
std::string apiInfoTable() noexcept;
|
||||
};
|
||||
}}
|
||||
|
||||
|
||||
#endif //MEAR_APIREPOSITORY_H
|
||||
@@ -1,117 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/6/19.
|
||||
//
|
||||
|
||||
#include "BaseRepository.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <SQLiteCpp/SQLiteCpp.h>
|
||||
|
||||
namespace repository { namespace local {
|
||||
bool BaseRepository::databaseExist(const std::string& appPath)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
return true;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BaseRepository::doesTableExist(const std::string& appPath)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
|
||||
auto result = db.tableExists(m_tableName);
|
||||
return result;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BaseRepository::isTableEmpty(const std::string& appPath)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
std::string queryStr("SELECT * FROM ");
|
||||
queryStr.append(m_tableName);
|
||||
|
||||
SQLite::Statement query(db, queryStr);
|
||||
|
||||
const auto result = query.hasRow();
|
||||
|
||||
return result;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void BaseRepository::initializedDatabase(const std::string& appPath)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void BaseRepository::deleteRecord(const std::string& appPath)
|
||||
{
|
||||
try {
|
||||
auto db = getDbConn(appPath, ConnType::ReadWrite);
|
||||
|
||||
std::string queryString("DELETE FROM ");
|
||||
queryString.append(m_tableName);
|
||||
|
||||
db.exec(queryString);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string BaseRepository::pathOfDatabase(const std::string& appPath)
|
||||
{
|
||||
std::string dbPath(appPath);
|
||||
auto lastChar = dbPath.at(dbPath.size()-1);
|
||||
if (lastChar != '/') {
|
||||
dbPath.append("/");
|
||||
}
|
||||
|
||||
dbPath.append(m_databaseName);
|
||||
|
||||
return dbPath;
|
||||
}
|
||||
|
||||
SQLite::Database BaseRepository::getDbConn(const std::string& path, ConnType dbType)
|
||||
{
|
||||
auto dbPath = pathOfDatabase(path);
|
||||
switch (dbType) {
|
||||
case ConnType::ReadOnly:
|
||||
return SQLite::Database(dbPath, SQLite::OPEN_READONLY);
|
||||
case ConnType::ReadWrite:
|
||||
return SQLite::Database(dbPath, SQLite::OPEN_READWRITE);
|
||||
case ConnType::Create:
|
||||
return SQLite::Database(dbPath, SQLite::OPEN_CREATE);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return SQLite::Database(dbPath);
|
||||
}
|
||||
}}
|
||||
@@ -1,41 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/6/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_BASEREPOSITORY_H
|
||||
#define MEAR_BASEREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
#include <exception>
|
||||
|
||||
#include <SQLiteCpp/Database.h>
|
||||
|
||||
namespace repository { namespace local {
|
||||
class BaseRepository {
|
||||
public:
|
||||
bool databaseExist(const std::string&);
|
||||
bool doesTableExist(const std::string&);
|
||||
bool isTableEmpty(const std::string&);
|
||||
|
||||
void initializedDatabase(const std::string&);
|
||||
void deleteRecord(const std::string&);
|
||||
protected:
|
||||
enum class ConnType;
|
||||
std::string pathOfDatabase(const std::string&);
|
||||
|
||||
SQLite::Database getDbConn(const std::string&, ConnType);
|
||||
|
||||
const std::string m_databaseName = "mear.db3";
|
||||
std::string m_tableName;
|
||||
|
||||
enum class ConnType {
|
||||
ReadOnly = 0,
|
||||
ReadWrite,
|
||||
Create
|
||||
};
|
||||
private:
|
||||
};
|
||||
} }
|
||||
|
||||
|
||||
#endif //MEAR_BASEREPOSITORY_H
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
# Sets the minimum version of CMake required to build the native library.
|
||||
|
||||
cmake_minimum_required(VERSION 3.6.0)
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# Creates and names a library, sets it as either STATIC
|
||||
# or SHARED, and provides the relative paths to its source code.
|
||||
@@ -13,41 +13,39 @@ cmake_minimum_required(VERSION 3.6.0)
|
||||
|
||||
# set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
|
||||
|
||||
set(CMAKE_CXX11_EXTENSION_COMPILE_OPTION "-std=c++17")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -O0 -std=c++17")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3 -std=c++17 -DNDEBUG")
|
||||
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
# set(CMAKE_CXX11_EXTENSION_COMPILE_OPTION "-std=c++17")
|
||||
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
|
||||
# set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -O0 -std=c++17")
|
||||
# set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3 -std=c++17 -DNDEBUG")
|
||||
|
||||
set (SOURCES
|
||||
APIRepository.cpp
|
||||
BaseRepository.cpp
|
||||
Demo.cpp
|
||||
RepeatRepository.cpp
|
||||
ShuffleRepository.cpp
|
||||
Tok.cpp
|
||||
TokenRepository.cpp
|
||||
UserRepository.cpp
|
||||
)
|
||||
|
||||
set (HEADERS
|
||||
APIRepository.h
|
||||
BaseRepository.h
|
||||
model/CoverArt.h
|
||||
CoverArtRepository.h
|
||||
Demo.h
|
||||
manager/DirectoryManager.h
|
||||
manager/Tok.h
|
||||
model/APIInfo.h
|
||||
model/CoverArt.h
|
||||
model/Song.h
|
||||
model/Token.h
|
||||
model/User.h
|
||||
RepeatRepository.h
|
||||
RepeatTypes.h
|
||||
ShuffleRepository.h
|
||||
ShuffleTypes.h
|
||||
SongRepository.h
|
||||
Tok.h
|
||||
TokenRepository.h
|
||||
UserRepository.h
|
||||
GeneralUtility.h
|
||||
repository/APIRepository.h
|
||||
repository/BaseRepository.h
|
||||
repository/CoverArtRepository.h
|
||||
repository/RepeatRepository.h
|
||||
repository/ShuffleRepository.h
|
||||
repository/SongRepository.h
|
||||
repository/TokenRepository.h
|
||||
repository/UserRepository.h
|
||||
types/ConnType.h
|
||||
types/RepeatTypes.h
|
||||
types/ShuffleTypes.h
|
||||
utility/GeneralUtility.h
|
||||
)
|
||||
|
||||
set(JSON_BuildTests OFF CACHE INTERNAL "")
|
||||
@@ -88,7 +86,7 @@ set_target_properties(boo PROPERTIES IMPORTED_LOCATION
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/android/${ANDROID_ABI}/libcurl.a"
|
||||
)
|
||||
|
||||
include_directories(${CURL_INCLUDE_DIR} ${SQLITECPP_INCLUDE})
|
||||
include_directories(${CURL_INCLUDE_DIR} ${SQLITECPP_INCLUDE} ${HEADERS})
|
||||
|
||||
target_link_libraries(native-lib PRIVATE
|
||||
nlohmann_json::nlohmann_json
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 11/8/19.
|
||||
//
|
||||
|
||||
#include "CoverArtRepository.h"
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
namespace repository {
|
||||
|
||||
/**
|
||||
template<typename C>
|
||||
std::vector<C> CoverArtRepository<C>::retrieveCoverArtRecords(const model::Token& token,
|
||||
const std::string& uri) {
|
||||
std::vector<std::string> vals;
|
||||
|
||||
return vals;
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
std::vector<data> CoverArtRepository<C>::retrieveCoverArtData(const model::Token& token,
|
||||
const C& cover,
|
||||
const std::string& uri) {
|
||||
std::string fullUri(uri);
|
||||
if (fullUri.at(fullUri.size() - 1) != '/') {
|
||||
fullUri.append("/");
|
||||
}
|
||||
fullUri.append(downloadEndpoint());
|
||||
fullUri.append(std::to_string(cover.id));
|
||||
|
||||
CURL *curl;
|
||||
CURLcode res;
|
||||
struct curl_slist *chunk = nullptr;
|
||||
curl = curl_easy_init();
|
||||
|
||||
std::vector<data> vals;
|
||||
|
||||
if (!curl) {
|
||||
return vals;
|
||||
}
|
||||
|
||||
std::string data;
|
||||
std::string authHeader("Authorization: Bearer ");
|
||||
authHeader.append(token.accessToken);
|
||||
constexpr auto keepAliveHeader = "Content-type: Keep-alive";
|
||||
chunk = curl_slist_append(chunk, authHeader.c_str());
|
||||
chunk = curl_slist_append(chunk, keepAliveHeader);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, fullUri.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, respBodyRetriever);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
|
||||
std::copy(data.begin(), data.end(), vals);
|
||||
|
||||
return vals;
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
C CoverArtRepository<C>::retrieveCoverArtRecord(const model::Token& token,
|
||||
const C& cover,
|
||||
const std::string& uri) {
|
||||
model::CoverArt cov;
|
||||
|
||||
return cov;
|
||||
}
|
||||
|
||||
|
||||
template<typename C>
|
||||
constexpr auto CoverArtRepository<C>::downloadEndpoint() noexcept {
|
||||
return "api/v1/coverart/download";
|
||||
}
|
||||
template<typename C>
|
||||
constexpr auto CoverArtRepository<C>::recordsEndpoint() noexcept {
|
||||
return "api/v1/coverart/";
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
size_t CoverArtRepository<C>::respBodyRetriever(void *ptr, size_t size, size_t nmemb, char *e) {
|
||||
((std::string*)e)->append((char*)ptr, size * nmemb);
|
||||
|
||||
return size * nmemb;
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -23,14 +23,14 @@
|
||||
#include <sqlite3.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "APIRepository.h"
|
||||
#include "CoverArtRepository.h"
|
||||
#include "RepeatRepository.h"
|
||||
#include "ShuffleRepository.h"
|
||||
#include "SongRepository.h"
|
||||
#include "Tok.h"
|
||||
#include "TokenRepository.h"
|
||||
#include "UserRepository.h"
|
||||
#include "repository/APIRepository.h"
|
||||
#include "repository/CoverArtRepository.h"
|
||||
#include "repository/RepeatRepository.h"
|
||||
#include "repository/ShuffleRepository.h"
|
||||
#include "repository/SongRepository.h"
|
||||
#include "manager/Tok.h"
|
||||
#include "repository/TokenRepository.h"
|
||||
#include "repository/UserRepository.h"
|
||||
|
||||
|
||||
|
||||
@@ -228,7 +228,7 @@ void saveCredentials(const model::User& user, const std::string& appDirectory)
|
||||
if (!userRepo.databaseExist(appDirectory)) {
|
||||
userRepo.initializedDatabase(appDirectory);
|
||||
}
|
||||
if (!userRepo.doesUserTableExist(appDirectory)) {
|
||||
if (!userRepo.doesTableExist(appDirectory)) {
|
||||
userRepo.createUserTable(appDirectory);
|
||||
}
|
||||
|
||||
@@ -304,6 +304,22 @@ Java_com_example_mear_repositories_TrackRepository_retrieveSongs(
|
||||
}
|
||||
}
|
||||
|
||||
auto curPath = "/data/data/com.example.mear/";
|
||||
std::string filename;
|
||||
std::string currPathStr(curPath);
|
||||
std::fstream tmp;
|
||||
if (currPathStr.at(currPathStr.size() - 1) == '/') {
|
||||
filename.assign(currPathStr);
|
||||
} else {
|
||||
filename.assign(currPathStr);
|
||||
filename.append("/");
|
||||
}
|
||||
filename.append("test.txt");
|
||||
tmp.open(filename.c_str(), std::ios::out);
|
||||
auto content = "hello fs";
|
||||
tmp.write(content, std::strlen(content));
|
||||
tmp.close();
|
||||
|
||||
return songs;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/21/19.
|
||||
//
|
||||
|
||||
#include "RepeatRepository.h"
|
||||
|
||||
namespace repository { namespace local {
|
||||
RepeatRepository::RepeatRepository() {
|
||||
m_tableName = repeatTable();
|
||||
}
|
||||
|
||||
|
||||
RepeatTypes RepeatRepository::retrieveRepeatMode(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadOnly);
|
||||
std::string queryString("SELECT * FROM ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" LIMIT 1");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
const auto result = query.executeStep();
|
||||
|
||||
auto repeatType = query.getColumn(1).getInt();
|
||||
auto val = static_cast<RepeatTypes>(repeatType);
|
||||
return val;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return RepeatTypes::RepeatOff;
|
||||
}
|
||||
|
||||
|
||||
void RepeatRepository::createRepeatTable(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
std::string queryString("CREATE TABLE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Id INTEGER PRIMARY KEY, RepeatMode INT)");
|
||||
|
||||
db.exec(queryString);
|
||||
|
||||
queryString = "INSERT INTO " + m_tableName + " (RepeatMode) VALUES(?)";
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, static_cast<int>(RepeatTypes::RepeatOff));
|
||||
query.exec();
|
||||
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void RepeatRepository::updateRepeat(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
|
||||
if (isTableEmpty(path)) {
|
||||
std::string queryString("INSERT INTO ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (RepeatMode) VALUES (?)");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, static_cast<int>(RepeatTypes::RepeatOff));
|
||||
query.exec();
|
||||
} else {
|
||||
std::string queryString("UPDATE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" SET RepeatMode = ?");
|
||||
|
||||
auto repeatMode = retrieveRepeatMode(path);
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
switch (repeatMode) {
|
||||
case RepeatTypes::RepeatOff:
|
||||
query.bind(1, static_cast<int>(RepeatTypes::RepeatSong));
|
||||
break;
|
||||
case RepeatTypes::RepeatSong:
|
||||
query.bind(1, static_cast<int>(RepeatTypes::RepeatOff));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
query.exec();
|
||||
}
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string RepeatRepository::repeatTable() noexcept { return "Repeat"; }
|
||||
}}
|
||||
@@ -1,28 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/21/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_REPEATREPOSITORY_H
|
||||
#define MEAR_REPEATREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "BaseRepository.h"
|
||||
#include "RepeatTypes.h"
|
||||
|
||||
namespace repository { namespace local {
|
||||
class RepeatRepository : public BaseRepository {
|
||||
public:
|
||||
RepeatRepository();
|
||||
|
||||
RepeatTypes retrieveRepeatMode(const std::string&);
|
||||
|
||||
void createRepeatTable(const std::string&);
|
||||
void updateRepeat(const std::string&);
|
||||
private:
|
||||
std::string repeatTable() noexcept;
|
||||
};
|
||||
}}
|
||||
|
||||
|
||||
#endif //MEAR_REPEATREPOSITORY_H
|
||||
@@ -1,98 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/21/19.
|
||||
//
|
||||
|
||||
#include "ShuffleRepository.h"
|
||||
|
||||
namespace repository { namespace local {
|
||||
ShuffleRepository::ShuffleRepository() {
|
||||
m_tableName = shuffleTable();
|
||||
}
|
||||
|
||||
|
||||
ShuffleTypes ShuffleRepository::retrieveShuffleMode(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadOnly);
|
||||
std::string queryString("SELECT * FROM ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" LIMIT 1");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
const auto result = query.executeStep();
|
||||
auto shuffleType = query.getColumn(1).getInt();
|
||||
auto val = static_cast<ShuffleTypes>(shuffleType);
|
||||
|
||||
return val;
|
||||
|
||||
} catch (std::exception &ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return ShuffleTypes::ShuffleOff;
|
||||
}
|
||||
|
||||
|
||||
void ShuffleRepository::createShuffleTable(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
std::string queryString("CREATE TABLE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Id INTEGER PRIMARY KEY, ShuffleMode INT)");
|
||||
|
||||
db.exec(queryString);
|
||||
|
||||
queryString = "INSERT INTO ";
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (ShuffleMode) VALUES(?)");
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, static_cast<int>(ShuffleTypes::ShuffleOff));
|
||||
query.exec();
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void ShuffleRepository::updateShuffle(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
|
||||
if (!doesTableExist(path)) {
|
||||
createShuffleTable(path);
|
||||
}
|
||||
|
||||
if (isTableEmpty(path)) {
|
||||
constexpr auto queryString = "INSERT INTO ? (ShuffleMode) VALUES(?)";
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, m_tableName);
|
||||
query.bind(2, static_cast<int>(ShuffleTypes::ShuffleOff));
|
||||
query.exec();
|
||||
} else {
|
||||
std::string queryString("UPDATE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" SET ShuffleMode = ?");
|
||||
|
||||
auto shuffleMode = retrieveShuffleMode(path);
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
switch (shuffleMode) {
|
||||
case ShuffleTypes::ShuffleOff:
|
||||
query.bind(1, static_cast<int>(ShuffleTypes::ShuffleOn));
|
||||
break;
|
||||
case ShuffleTypes ::ShuffleOn:
|
||||
query.bind(1, static_cast<int>(ShuffleTypes::ShuffleOff));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
query.exec();
|
||||
}
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string ShuffleRepository::shuffleTable() noexcept { return "Shuffle"; }
|
||||
}}
|
||||
@@ -1,28 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/21/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_SHUFFLEREPOSITORY_H
|
||||
#define MEAR_SHUFFLEREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "BaseRepository.h"
|
||||
#include "ShuffleTypes.h"
|
||||
|
||||
namespace repository { namespace local {
|
||||
class ShuffleRepository : public BaseRepository {
|
||||
public:
|
||||
ShuffleRepository();
|
||||
|
||||
ShuffleTypes retrieveShuffleMode(const std::string&);
|
||||
|
||||
void createShuffleTable(const std::string&);
|
||||
void updateShuffle(const std::string&);
|
||||
private:
|
||||
std::string shuffleTable() noexcept;
|
||||
};
|
||||
}}
|
||||
|
||||
|
||||
#endif //MEAR_SHUFFLEREPOSITORY_H
|
||||
@@ -1,33 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/3/19.
|
||||
//
|
||||
|
||||
#include "SongRepository.h"
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace repository {
|
||||
/**
|
||||
std::vector<model::Song> SongRepository::fetchSongs(const model::Token& token,
|
||||
const std::string& uri)
|
||||
{
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
model::Song SongRepository::retrieveSong(const model::Token& token, const model::Song& sng,
|
||||
const std::string& baseUri) {
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
size_t SongRepository::respBodyRetriever(void* ptr, size_t size, size_t nmemb, char *e)
|
||||
{
|
||||
((std::string*)e)->append((char*)ptr, size * nmemb);
|
||||
return size * nmemb;
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/1/19.
|
||||
//
|
||||
|
||||
#include "Tok.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace manager {
|
||||
model::Token Tok::fetchTokenTrans(const model::User &user, const std::string& uri) {
|
||||
CURL *curl;
|
||||
struct curl_slist *chunk = nullptr;
|
||||
CURLcode res;
|
||||
|
||||
curl = curl_easy_init();
|
||||
|
||||
if (!curl) {
|
||||
return model::Token("none");
|
||||
}
|
||||
|
||||
std::string resp;
|
||||
const auto loginUri = fetchLoginUri(uri);
|
||||
const auto obj = userJsonString(user);
|
||||
|
||||
constexpr auto contentType = "Content-type: Keep-alive";
|
||||
chunk = curl_slist_append(chunk , contentType);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, loginUri.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, obj.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, respBodyRetriever);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
if (res == CURLE_OK) {
|
||||
auto s = nlohmann::json::parse(resp);
|
||||
const auto tokenStr = std::move(s["token"].get<std::string>());
|
||||
|
||||
return model::Token(std::move(tokenStr));
|
||||
}
|
||||
|
||||
return model::Token("failure");
|
||||
}
|
||||
|
||||
|
||||
constexpr auto Tok::loginEndpoint() noexcept
|
||||
{
|
||||
return "api/v1/login";
|
||||
}
|
||||
|
||||
std::string Tok::fetchLoginUri(const std::string& uriBase) noexcept
|
||||
{
|
||||
std::string uri(uriBase);
|
||||
if (uri.at(uri.size() - 1) != '/') {
|
||||
uri.append("/");
|
||||
}
|
||||
uri.append(loginEndpoint());
|
||||
|
||||
return uri;
|
||||
}
|
||||
std::string Tok::userJsonString(const model::User& user)
|
||||
{
|
||||
nlohmann::json usr;
|
||||
usr["username"] = user.username;
|
||||
usr["password"] = user.password;
|
||||
|
||||
return usr.dump();
|
||||
}
|
||||
|
||||
|
||||
size_t Tok::respBodyRetriever(void* ptr, size_t size, size_t nmemb, char *e)
|
||||
{
|
||||
((std::string*)e)->append((char*)ptr, size * nmemb);
|
||||
|
||||
return size * nmemb;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/1/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_TOK_H
|
||||
#define MEAR_TOK_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "model/Token.h"
|
||||
#include "model/User.h"
|
||||
|
||||
namespace manager {
|
||||
|
||||
class Tok {
|
||||
public:
|
||||
model::Token fetchTokenTrans(const model::User&, const std::string&);
|
||||
private:
|
||||
std::string fetchLoginUri(const std::string&) noexcept;
|
||||
constexpr auto loginEndpoint() noexcept;
|
||||
std::string userJsonString(const model::User&);
|
||||
|
||||
static size_t respBodyRetriever(void*, size_t, size_t, char*);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif //MEAR_TOK_H
|
||||
@@ -1,76 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/6/19.
|
||||
//
|
||||
|
||||
#include "TokenRepository.h"
|
||||
|
||||
#include <SQLiteCpp/Database.h>
|
||||
|
||||
namespace repository { namespace local {
|
||||
TokenRepository::TokenRepository()
|
||||
{
|
||||
m_tableName = tokenTable();
|
||||
}
|
||||
|
||||
|
||||
model::Token TokenRepository::retrieveToken(const std::string& path)
|
||||
{
|
||||
model::Token token;
|
||||
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadOnly);
|
||||
std::string queryString("SELECT * FROM ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" LIMIT 1");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
auto result = query.executeStep();
|
||||
//const std::string t(query.getColumn(1).getString());
|
||||
token.accessToken = std::move(query.getColumn(1).getString());
|
||||
|
||||
return token;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
void TokenRepository::createTokenTable(const std::string& path)
|
||||
{
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
|
||||
std::string queryString("CREATE TABLE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Id INTEGER PRIMARY KEY, AccessToken TEXT)");
|
||||
|
||||
db.exec(queryString);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void TokenRepository::saveToken(const model::Token& token, const std::string& path)
|
||||
{
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
|
||||
std::string queryString("INSERT INTO ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (AccessToken) VALUES (?)");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, token.accessToken);
|
||||
|
||||
query.exec();
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string TokenRepository::tokenTable() noexcept { return "Token"; }
|
||||
}}
|
||||
@@ -1,28 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/6/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_TOKENREPOSITORY_H
|
||||
#define MEAR_TOKENREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "model/Token.h"
|
||||
#include "BaseRepository.h"
|
||||
|
||||
namespace repository { namespace local {
|
||||
class TokenRepository: public BaseRepository {
|
||||
public:
|
||||
TokenRepository();
|
||||
|
||||
model::Token retrieveToken(const std::string&);
|
||||
|
||||
void createTokenTable(const std::string&);
|
||||
void saveToken(const model::Token&, const std::string&);
|
||||
private:
|
||||
std::string tokenTable() noexcept;
|
||||
};
|
||||
}}
|
||||
|
||||
|
||||
#endif //MEAR_TOKENREPOSITORY_H
|
||||
@@ -1,119 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/6/19.
|
||||
//
|
||||
|
||||
#include "UserRepository.h"
|
||||
|
||||
#include <SQLiteCpp/SQLiteCpp.h>
|
||||
|
||||
namespace repository { namespace local {
|
||||
UserRepository::UserRepository()
|
||||
{
|
||||
m_tableName = userTable();
|
||||
}
|
||||
|
||||
|
||||
model::User UserRepository::retrieveUserCredentials(const std::string &appPath)
|
||||
{
|
||||
model::User user;
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
|
||||
std::string queryString("SELECT * FROM ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" LIMIT 1");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
auto result = query.executeStep();
|
||||
|
||||
auto valZero = query.getColumn(1);
|
||||
auto valOne = query.getColumn(2);
|
||||
|
||||
user.username = valZero.getString();
|
||||
user.password = valOne.getString();
|
||||
|
||||
return user;
|
||||
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
[[deprecated("use the base class doesTableExist() function")]]
|
||||
bool UserRepository::doesUserTableExist(const std::string &appPath)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
|
||||
return db.tableExists(m_tableName);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void UserRepository::createUserTable(const std::string &appPath)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
|
||||
std::string queryString("CREATE TABLE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Id INTEGER PRIMARY KEY, Username TEXT, Password TEXT)");
|
||||
db.exec(queryString);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void UserRepository::deleteUserTable(const std::string& appPath)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
|
||||
std::string queryStr("DELETE FROM ");
|
||||
queryStr.append(m_tableName);
|
||||
|
||||
db.exec(queryStr);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void UserRepository::saveUserCred(const model::User & user, const std::string& appPath)
|
||||
{
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
std::string queryString("INSERT INTO ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Username, Password) VALUES (?, ?)");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, user.username);
|
||||
query.bind(2, user.password);
|
||||
|
||||
query.exec();
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string UserRepository::userTable()
|
||||
{
|
||||
constexpr auto table = "User";
|
||||
|
||||
return table;
|
||||
}
|
||||
}}
|
||||
@@ -1,33 +0,0 @@
|
||||
//
|
||||
// Created by brahmix on 10/6/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_USERREPOSITORY_H
|
||||
#define MEAR_USERREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "BaseRepository.h"
|
||||
#include "model/User.h"
|
||||
|
||||
|
||||
namespace repository { namespace local {
|
||||
|
||||
class UserRepository : public BaseRepository {
|
||||
public:
|
||||
UserRepository();
|
||||
|
||||
model::User retrieveUserCredentials(const std::string&);
|
||||
|
||||
bool doesUserTableExist(const std::string&);
|
||||
|
||||
void createUserTable(const std::string&);
|
||||
void deleteUserTable(const std::string&);
|
||||
void saveUserCred(const model::User&, const std::string&);
|
||||
private:
|
||||
std::string userTable();
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif //MEAR_USERREPOSITORY_H
|
||||
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by brahmix on 11/29/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_DIRECTORYMANAGER_H
|
||||
#define MEAR_DIRECTORYMANAGER_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../model/Song.h"
|
||||
|
||||
namespace manager {
|
||||
class DirectoryManager {
|
||||
public:
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
Str fullSongPath(const Song& song, const Str& path) {
|
||||
auto s = "";
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
Str albumPath(const Song& song, const Str& path) {
|
||||
auto ss = "";
|
||||
|
||||
return ss;
|
||||
}
|
||||
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
Str artistPath(const Song& song, const Str& path) {
|
||||
return "art";
|
||||
}
|
||||
|
||||
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
bool doesSongExist(const Song& song, const Str& path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
void createSongDirectory(const Song& song, const Str& path) {
|
||||
|
||||
}
|
||||
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
void deleteSongDirectory(const Song& song, const Str& path) {
|
||||
|
||||
}
|
||||
private:
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MEAR_DIRECTORYMANAGER_H
|
||||
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// Created by brahmix on 10/1/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_TOK_H
|
||||
#define MEAR_TOK_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "../model/Token.h"
|
||||
#include "../model/User.h"
|
||||
|
||||
namespace manager {
|
||||
template<class Token = model::Token>
|
||||
class Tok {
|
||||
public:
|
||||
template<class User = model::User>
|
||||
Token fetchTokenTrans(const User& user, const std::string& uri) {
|
||||
CURL *curl;
|
||||
struct curl_slist *chunk = nullptr;
|
||||
CURLcode res;
|
||||
|
||||
curl = curl_easy_init();
|
||||
|
||||
if (!curl) {
|
||||
return model::Token("none");
|
||||
}
|
||||
|
||||
std::string resp;
|
||||
const auto loginUri = fetchLoginUri(uri);
|
||||
const auto obj = userJsonString(user);
|
||||
|
||||
constexpr auto contentType = "Content-type: Keep-alive";
|
||||
chunk = curl_slist_append(chunk, contentType);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, loginUri.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, obj.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, respBodyRetriever);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
if (res == CURLE_OK) {
|
||||
auto s = nlohmann::json::parse(resp);
|
||||
const auto tokenStr = std::move(s["token"].get<std::string>());
|
||||
|
||||
return Token(std::move(tokenStr));
|
||||
}
|
||||
|
||||
return Token("failure");
|
||||
}
|
||||
private:
|
||||
std::string fetchLoginUri(const std::string& uriBase) noexcept {
|
||||
std::string uri(uriBase);
|
||||
if (uri.at(uri.size() - 1) != '/') {
|
||||
uri.append("/");
|
||||
}
|
||||
uri.append(loginEndpoint());
|
||||
|
||||
return uri;
|
||||
}
|
||||
|
||||
std::string userJsonString(const model::User& user) {
|
||||
nlohmann::json usr;
|
||||
usr["username"] = user.username;
|
||||
usr["password"] = user.password;
|
||||
|
||||
return usr.dump();
|
||||
}
|
||||
|
||||
|
||||
constexpr auto loginEndpoint() noexcept {
|
||||
return "api/v1/login";
|
||||
}
|
||||
|
||||
|
||||
static size_t respBodyRetriever(void* ptr, size_t size, size_t nmemb, char* e) {
|
||||
((std::string*)e)->append((char*)ptr, size * nmemb);
|
||||
|
||||
return size * nmemb;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif //MEAR_TOK_H
|
||||
@@ -26,14 +26,6 @@ public:
|
||||
const int duration, const int year, const int coverArtId) :
|
||||
id(id), title(title), artist(artist), album(album), albumArtist(albumArtist),
|
||||
genre(genre), duration(duration), year(year), coverArtId(coverArtId) { }
|
||||
/**
|
||||
Song(const int id, const std::string&& title, const std::string&& artist,
|
||||
const std::string&& album, const std::string&& genre,
|
||||
const int duration, const int year) :
|
||||
id(id), title(std::move(title)), artist(std::move(artist)),
|
||||
album(std::move(album)), genre(std::move(genre)), duration(duration),
|
||||
year(year) { }
|
||||
*/
|
||||
|
||||
int id;
|
||||
std::string title;
|
||||
@@ -45,6 +37,8 @@ Song(const int id, const std::string&& title, const std::string&& artist,
|
||||
int year;
|
||||
int coverArtId;
|
||||
std::vector<char> data;
|
||||
bool downloaded;
|
||||
std::string path;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
//
|
||||
// Created by brahmix on 10/12/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_APIREPOSITORY_H
|
||||
#define MEAR_APIREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "BaseRepository.h"
|
||||
#include "../model/APIInfo.h"
|
||||
|
||||
namespace repository { namespace local {
|
||||
class APIRepository: public BaseRepository {
|
||||
public:
|
||||
APIRepository() {
|
||||
m_tableName = apiInfoTable();
|
||||
}
|
||||
|
||||
|
||||
model::APIInfo retrieveAPIInfo(const std::string& path) {
|
||||
model::APIInfo apiInfo;
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(path);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
|
||||
std::string queryString("SELECT * FROM ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" LIMIT 1");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
auto result = query.executeStep();
|
||||
apiInfo.uri = query.getColumn(1).getString();
|
||||
apiInfo.version = query.getColumn(2).getInt();
|
||||
|
||||
return apiInfo;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return apiInfo;
|
||||
}
|
||||
|
||||
|
||||
[[deprecated("use the base class function")]]
|
||||
bool doesAPIInfoTableExist(const std::string& path) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(path);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
|
||||
return db.tableExists(m_tableName);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void createAPiInfoTable(const std::string& path) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(path);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
|
||||
std::string queryString("CREATE TABLE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Id INTEGER PRIMARY KEY, Uri TEXT, Version INT)");
|
||||
db.exec(queryString);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void deleteAPIInfo(const model::APIInfo& apiInfo, const std::string& path) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(path);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
|
||||
std::string queryString("DELETE FROM ");
|
||||
queryString.append(m_tableName);
|
||||
|
||||
db.exec(queryString);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void saveAPIInfo(const model::APIInfo& apiInfo, const std::string& path) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(path);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
std::string queryString("INSERT INTO ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Uri, Version) VALUES (?, ?)");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, apiInfo.uri);
|
||||
query.bind(2, apiInfo.version);
|
||||
|
||||
query.exec();
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
private:
|
||||
std::string apiInfoTable() noexcept { return "APIInfo"; }
|
||||
};
|
||||
}}
|
||||
|
||||
|
||||
#endif //MEAR_APIREPOSITORY_H
|
||||
@@ -0,0 +1,125 @@
|
||||
//
|
||||
// Created by brahmix on 10/6/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_BASEREPOSITORY_H
|
||||
#define MEAR_BASEREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
#include <exception>
|
||||
|
||||
#include <SQLiteCpp/Database.h>
|
||||
|
||||
#include "../types/ConnType.h"
|
||||
|
||||
namespace repository { namespace local {
|
||||
using type::ConnType;
|
||||
|
||||
class BaseRepository {
|
||||
public:
|
||||
bool databaseExist(const std::string& appPath) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
return true;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool doesTableExist(const std::string& appPath) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
|
||||
auto result = db.tableExists(m_tableName);
|
||||
return result;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isTableEmpty(const std::string& appPath) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
std::string queryStr("SELECT * FROM ");
|
||||
queryStr.append(m_tableName);
|
||||
|
||||
SQLite::Statement query(db, queryStr);
|
||||
|
||||
const auto result = query.hasRow();
|
||||
|
||||
return result;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void initializedDatabase(const std::string& appPath) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void deleteRecord(const std::string& appPath) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
protected:
|
||||
std::string pathOfDatabase(const std::string& appPath) {
|
||||
std::string dbPath(appPath);
|
||||
auto lastChar = dbPath.at(dbPath.size()-1);
|
||||
if (lastChar != '/') {
|
||||
dbPath.append("/");
|
||||
}
|
||||
|
||||
dbPath.append(databaseName());
|
||||
|
||||
return dbPath;
|
||||
}
|
||||
|
||||
template<typename Str = std::string>
|
||||
const Str databaseName() noexcept { return "mear.db3"; }
|
||||
|
||||
|
||||
SQLite::Database getDbConn(const std::string& path, ConnType dbType) {
|
||||
auto dbPath = pathOfDatabase(path);
|
||||
switch (dbType) {
|
||||
case ConnType::ReadOnly:
|
||||
return SQLite::Database(dbPath, SQLite::OPEN_READONLY);
|
||||
case ConnType::ReadWrite:
|
||||
return SQLite::Database(dbPath, SQLite::OPEN_READWRITE);
|
||||
case ConnType::Create:
|
||||
return SQLite::Database(dbPath, SQLite::OPEN_CREATE);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return SQLite::Database(dbPath);
|
||||
}
|
||||
|
||||
std::string m_tableName;
|
||||
private:
|
||||
};
|
||||
} }
|
||||
|
||||
|
||||
#endif //MEAR_BASEREPOSITORY_H
|
||||
+3
-3
@@ -10,10 +10,10 @@
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include "../3rdparty/android/include/curl/curl.h"
|
||||
|
||||
#include "model/CoverArt.h"
|
||||
#include "model/Token.h"
|
||||
#include "../model/CoverArt.h"
|
||||
#include "../model/Token.h"
|
||||
|
||||
using data = char;
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
//
|
||||
// Created by brahmix on 10/21/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_REPEATREPOSITORY_H
|
||||
#define MEAR_REPEATREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "BaseRepository.h"
|
||||
#include "../types/RepeatTypes.h"
|
||||
|
||||
namespace repository { namespace local {
|
||||
class RepeatRepository : public BaseRepository {
|
||||
public:
|
||||
RepeatRepository() {
|
||||
m_tableName = repeatTable();
|
||||
}
|
||||
|
||||
|
||||
RepeatTypes retrieveRepeatMode(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadOnly);
|
||||
std::string queryString("SELECT * FROM ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" LIMIT 1");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
const auto result = query.executeStep();
|
||||
|
||||
auto repeatType = query.getColumn(1).getInt();
|
||||
auto val = static_cast<RepeatTypes>(repeatType);
|
||||
return val;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return RepeatTypes::RepeatOff;
|
||||
}
|
||||
|
||||
|
||||
void createRepeatTable(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
std::string queryString("CREATE TABLE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Id INTEGER PRIMARY KEY, RepeatMode INT)");
|
||||
|
||||
db.exec(queryString);
|
||||
|
||||
queryString = "INSERT INTO " + m_tableName + " (RepeatMode) VALUES(?)";
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, static_cast<int>(RepeatTypes::RepeatOff));
|
||||
query.exec();
|
||||
|
||||
} catch (std::exception &ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void updateRepeat(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
|
||||
if (isTableEmpty(path)) {
|
||||
std::string queryString("INSERT INTO ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (RepeatMode) VALUES (?)");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, static_cast<int>(RepeatTypes::RepeatOff));
|
||||
query.exec();
|
||||
} else {
|
||||
std::string queryString("UPDATE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" SET RepeatMode = ?");
|
||||
|
||||
auto repeatMode = retrieveRepeatMode(path);
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
switch (repeatMode) {
|
||||
case RepeatTypes::RepeatOff:
|
||||
query.bind(1, static_cast<int>(RepeatTypes::RepeatSong));
|
||||
break;
|
||||
case RepeatTypes::RepeatSong:
|
||||
query.bind(1, static_cast<int>(RepeatTypes::RepeatOff));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
query.exec();
|
||||
}
|
||||
} catch (std::exception &ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
private:
|
||||
std::string repeatTable() noexcept { return "Repeat"; }
|
||||
};
|
||||
}}
|
||||
|
||||
|
||||
#endif //MEAR_REPEATREPOSITORY_H
|
||||
@@ -0,0 +1,109 @@
|
||||
//
|
||||
// Created by brahmix on 10/21/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_SHUFFLEREPOSITORY_H
|
||||
#define MEAR_SHUFFLEREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "BaseRepository.h"
|
||||
#include "../types/ShuffleTypes.h"
|
||||
|
||||
namespace repository { namespace local {
|
||||
class ShuffleRepository : public BaseRepository {
|
||||
public:
|
||||
ShuffleRepository() {
|
||||
m_tableName = shuffleTable();
|
||||
}
|
||||
|
||||
|
||||
ShuffleTypes retrieveShuffleMode(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadOnly);
|
||||
std::string queryString("SELECT * FROM ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" LIMIT 1");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
const auto result = query.executeStep();
|
||||
auto shuffleType = query.getColumn(1).getInt();
|
||||
auto val = static_cast<ShuffleTypes>(shuffleType);
|
||||
|
||||
return val;
|
||||
|
||||
} catch (std::exception &ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return ShuffleTypes::ShuffleOff;
|
||||
}
|
||||
|
||||
|
||||
void createShuffleTable(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
std::string queryString("CREATE TABLE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Id INTEGER PRIMARY KEY, ShuffleMode INT)");
|
||||
|
||||
db.exec(queryString);
|
||||
|
||||
queryString = "INSERT INTO ";
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (ShuffleMode) VALUES(?)");
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, static_cast<int>(ShuffleTypes::ShuffleOff));
|
||||
query.exec();
|
||||
} catch (std::exception &ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void updateShuffle(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
|
||||
if (!doesTableExist(path)) {
|
||||
createShuffleTable(path);
|
||||
}
|
||||
|
||||
if (isTableEmpty(path)) {
|
||||
constexpr auto queryString = "INSERT INTO ? (ShuffleMode) VALUES(?)";
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, m_tableName);
|
||||
query.bind(2, static_cast<int>(ShuffleTypes::ShuffleOff));
|
||||
query.exec();
|
||||
} else {
|
||||
std::string queryString("UPDATE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" SET ShuffleMode = ?");
|
||||
|
||||
auto shuffleMode = retrieveShuffleMode(path);
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
switch (shuffleMode) {
|
||||
case ShuffleTypes::ShuffleOff:
|
||||
query.bind(1, static_cast<int>(ShuffleTypes::ShuffleOn));
|
||||
break;
|
||||
case ShuffleTypes::ShuffleOn:
|
||||
query.bind(1, static_cast<int>(ShuffleTypes::ShuffleOff));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
query.exec();
|
||||
}
|
||||
} catch (std::exception &ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
private:
|
||||
std::string shuffleTable() noexcept { return "Shuffle"; }
|
||||
};
|
||||
}}
|
||||
|
||||
|
||||
#endif //MEAR_SHUFFLEREPOSITORY_H
|
||||
@@ -8,12 +8,12 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "../3rdparty/android/include/curl/curl.h"
|
||||
#include "../3rdparty/json/single_include/nlohmann/json.hpp"
|
||||
|
||||
#include "model/Song.h"
|
||||
#include "model/Token.h"
|
||||
#include "GeneralUtility.h"
|
||||
#include "../model/Song.h"
|
||||
#include "../model/Token.h"
|
||||
#include "../utility/GeneralUtility.h"
|
||||
|
||||
namespace repository {
|
||||
|
||||
@@ -61,10 +61,6 @@ namespace repository {
|
||||
songJson["album_artist"].get<std::string>(),
|
||||
songJson["genre"].get<std::string>(), songJson["duration"].get<int>(),
|
||||
songJson["year"].get<int>(), songJson["coverart_id"].get<int>());
|
||||
/**
|
||||
song.albumArtist = songJson["album_artist"].get<std::string>();
|
||||
song.coverArtId = songJson["coverart_id"].get<int>();
|
||||
*/
|
||||
|
||||
songs.push_back(song);
|
||||
}
|
||||
@@ -113,17 +109,6 @@ namespace repository {
|
||||
s["album_artist"].get<std::string>(), s["genre"].get<std::string>(),
|
||||
s["duration"].get<int>(), s["year"].get<int>(),
|
||||
s["coverart_id"].get<int>());
|
||||
/**
|
||||
song.id = s["id"].get<int>();
|
||||
song.title = s["title"].get<std::string>();
|
||||
song.album = s["album"].get<std::string>();
|
||||
song.albumArtist = s["album_artist"].get<std::string>();
|
||||
song.artist = s["artist"].get<std::string>();
|
||||
song.genre = s["genre"].get<std::string>();
|
||||
song.duration = s["duration"].get<int>();
|
||||
song.year = s["year"].get<int>();
|
||||
song.coverArtId = s["coverart_id"].get<int>();
|
||||
*/
|
||||
|
||||
return song;
|
||||
}
|
||||
@@ -164,13 +149,9 @@ namespace repository {
|
||||
return size * nmemb;
|
||||
}
|
||||
|
||||
constexpr auto songRecordEndpoint() noexcept {
|
||||
return "api/v1/song/";
|
||||
}
|
||||
constexpr auto songRecordEndpoint() noexcept { return "api/v1/song/"; }
|
||||
|
||||
constexpr auto songDownloadEndpoint() noexcept {
|
||||
return "api/v1/song/data/";
|
||||
}
|
||||
constexpr auto songDownloadEndpoint() noexcept { return "api/v1/song/data/"; }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// Created by brahmix on 10/6/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_TOKENREPOSITORY_H
|
||||
#define MEAR_TOKENREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../model/Token.h"
|
||||
#include "BaseRepository.h"
|
||||
|
||||
namespace repository { namespace local {
|
||||
template<class Token = model::Token>
|
||||
class TokenRepository: public BaseRepository {
|
||||
public:
|
||||
TokenRepository() {
|
||||
m_tableName = tokenTable();
|
||||
}
|
||||
|
||||
|
||||
Token retrieveToken(const std::string& path) {
|
||||
Token token;
|
||||
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadOnly);
|
||||
std::string queryString("SELECT * FROM ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" LIMIT 1");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
auto result = query.executeStep();
|
||||
token.accessToken = std::move(query.getColumn(1).getString());
|
||||
|
||||
return token;
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
void createTokenTable(const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
|
||||
std::string queryString("CREATE TABLE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Id INTEGER PRIMARY KEY, AccessToken TEXT)");
|
||||
|
||||
db.exec(queryString);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void saveToken(const Token& token, const std::string& path) {
|
||||
try {
|
||||
auto db = getDbConn(path, ConnType::ReadWrite);
|
||||
|
||||
std::string queryString("INSERT INTO ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (AccessToken) VALUES (?)");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, token.accessToken);
|
||||
|
||||
query.exec();
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
private:
|
||||
std::string tokenTable() noexcept { return "Token"; }
|
||||
};
|
||||
}}
|
||||
|
||||
|
||||
#endif //MEAR_TOKENREPOSITORY_H
|
||||
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// Created by brahmix on 10/6/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_USERREPOSITORY_H
|
||||
#define MEAR_USERREPOSITORY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "BaseRepository.h"
|
||||
#include "../model/User.h"
|
||||
|
||||
|
||||
namespace repository { namespace local {
|
||||
template<class User = model::User>
|
||||
class UserRepository : public BaseRepository {
|
||||
public:
|
||||
UserRepository() {
|
||||
m_tableName = userTable();
|
||||
}
|
||||
|
||||
|
||||
User retrieveUserCredentials(const std::string& appPath) {
|
||||
User user;
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
|
||||
|
||||
std::string queryString("SELECT * FROM ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" LIMIT 1");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
|
||||
auto result = query.executeStep();
|
||||
|
||||
auto valZero = query.getColumn(1);
|
||||
auto valOne = query.getColumn(2);
|
||||
|
||||
user.username = valZero.getString();
|
||||
user.password = valOne.getString();
|
||||
|
||||
return user;
|
||||
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
void createUserTable(const std::string& appPath) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
|
||||
std::string queryString("CREATE TABLE ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Id INTEGER PRIMARY KEY, Username TEXT, Password TEXT)");
|
||||
db.exec(queryString);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void deleteUserTable(const std::string& appPath) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
|
||||
std::string queryStr("DELETE FROM ");
|
||||
queryStr.append(m_tableName);
|
||||
|
||||
db.exec(queryStr);
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void saveUserCred(const User& user, const std::string& appPath) {
|
||||
try {
|
||||
const auto dbPath = pathOfDatabase(appPath);
|
||||
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
|
||||
std::string queryString("INSERT INTO ");
|
||||
queryString.append(m_tableName);
|
||||
queryString.append(" (Username, Password) VALUES (?, ?)");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, user.username);
|
||||
query.bind(2, user.password);
|
||||
|
||||
query.exec();
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
private:
|
||||
std::string userTable() noexcept { return "User"; }
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif //MEAR_USERREPOSITORY_H
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by brahmix on 11/29/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_CONNTYPE_H
|
||||
#define MEAR_CONNTYPE_H
|
||||
|
||||
namespace type {
|
||||
enum class ConnType {
|
||||
ReadOnly = 0,
|
||||
ReadWrite = 1,
|
||||
Create = 2
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MEAR_CONNTYPE_H
|
||||
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by brahmix on 11/25/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_GENERALUTILITY_H
|
||||
#define MEAR_GENERALUTILITY_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../model/Token.h"
|
||||
|
||||
namespace utility {
|
||||
class GeneralUtility {
|
||||
public:
|
||||
template<typename S = std::string>
|
||||
static S appendForwardSlashToUri(const S& uri) {
|
||||
S fullUri(uri);
|
||||
if (fullUri.at(fullUri.size() - 1) != '/') {
|
||||
fullUri.append("/");
|
||||
}
|
||||
|
||||
return fullUri;
|
||||
}
|
||||
template<typename S = std::string, class Token = model::Token>
|
||||
static S authHeader(const Token& token) {
|
||||
std::string header("Authorization: Bearer ");
|
||||
header.append(token.accessToken);
|
||||
|
||||
return header;
|
||||
}
|
||||
private:
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MEAR_GENERALUTILITY_H
|
||||
Reference in New Issue
Block a user