Working on saving the api information to the database and then saving the token. After that work on fetching the songs from icarus and viewing them.
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// 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.endpoint = query.getColumn(2).getString();
|
||||
apiInfo.version = query.getColumn(3).getInt();
|
||||
|
||||
return apiInfo;
|
||||
} catch (std::exception ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
|
||||
return apiInfo;
|
||||
}
|
||||
|
||||
|
||||
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, Endpoint 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, Endpoint, Version) VALUES (?, ?, ?)");
|
||||
|
||||
SQLite::Statement query(db, queryString);
|
||||
query.bind(1, apiInfo.uri);
|
||||
query.bind(2, apiInfo.endpoint);
|
||||
query.bind(3, apiInfo.version);
|
||||
|
||||
query.exec();
|
||||
} catch (std::exception& ex) {
|
||||
auto msg = ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string APIRepository::apiInfoTable() noexcept { return "APIInfo"; }
|
||||
}}
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// 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&);
|
||||
|
||||
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();
|
||||
std::string apiInfoTable() noexcept;
|
||||
};
|
||||
}}
|
||||
|
||||
|
||||
#endif //MEAR_APIREPOSITORY_H
|
||||
@@ -19,6 +19,7 @@ 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
|
||||
SongRepository.cpp
|
||||
@@ -28,7 +29,9 @@ set (SOURCES
|
||||
)
|
||||
|
||||
set (HEADERS
|
||||
APIRepository.h
|
||||
BaseRepository.h
|
||||
model/APIInfo.h
|
||||
model/Song.h
|
||||
model/Token.h
|
||||
model/User.h
|
||||
|
||||
@@ -23,9 +23,11 @@
|
||||
#include <curl/curl.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include "model/APIInfo.h"
|
||||
#include "model/Song.h"
|
||||
#include "model/Token.h"
|
||||
#include "model/User.h"
|
||||
#include "APIRepository.h"
|
||||
#include "SongRepository.h"
|
||||
#include "Tok.h"
|
||||
#include "UserRepository.h"
|
||||
@@ -44,6 +46,7 @@ model::User retrieveCredentials(const std::string&);
|
||||
std::string fetchToken(const std::string&, const std::string&, const std::string&);
|
||||
|
||||
bool doesDatabaseExist(const std::string&);
|
||||
bool apiInformationExist(const std::string&);
|
||||
bool userCredentialExist(const std::string&);
|
||||
|
||||
void saveCredentials(const model::User&, const std::string&);
|
||||
@@ -150,6 +153,13 @@ bool doesDatabaseExist(const std::string& dataPath)
|
||||
return result;
|
||||
}
|
||||
|
||||
bool apiInformationExist(const std::string& dataPath)
|
||||
{
|
||||
repository::local::APIRepository apiRepo;
|
||||
|
||||
return apiRepo.isTableEmpty(dataPath);
|
||||
}
|
||||
|
||||
bool userCredentialExist(const std::string& dataPath)
|
||||
{
|
||||
repository::local::UserRepository userRepo;
|
||||
@@ -381,6 +391,19 @@ Java_com_example_mear_activities_LoginActivity_logUser(
|
||||
}
|
||||
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT jboolean
|
||||
JNICALL
|
||||
Java_com_example_mear_repositories_APIRepository_isAPIInfoTableEmpty(
|
||||
JNIEnv *env,
|
||||
jobject thisObj,
|
||||
jstring pathStr
|
||||
) {
|
||||
const std::string datapath = env->GetStringUTFChars(pathStr, nullptr);
|
||||
|
||||
return apiInformationExist(datapath);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT jboolean
|
||||
JNICALL
|
||||
|
||||
@@ -19,7 +19,6 @@ 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&);
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by brahmix on 10/12/19.
|
||||
//
|
||||
|
||||
#ifndef MEAR_APIINFO_H
|
||||
#define MEAR_APIINFO_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace model {
|
||||
class APIInfo
|
||||
{
|
||||
public:
|
||||
std::string uri;
|
||||
std::string endpoint;
|
||||
int version;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MEAR_APIINFO_H
|
||||
@@ -5,17 +5,17 @@ import android.os.Bundle
|
||||
import android.os.Environment
|
||||
import android.support.design.widget.Snackbar
|
||||
import com.example.mear.R
|
||||
import com.example.mear.models.*
|
||||
|
||||
import kotlinx.android.synthetic.main.activity_login.*
|
||||
import kotlinx.android.synthetic.main.content_login.*
|
||||
import org.jetbrains.anko.toast
|
||||
|
||||
import com.example.mear.models.Song
|
||||
import com.example.mear.models.Token
|
||||
import com.example.mear.models.Track
|
||||
import com.example.mear.models.User
|
||||
import com.example.mear.repositories.TrackRepository
|
||||
import com.example.mear.repositories.UserRepository
|
||||
import com.example.mear.repositories.*
|
||||
import mear.com.example.mear.repositories.APIRepository
|
||||
|
||||
//import com.example.mear.repositories.TrackRepository
|
||||
//import com.example.mear.repositories.UserRepository
|
||||
|
||||
class LoginActivity : BaseServiceActivity() {
|
||||
|
||||
@@ -24,19 +24,7 @@ class LoginActivity : BaseServiceActivity() {
|
||||
setContentView(R.layout.activity_login)
|
||||
setSupportActionBar(toolbar)
|
||||
|
||||
val pa = appDirectory()
|
||||
val usrRepo = UserRepository()
|
||||
if (usrRepo.databaseExist(pa) && !usrRepo.isTableEmpty(pa)) {
|
||||
val usr = usrRepo.retrieveCredentials(pa)
|
||||
username.setText(usr.username)
|
||||
password.setText(usr.password)
|
||||
}
|
||||
|
||||
doBindService()
|
||||
|
||||
demoStream.setOnClickListener {
|
||||
toast("vacant").show()
|
||||
}
|
||||
loadElements()
|
||||
|
||||
login.setOnClickListener {
|
||||
loginButton()
|
||||
@@ -48,6 +36,7 @@ class LoginActivity : BaseServiceActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun loginButton() {
|
||||
if (!validFields()) {
|
||||
toast("Fields are invalid").show()
|
||||
@@ -55,22 +44,27 @@ class LoginActivity : BaseServiceActivity() {
|
||||
}
|
||||
|
||||
val saveCred = saveUserCred.isChecked
|
||||
var apiUriStr = apiUri.text.toString()
|
||||
var apiInfo = APIInfo(apiUri.text.toString(), 1)
|
||||
//var apiUriStr = apiUri.text.toString()
|
||||
val usr = User(username.text.toString(), password.text.toString())
|
||||
|
||||
if (apiUriStr.isEmpty()) {
|
||||
apiUriStr = ""
|
||||
if (apiInfo.uri.isEmpty()) {
|
||||
apiInfo.uri = ""
|
||||
}
|
||||
|
||||
val usrRepo = UserRepository()
|
||||
val trackRepo = TrackRepository()
|
||||
val myToken = usrRepo.fetchToken(usr, apiUriStr)
|
||||
val myToken = usrRepo.fetchToken(usr, apiInfo.uri)
|
||||
|
||||
try {
|
||||
val pa = appDirectory()
|
||||
val so = Song(5)
|
||||
val song = trackRepo.fetchSong(myToken, so, apiUriStr)
|
||||
val song = trackRepo.fetchSong(myToken, so, apiInfo.uri)
|
||||
if (saveCred && usrRepo.isTableEmpty(pa)) {
|
||||
val api = APIRepository()
|
||||
if (api.isTableEmpty(pa)) {
|
||||
api.SaveRecord(apiInfo, pa)
|
||||
}
|
||||
usrRepo.saveCredentials(usr, pa)
|
||||
}
|
||||
//startActivity(Intent(this, IcarusSongActivity::class.java))
|
||||
@@ -80,6 +74,26 @@ class LoginActivity : BaseServiceActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadElements() {
|
||||
val pa = appDirectory()
|
||||
val usrRepo = UserRepository()
|
||||
val apiRepo = APIRepository()
|
||||
if (!usrRepo.databaseExist(pa)) {
|
||||
return
|
||||
}
|
||||
if (!usrRepo.isTableEmpty(pa)) {
|
||||
val usr = usrRepo.retrieveCredentials(pa)
|
||||
username.setText(usr.username)
|
||||
password.setText(usr.password)
|
||||
}
|
||||
if (!apiRepo.isTableEmpty(pa)) {
|
||||
val api = apiRepo.retrieveRecord(pa)
|
||||
val s: String = "${api.uri}" + "${api.version} ${api.endpoint}"
|
||||
apiUri.setText(s)
|
||||
}
|
||||
|
||||
doBindService()
|
||||
}
|
||||
|
||||
private fun appDirectory(): String {
|
||||
return Environment.getDataDirectory().toString() + "/data/" +
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.example.mear.models
|
||||
|
||||
class APIInfo(var uri: String = "", var version: Int = 1,
|
||||
var endpoint: String = "")
|
||||
@@ -0,0 +1,35 @@
|
||||
package mear.com.example.mear.repositories
|
||||
|
||||
import com.example.mear.models.APIInfo
|
||||
import com.example.mear.repositories.BaseRepository
|
||||
|
||||
class APIRepository: BaseRepository() {
|
||||
|
||||
companion object {
|
||||
init {
|
||||
System.loadLibrary("native-lib")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private external fun retrieveAPIInfoRecord(path: String): APIInfo
|
||||
|
||||
private external fun isAPIInfoTableEmpty(path: String): Boolean
|
||||
|
||||
private external fun saveAPIInfoRecord(api: APIInfo, path: String)
|
||||
|
||||
|
||||
fun retrieveRecord(path: String): APIInfo {
|
||||
return retrieveAPIInfoRecord(path)
|
||||
}
|
||||
|
||||
|
||||
fun isTableEmpty(path: String): Boolean {
|
||||
return isAPIInfoTableEmpty(path)
|
||||
}
|
||||
|
||||
|
||||
fun SaveRecord(api: APIInfo, path: String) {
|
||||
return saveAPIInfoRecord(api, path)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user