Switching to C++ #60

Merged
kdeng00 merged 36 commits from experiment into master 2019-09-03 16:17:20 -04:00
10 changed files with 68 additions and 160 deletions
Showing only changes of commit 1255aa0ff7 - Show all commits
+2
View File
@@ -18,6 +18,8 @@ CREATE TABLE Song (
Genre TEXT NOT NULL, Genre TEXT NOT NULL,
Year INT NOT NULL, Year INT NOT NULL,
Duration INT NOT NULL, Duration INT NOT NULL,
Track INT NOT NULL,
Disc INT NOT NULL,
SongPath TEXT NOT NULL, SongPath TEXT NOT NULL,
CoverArtId INT NOT NULL, CoverArtId INT NOT NULL,
-77
View File
@@ -1,77 +0,0 @@
#ifndef MODELS_H_
#define MODELS_H_
#include <string>
#include <vector>
struct Song
{
int id;
std::string title;
std::string artist;
std::string album;
std::string genre;
int year;
int duration;
std::string songPath;
std::vector<unsigned char> data;
};
struct Cover
{
int id;
std::string songTitle;
std::string imagePath;
// Currently not being used but it should
std::vector<unsigned char> data;
};
struct LoginRes
{
int UserId;
char Username[1024];
char Token[1024];
char TokenType[1024];
char Message[1024];
int Expiration;
};
struct loginResult
{
int user_id;
std::string username;
std::string access_token;
std::string token_type;
std::string message;
int expiration;
};
struct auth_credentials
{
std::string domain;
std::string api_identifier;
std::string client_id;
std::string client_secret;
std::string uri;
std::string endpoint;
};
struct database_connection
{
std::string server;
std::string username;
std::string password;
std::string database;
};
struct TokenReq
{
char ClientId[1024];
char ClientSecret[1024];
char Audience[1024];
char GrantType[1024];
char URI[1024];
char Endpoint[1024];
};
#endif
+2
View File
@@ -13,6 +13,8 @@ struct Song
std::string genre; std::string genre;
int year; int year;
int duration; int duration;
int track;
int disc;
std::string songPath; std::string songPath;
std::vector<unsigned char> data; std::vector<unsigned char> data;
int coverArtId; int coverArtId;
+8
View File
@@ -94,6 +94,8 @@ public:
song->genre = songDb.genre.c_str(); song->genre = songDb.genre.c_str();
song->year = songDb.year; song->year = songDb.year;
song->duration = songDb.duration; song->duration = songDb.duration;
song->track = songDb.track;
song->disc = songDb.disc;
songs->pushBack(song); songs->pushBack(song);
} }
@@ -119,6 +121,8 @@ public:
song->genre = songDb.genre.c_str(); song->genre = songDb.genre.c_str();
song->year = songDb.year; song->year = songDb.year;
song->duration = songDb.duration; song->duration = songDb.duration;
song->track = songDb.track;
song->disc = songDb.disc;
return createDtoResponse(Status::CODE_200, song); return createDtoResponse(Status::CODE_200, song);
} }
@@ -148,6 +152,8 @@ public:
return response; return response;
} }
// TODO: create endpoint for updating songs
ENDPOINT("DELETE", "api/v1/song/data/{id}", songDelete, PATH(Int32, id)) { ENDPOINT("DELETE", "api/v1/song/data/{id}", songDelete, PATH(Int32, id)) {
Song song; Song song;
@@ -159,6 +165,8 @@ public:
return createResponse(Status::CODE_200, "OK"); return createResponse(Status::CODE_200, "OK");
} }
// TODO: create endpoint for streaming songs
#include OATPP_CODEGEN_END(ApiController) #include OATPP_CODEGEN_END(ApiController)
private: private:
std::string m_exe_path; std::string m_exe_path;
+31 -9
View File
@@ -77,11 +77,11 @@ void songRepository::saveRecord(const Song& song)
MYSQL_STMT *stmt = mysql_stmt_init(conn); MYSQL_STMT *stmt = mysql_stmt_init(conn);
std::string query = "INSERT INTO Song(Title, Artist, Album, Genre, "; std::string query = "INSERT INTO Song(Title, Artist, Album, Genre, ";
query.append("Year, Duration, SongPath, CoverArtId) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); query.append("Year, Duration, Track, Disc, SongPath, CoverArtId) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
status = mysql_stmt_prepare(stmt, query.c_str(), query.size()); status = mysql_stmt_prepare(stmt, query.c_str(), query.size());
MYSQL_BIND params[8]; MYSQL_BIND params[10];
memset(params, 0, sizeof(params)); memset(params, 0, sizeof(params));
params[0].buffer_type = MYSQL_TYPE_STRING; params[0].buffer_type = MYSQL_TYPE_STRING;
@@ -118,16 +118,26 @@ void songRepository::saveRecord(const Song& song)
params[5].length = 0; params[5].length = 0;
params[5].is_null = 0; params[5].is_null = 0;
params[6].buffer_type = MYSQL_TYPE_STRING; params[6].buffer_type = MYSQL_TYPE_LONG;
params[6].buffer = (char*)song.songPath.c_str(); params[6].buffer = (char*)&song.track;
auto pathLength = song.songPath.size(); params[6].length = 0;
params[6].length = &pathLength;
params[6].is_null = 0; params[6].is_null = 0;
params[7].buffer_type = MYSQL_TYPE_LONG; params[7].buffer_type = MYSQL_TYPE_LONG;
params[7].buffer = (char*)&song.coverArtId; params[7].buffer = (char*)&song.disc;
params[7].length = 0; params[7].length = 0;
params[7].is_null = 0; params[7].is_null = 0;
params[8].buffer_type = MYSQL_TYPE_STRING;
params[8].buffer = (char*)song.songPath.c_str();
auto pathLength = song.songPath.size();
params[8].length = &pathLength;
params[8].is_null = 0;
params[9].buffer_type = MYSQL_TYPE_LONG;
params[9].buffer = (char*)&song.coverArtId;
params[9].length = 0;
params[9].is_null = 0;
status = mysql_stmt_bind_param(stmt, params); status = mysql_stmt_bind_param(stmt, params);
status = mysql_stmt_execute(stmt); status = mysql_stmt_execute(stmt);
@@ -173,9 +183,15 @@ std::vector<Song> songRepository::parseRecords(MYSQL_RES* results)
song.duration = std::stoi(row[i]); song.duration = std::stoi(row[i]);
break; break;
case 7: case 7:
song.songPath = row[i]; song.track = std::stoi(row[i]);
break; break;
case 8: case 8:
song.disc = std::stoi(row[i]);
break;
case 9:
song.songPath = row[i];
break;
case 10:
song.coverArtId = std::stoi(row[i]); song.coverArtId = std::stoi(row[i]);
break; break;
} }
@@ -218,9 +234,15 @@ Song songRepository::parseRecord(MYSQL_RES* results)
song.duration = std::stoi(row[i]); song.duration = std::stoi(row[i]);
break; break;
case 7: case 7:
song.songPath = row[i]; song.track = std::stoi(row[i]);
break; break;
case 8: case 8:
song.disc = std::stoi(row[i]);
break;
case 9:
song.songPath = row[i];
break;
case 10:
song.coverArtId = std::stoi(row[i]); song.coverArtId = std::stoi(row[i]);
break; break;
} }
+2
View File
@@ -15,6 +15,8 @@ class songDto : public oatpp::data::mapping::type::Object
DTO_FIELD(String, artist); DTO_FIELD(String, artist);
DTO_FIELD(String, album); DTO_FIELD(String, album);
DTO_FIELD(String, genre); DTO_FIELD(String, genre);
DTO_FIELD(Int32, track);
DTO_FIELD(Int32, disc);
DTO_FIELD(Int32, year); DTO_FIELD(Int32, year);
DTO_FIELD(Int32, duration); DTO_FIELD(Int32, duration);
}; };
-28
View File
@@ -1,28 +0,0 @@
#ifndef LOGINHANDLER_H_
#define LOGINHANDLER_H_
#include <memory>
#include "oatpp/network/server/Server.hpp"
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "dto/loginResultDto.hpp"
class loginHandler : public oatpp::web::server::HttpRequestHandler
{
public:
std::shared_ptr<OutgoingResponse> handle(const std::shared_ptr<IncomingRequest>& request) override
{
auto logRes = loginResultDto::createShared();
logRes->access_token = "hahahahahahaha";
logRes->token_type = "Fly";
return ResponseFactory::createResponse(Status::CODE_200, "ddd");
}
private:
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, m_objectMapper);
};
#endif
+1 -43
View File
@@ -39,52 +39,10 @@ void run(const std::string& working_path)
server.run(); server.run();
} }
void test_database()
{
database_connection mysqlD;
mysqlD.server = ""; // where the mysql database is
mysqlD.username = ""; // the root user of mysql
mysqlD.password = ""; // the password of the root user in mysql
mysqlD.database = ""; // the databse to pick
base_repository base;
/**
auto conn = base.setup_mysql_connection(mysqlD);
// assign the results return to the MYSQL_RES pointer
const std::string query = "SELECT *, NULL FROM Song";
auto res = base.perform_mysql_query(conn, query);
auto num_of_fields = mysql_num_fields(res);
printf("MySQL Tables in mysql database:\n");
auto row_count = 1;
for (MYSQL_ROW row = NULL; (row = mysql_fetch_row(res)) != NULL;) {
std::cout << " row " << row_count << std::endl;
for (auto i = 0; i != num_of_fields; ++i) {
auto val = row[i];
if ( val == NULL) {
std::cout << "found null value " << std::endl;
continue;
}
std::cout << val << " ";
}
std::cout << std::endl;
}
// clean up the database result set //
mysql_free_result(res);
// clean up the database link //
mysql_close(conn);
*/
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
oatpp::base::Environment::init(); oatpp::base::Environment::init();
std::string working_path = argv[0]; const std::string working_path = argv[0];
run(working_path); run(working_path);
+4 -3
View File
@@ -41,8 +41,7 @@ void song_manager::saveSong(Song& song)
auto songPath = directory_manager::create_directory_process(song, musicRootPath); auto songPath = directory_manager::create_directory_process(song, musicRootPath);
songPath.append(song.title); songPath.append(song.title);
songPath.append(".mp3"); songPath.append(".mp3");
std::cout << std::endl; std::cout << "\n\ntemp path: " << song.songPath << std::endl;
std::cout << "temp path: " << song.songPath << std::endl;
std::cout << "new path: " << songPath << std::endl; std::cout << "new path: " << songPath << std::endl;
fs::copy(song.songPath, songPath); fs::copy(song.songPath, songPath);
fs::remove(song.songPath); fs::remove(song.songPath);
@@ -84,13 +83,15 @@ void song_manager::deleteSong(Song& song)
void song_manager::printSong(const Song& song) void song_manager::printSong(const Song& song)
{ {
std::cout << std::endl << "song" << std::endl; std::cout << "\n\nsong" << std::endl;
std::cout << "title: " << song.title << std::endl; std::cout << "title: " << song.title << std::endl;
std::cout << "artist: " << song.artist << std::endl; std::cout << "artist: " << song.artist << std::endl;
std::cout << "album: " << song.album << std::endl; std::cout << "album: " << song.album << std::endl;
std::cout << "genre: " << song.genre << std::endl; std::cout << "genre: " << song.genre << std::endl;
std::cout << "duration: " << song.duration << std::endl; std::cout << "duration: " << song.duration << std::endl;
std::cout << "year: " << song.year << std::endl; std::cout << "year: " << song.year << std::endl;
std::cout << "track: " << song.track << std::endl;
std::cout << "disc: " << song.disc << std::endl;
std::cout << "song path: " << song.songPath << std::endl; std::cout << "song path: " << song.songPath << std::endl;
if (song.coverArtId != 0) { if (song.coverArtId != 0) {
std::cout << "cover art id: " << song.coverArtId << std::endl; std::cout << "cover art id: " << song.coverArtId << std::endl;
+18
View File
@@ -1,3 +1,4 @@
#include <algorithm>
#include <iostream> #include <iostream>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
@@ -25,9 +26,24 @@ Song metadata_retriever::retrieve_metadata(std::string& song_path)
song.album = file.tag()->album().toCString(); song.album = file.tag()->album().toCString();
song.genre = file.tag()->genre().toCString(); song.genre = file.tag()->genre().toCString();
song.year = file.tag()->year(); song.year = file.tag()->year();
song.track = file.tag()->track();
song.duration = file.audioProperties()->lengthInSeconds(); song.duration = file.audioProperties()->lengthInSeconds();
song.songPath = song_path; song.songPath = song_path;
// TODO: Move over to this eventually since at the moment
// I am only targetting mp3 files
// Used to retrieve disc
TagLib::MPEG::File sameFile(song_path.c_str());
auto tag = sameFile.ID3v2Tag();
auto frame = tag->frameList("TPOS");
if (frame.isEmpty()) {
song.disc = 1;
// TODO: set default disc to 1 if none found
} else {
song.disc = std::stoi(frame.front()->toString().toCString());
}
return song; return song;
} }
@@ -99,5 +115,7 @@ void metadata_retriever::update_metadata(Song sng_updated, const Song sng_old)
file.tag()->setYear(sng_updated.year); file.tag()->setYear(sng_updated.year);
} }
// TODO: functionality to update the track number and disc number
file.save(); file.save();
} }