Cover art record saved in the database. Next is to save the song record to the database and then try to download the song afterwards
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "database/base_repository.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "managers/directory_manager.h"
|
||||
@@ -16,8 +18,10 @@ MYSQL* base_repository::setup_mysql_connection()
|
||||
{
|
||||
MYSQL *conn = mysql_init(nullptr);
|
||||
|
||||
auto res = mysql_real_connect(conn, details.server.c_str(), details.username.c_str(),
|
||||
details.password.c_str(), details.database.c_str(), 0, nullptr, 0);
|
||||
if (!mysql_real_connect(conn, details.server.c_str(), details.username.c_str(),
|
||||
details.password.c_str(), details.database.c_str(), 0, nullptr, 0)) {
|
||||
std::cout << "connection error" << std::endl;
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
@@ -48,8 +52,9 @@ MYSQL_RES* base_repository::perform_mysql_query(MYSQL *conn, const std::string&
|
||||
void base_repository::intitalizeDetails()
|
||||
{
|
||||
auto databaseConfig = directory_manager::databaseConfigContent(path);
|
||||
details.database = databaseConfig.get<std::string>();
|
||||
details.password = databaseConfig.get<std::string>();
|
||||
details.server = databaseConfig.get<std::string>();
|
||||
details.username = databaseConfig.get<std::string>();
|
||||
|
||||
details.database = databaseConfig["database"].get<std::string>();
|
||||
details.password = databaseConfig["password"].get<std::string>();
|
||||
details.server = databaseConfig["server"].get<std::string>();
|
||||
details.username = databaseConfig["username"].get<std::string>();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,112 @@
|
||||
#include "database/coverArtRepository.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
|
||||
coverArtRepository::coverArtRepository(const std::string& path) : base_repository(path)
|
||||
{ }
|
||||
|
||||
Cover coverArtRepository::retrieveRecord(Cover& cov, coverFilter filter = coverFilter::id)
|
||||
{
|
||||
std::stringstream qry;
|
||||
auto conn = setup_mysql_connection();
|
||||
qry << "SELECT * FROM CoverArt WHERE ";
|
||||
|
||||
std::unique_ptr<char*> param;
|
||||
switch (filter) {
|
||||
case coverFilter::id:
|
||||
qry << "CoverArtId = " << cov.id;
|
||||
break;
|
||||
case coverFilter::songTitle:
|
||||
param = std::make_unique<char*>(new char[cov.songTitle.size()]);
|
||||
mysql_real_escape_string(conn, *param, cov.songTitle.c_str(), cov.songTitle.size());
|
||||
std::cout << *param << std::endl;
|
||||
qry << "SongTitle = '" << *param << "'";
|
||||
break;
|
||||
case coverFilter::imagePath:
|
||||
param = std::make_unique<char*>(new char[cov.imagePath.size()]);
|
||||
mysql_real_escape_string(conn, *param, cov.imagePath.c_str(), cov.imagePath.size());
|
||||
std::cout << *param << std::endl;
|
||||
qry << "ImagePath = '" << *param << "'";
|
||||
break;
|
||||
}
|
||||
|
||||
const std::string query = qry.str();
|
||||
std::cout << query << std::endl;
|
||||
auto results = perform_mysql_query(conn, query);
|
||||
std::cout << "the query has been performed" << std::endl;
|
||||
|
||||
auto covDb = parseRecord(results);
|
||||
|
||||
mysql_close(conn);
|
||||
std::cout << "done" << std::endl;
|
||||
|
||||
return covDb;
|
||||
}
|
||||
|
||||
|
||||
void coverArtRepository::saveRecord(const Cover& cov)
|
||||
{
|
||||
auto conn = setup_mysql_connection();
|
||||
|
||||
MYSQL_STMT *stmt;
|
||||
MYSQL_BIND params[2];
|
||||
my_bool isNull;
|
||||
int status;
|
||||
|
||||
stmt = mysql_stmt_init(conn);
|
||||
|
||||
const std::string query = "INSERT INTO CoverArt(SongTitle, ImagePath) VALUES(?, ?)";
|
||||
|
||||
status = mysql_stmt_prepare(stmt, query.c_str(), query.size());
|
||||
|
||||
memset(params, 0, sizeof(params));
|
||||
|
||||
params[0].buffer_type = MYSQL_TYPE_STRING;
|
||||
params[0].buffer = (char*)cov.songTitle.c_str();
|
||||
auto songTitleLength = cov.songTitle.size();
|
||||
params[0].length = &songTitleLength;
|
||||
params[0].is_null = 0;
|
||||
|
||||
params[1].buffer_type = MYSQL_TYPE_STRING;
|
||||
params[1].buffer = (char*)cov.imagePath.c_str();
|
||||
auto imagePathLength = cov.imagePath.size();
|
||||
params[1].length = &imagePathLength;
|
||||
params[1].is_null = 0;
|
||||
|
||||
status = mysql_stmt_bind_param(stmt, params);
|
||||
status = mysql_stmt_execute(stmt);
|
||||
|
||||
mysql_stmt_close(stmt);
|
||||
mysql_close(conn);
|
||||
|
||||
std::cout << "done" << std::endl;
|
||||
}
|
||||
|
||||
Cover coverArtRepository::parseRecord(MYSQL_RES *results)
|
||||
{
|
||||
std::cout << "parsing record" << std::endl;
|
||||
Cover cov;
|
||||
auto fieldNum = mysql_num_fields(results);
|
||||
|
||||
MYSQL_ROW row = mysql_fetch_row(results);
|
||||
|
||||
for (auto i = 0; i != fieldNum; ++i) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
cov.id = std::stoi(row[i]);
|
||||
break;
|
||||
case 1:
|
||||
cov.songTitle = row[i];
|
||||
break;
|
||||
case 2:
|
||||
cov.imagePath = row[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::cout << "done parsing record" << std::endl;
|
||||
|
||||
return cov;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "database/songRepository.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
|
||||
songRepository::songRepository(const std::string& path) : base_repository(path)
|
||||
{ }
|
||||
|
||||
Song songRepository::retrieveRecord(Song& song, songFilter)
|
||||
{
|
||||
|
||||
return song;
|
||||
}
|
||||
|
||||
void songRepository::saveRecord(const Song& song)
|
||||
{
|
||||
// TODO: pick up here. Save the song record to the database
|
||||
}
|
||||
|
||||
std::vector<Song> songRepository::parseRecords(MYSQL_RES* results)
|
||||
{
|
||||
std::vector<Song> songs;
|
||||
|
||||
|
||||
return songs;
|
||||
}
|
||||
|
||||
Song songRepository::parseRecord(MYSQL_RES* results)
|
||||
{
|
||||
Song song;
|
||||
|
||||
return song;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "managers/coverArtManager.h"
|
||||
|
||||
#include "database/coverArtRepository.h"
|
||||
#include "types/coverFilter.h"
|
||||
#include "utilities/metadata_retriever.h"
|
||||
|
||||
coverArtManager::coverArtManager(const std::string& configPath) : path(configPath)
|
||||
@@ -16,6 +17,10 @@ Cover coverArtManager::saveCover(const Song& song, std::string& rootPath, const
|
||||
cov.songTitle = song.title;
|
||||
|
||||
coverArtRepository covRepo(path);
|
||||
std::cout << "saving record to the database" << std::endl;
|
||||
covRepo.saveRecord(cov);
|
||||
std::cout << "retrieving record from database" << std::endl;
|
||||
cov = covRepo.retrieveRecord(cov, coverFilter::songTitle);
|
||||
|
||||
return cov;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "database/songRepository.h"
|
||||
#include "managers/coverArtManager.h"
|
||||
#include "managers/directory_manager.h"
|
||||
#include "utilities/metadata_retriever.h"
|
||||
@@ -34,6 +35,10 @@ void song_manager::saveSong(Song& song)
|
||||
stockCoverPath.append("/CoverArt.png");
|
||||
|
||||
auto cov = covMgr.saveCover(song, coverRootPath, stockCoverPath);
|
||||
song.coverArtId = cov.id;
|
||||
|
||||
songRepository songRepo(exe_path);
|
||||
songRepo.saveRecord(song);
|
||||
}
|
||||
|
||||
void song_manager::printSong(const Song& song)
|
||||
|
||||
Reference in New Issue
Block a user