User credentials are saved and retrieved from the SQLite database. Next I want to retrieve all songs from icarus and return it to java/kotlin. Some code cleanup

This commit is contained in:
kdeng00
2019-10-07 21:44:20 -04:00
parent bb6b08cf1a
commit 9483c6b834
26 changed files with 601 additions and 637 deletions
+1 -5
View File
@@ -15,7 +15,6 @@ namespace repository { namespace local {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
//SQLite::Database db(m_tableName, SQLite::OPEN_READONLY);
return true;
} catch (std::exception& ex) {
auto msg = ex.what();
@@ -39,7 +38,7 @@ namespace repository { namespace local {
auto msg = ex.what();
}
return false;
return true;
}
@@ -47,11 +46,8 @@ namespace repository { namespace local {
{
try {
const auto dbPath = pathOfDatabase(appPath);
//std::fstream cret(dbPath, std::ios::out);
//cret.close();
SQLite::Database db(dbPath, SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE);
//SQLite::Database db(m_tableName, SQLite::OPEN_CREATE);
} catch (std::exception& ex) {
auto msg = ex.what();
}
+125 -49
View File
@@ -30,24 +30,23 @@
#include "UserRepository.h"
model::Song retrieveSong(const std::string&, const std::string&);
model::Song retrieveSong(const std::string&, const std::string&, const int);
model::User retrieveCredentials(const std::string&);
std::string fetchToken(const std::string&, const std::string&, const std::string&);
bool doesDatabaseExist(const std::string&);
bool userCredentialExist(const std::string&);
void saveCredentials(const model::User&, const std::string&);
model::Song retrieveSong(const std::string& token, const std::string& baseUri)
model::Song retrieveSong(const std::string& token, const std::string& baseUri, const int songId)
{
repository::SongRepository songRepo;
model::Song song;
song.id = 1;
song.title = "Smooth";
song.album = "Amaze";
song.artist = "The Herb";
song.genre = "Blazing";
song.duration = 420;
song.year = 420;
model::Song song(songId, "Smooth", "Amaze", "The Herb", "Blazing",
420, 420);
song = songRepo.retrieveSong(token, baseUri, song);
@@ -55,6 +54,15 @@ model::Song retrieveSong(const std::string& token, const std::string& baseUri)
}
model::User retrieveCredentials(const std::string& dataPath)
{
repository::local::UserRepository userRepo;
auto user = userRepo.retrieveUserCredentials(dataPath);
return user;
}
std::string fetchToken(const std::string& username, const std::string& password,
const std::string& apiUri)
{
@@ -68,6 +76,22 @@ std::string fetchToken(const std::string& username, const std::string& password,
}
bool doesDatabaseExist(const std::string& dataPath)
{
repository::local::UserRepository userRepo;
const auto result = userRepo.databaseExist(dataPath);
return result;
}
bool userCredentialExist(const std::string& dataPath)
{
repository::local::UserRepository userRepo;
return userRepo.isTableEmpty(dataPath);
}
void iterateDirectory(const std::string& path)
{
/**
@@ -85,58 +109,37 @@ void iterateDirectory(const std::string& path)
void saveCredentials(const model::User& user, const std::string& appDirectory)
{
std::string filePath(appDirectory);
filePath.append("/");
repository::local::UserRepository userRepo;
if (!userRepo.databaseExist(filePath)) {
userRepo.initializedDatabase(filePath);
if (!userRepo.databaseExist(appDirectory)) {
userRepo.initializedDatabase(appDirectory);
}
if (!userRepo.doesUserTableExist(filePath)) {
userRepo.createUserTable(filePath);
if (!userRepo.doesUserTableExist(appDirectory)) {
userRepo.createUserTable(appDirectory);
}
if (userRepo.isTableEmpty(filePath)) {
userRepo.deleteUserTable(filePath);
if (userRepo.isTableEmpty(appDirectory)) {
userRepo.deleteUserTable(appDirectory);
}
userRepo.saveUserCred(user, filePath);
}
extern "C"
JNIEXPORT jstring
JNICALL
Java_com_example_mear_activities_DemoStreamActivity_logUser(
JNIEnv *env,
jobject thisObj,
jstring username,
jstring password,
jstring apiUri ) {
const std::string usr = env->GetStringUTFChars(username, NULL);
const std::string pass = env->GetStringUTFChars(password, NULL);
const std::string api = env->GetStringUTFChars(apiUri, NULL);
auto token = fetchToken(usr, pass, api);
return env->NewStringUTF(token.c_str());
userRepo.saveUserCred(user, appDirectory);
}
extern "C"
JNIEXPORT jobject
JNICALL
Java_com_example_mear_activities_DemoStreamActivity_retrieveSong(
Java_com_example_mear_activities_LoginActivity_retrieveSong(
JNIEnv *env,
jobject thisObj,
jstring token,
jstring apiUri
)
jstring apiUri,
jint idOfSong
)
{
const std::string tok = env->GetStringUTFChars(token, NULL);
const std::string baseUri = env->GetStringUTFChars(apiUri, NULL);
auto song = retrieveSong(tok, baseUri);
auto song = retrieveSong(tok, baseUri, idOfSong);
jclass songClass = env->FindClass( "com/example/mear/models/Song");
jmethodID jconstructor = env->GetMethodID( songClass, "<init>", "()V" );
@@ -169,11 +172,87 @@ Java_com_example_mear_activities_DemoStreamActivity_retrieveSong(
return songObj;
}
extern "C"
JNIEXPORT jobject
JNICALL
Java_com_example_mear_activities_LoginActivity_retrieveUserCredentials(
JNIEnv *env,
jobject thisObj,
jstring dataPath
) {
jclass userClass = env->FindClass("com/example/mear/models/User");
jmethodID userContructor = env->GetMethodID(userClass, "<init>", "()V");
jobject userObj = env->NewObject(userClass, userContructor);
const std::string dataPathStr = env->GetStringUTFChars(dataPath, nullptr);
auto user = retrieveCredentials(dataPathStr);
jmethodID usernameId = env->GetMethodID(userClass, "setUsername", "(Ljava/lang/String;)V");
jmethodID passwordId = env->GetMethodID(userClass, "setPassword", "(Ljava/lang/String;)V");
jstring username = env->NewStringUTF(user.username.c_str());
jstring password = env->NewStringUTF(user.password.c_str());
env->CallVoidMethod(userObj, usernameId, username);
env->CallVoidMethod(userObj, passwordId, password);
return userObj;
}
extern "C"
JNIEXPORT jstring
JNICALL
Java_com_example_mear_activities_LoginActivity_logUser(
JNIEnv *env,
jobject thisObj,
jstring username,
jstring password,
jstring apiUri ) {
const std::string usr = env->GetStringUTFChars(username, NULL);
const std::string pass = env->GetStringUTFChars(password, NULL);
const std::string api = env->GetStringUTFChars(apiUri, NULL);
auto token = fetchToken(usr, pass, api);
return env->NewStringUTF(token.c_str());
}
extern "C"
JNIEXPORT jboolean
JNICALL
Java_com_example_mear_activities_LoginActivity_isUserTableEmpty(
JNIEnv *env,
jobject thisObj,
jstring dataPath
) {
const std::string dataPathStr = env->GetStringUTFChars(dataPath, NULL);
jboolean result = userCredentialExist(dataPathStr);
return result;
}
extern "C"
JNIEXPORT jboolean
JNICALL
Java_com_example_mear_activities_LoginActivity_doesDatabaseExist(
JNIEnv *env,
jobject thidObj,
jstring dataPath
) {
const std::string dataPathStr = env->GetStringUTFChars(dataPath, NULL);
jboolean result = doesDatabaseExist(dataPathStr);
return result;
}
extern "C"
JNIEXPORT void
JNICALL
Java_com_example_mear_activities_DemoStreamActivity_pathIteratorDemo(
Java_com_example_mear_activities_LoginActivity_pathIteratorDemo(
JNIEnv *env,
jobject thisObj,
jstring path
@@ -187,21 +266,18 @@ Java_com_example_mear_activities_DemoStreamActivity_pathIteratorDemo(
extern "C"
JNIEXPORT void
JNICALL
Java_com_example_mear_activities_DemoStreamActivity_saveUserCredentials(
Java_com_example_mear_activities_LoginActivity_saveUserCredentials(
JNIEnv *env,
jobject thisObj,
jstring username,
jstring password,
jstring appDirectory
) {
const std::string usernameStr = env->GetStringUTFChars(username, NULL);
const std::string passwordStr = env->GetStringUTFChars(password, NULL);
const std::string appDirectoryStr = env->GetStringUTFChars(appDirectory, NULL);
model::User user;
user.username = usernameStr;
user.password = passwordStr;
model::User user(usernameStr, passwordStr);
saveCredentials(user, appDirectoryStr);
}
-4
View File
@@ -19,7 +19,6 @@ namespace repository { namespace local {
try {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
//SQLite::Database db(m_tableName, SQLite::OPEN_READONLY);
std::string queryString("SELECT * FROM ");
queryString.append(m_tableName);
@@ -50,7 +49,6 @@ namespace repository { namespace local {
try {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
//SQLite::Database db(m_tableName, SQLite::OPEN_READONLY);
return db.tableExists(m_tableName);
} catch (std::exception& ex) {
@@ -66,7 +64,6 @@ namespace repository { namespace local {
try {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
//SQLite::Database db(m_tableName, SQLite::OPEN_READWRITE);
std::string queryString("CREATE TABLE ");
queryString.append(m_tableName);
@@ -97,7 +94,6 @@ namespace repository { namespace local {
try {
const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READWRITE);
//SQLite::Database db(m_tableName, SQLite::OPEN_READWRITE);
std::string queryString("INSERT INTO ");
queryString.append(m_tableName);
queryString.append(" (Username, Password) VALUES (?, ?)");
+1
View File
@@ -19,6 +19,7 @@ namespace repository { namespace local {
model::User retrieveUserCredentials(const std::string&);
bool isEmpty(const std::string&);
bool doesUserTableExist(const std::string&);
void createUserTable(const std::string&);
+15 -1
View File
@@ -10,8 +10,22 @@
namespace model {
struct Song
class Song
{
public:
Song() = default;
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(title), artist(artist), album(album),
genre(genre), duration(duration), year(year) { }
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;
std::string artist;
+5 -2
View File
@@ -6,14 +6,17 @@
#define MEAR_TOKEN_H
#include <string>
#include <utility>
namespace model {
class Token
{
public:
Token(const std::string& token) : token(token) { }
Token(const std::string& accessToken) : accessToken(accessToken) { }
Token(const std::string&& accessToken) :
accessToken(std::move(accessToken)) { }
std::string token;
std::string accessToken;
};
}
+6 -1
View File
@@ -5,13 +5,18 @@
#ifndef MEAR_USER_H
#define MEAR_USER_H
#include <string>
#include <utility>
namespace model {
struct User
class User
{
public:
User() = default;
User(const std::string& user, const std::string& pass) :
username(user), password(pass) { }
User(const std::string&& user, const std::string&& pass) :
username(std::move(user)), password(std::move(pass)) {}
std::string username;
std::string password;