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:
+4
-2
@@ -11,7 +11,7 @@ set(SOURCES
|
|||||||
src/controller/songController.hpp
|
src/controller/songController.hpp
|
||||||
src/database/base_repository.cpp
|
src/database/base_repository.cpp
|
||||||
src/database/coverArtRepository.cpp
|
src/database/coverArtRepository.cpp
|
||||||
#src/database/songRepository.cpp
|
src/database/songRepository.cpp
|
||||||
src/dto/loginResultDto.hpp
|
src/dto/loginResultDto.hpp
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/managers/coverArtManager.cpp
|
src/managers/coverArtManager.cpp
|
||||||
@@ -24,7 +24,7 @@ set(SOURCES
|
|||||||
set(HEADERS
|
set(HEADERS
|
||||||
include/database/base_repository.h
|
include/database/base_repository.h
|
||||||
include/database/coverArtRepository.h
|
include/database/coverArtRepository.h
|
||||||
#include/database/songRepository.h
|
include/database/songRepository.h
|
||||||
include/managers/coverArtManager.h
|
include/managers/coverArtManager.h
|
||||||
include/managers/directory_manager.h
|
include/managers/directory_manager.h
|
||||||
include/managers/song_manager.h
|
include/managers/song_manager.h
|
||||||
@@ -32,7 +32,9 @@ set(HEADERS
|
|||||||
include/models/models.h
|
include/models/models.h
|
||||||
include/utilities/imageFile.h
|
include/utilities/imageFile.h
|
||||||
include/utilities/metadata_retriever.h
|
include/utilities/metadata_retriever.h
|
||||||
|
include/types/coverFilter.h
|
||||||
include/types/scopes.h
|
include/types/scopes.h
|
||||||
|
include/types/songFilter.h
|
||||||
)
|
)
|
||||||
|
|
||||||
set (TAGLIB
|
set (TAGLIB
|
||||||
|
|||||||
@@ -5,14 +5,18 @@
|
|||||||
|
|
||||||
#include "database/base_repository.h"
|
#include "database/base_repository.h"
|
||||||
#include "models/models.h"
|
#include "models/models.h"
|
||||||
|
#include "types/coverFilter.h"
|
||||||
|
|
||||||
class coverArtRepository : public base_repository
|
class coverArtRepository : public base_repository
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
coverArtRepository(const std::string&);
|
coverArtRepository(const std::string&);
|
||||||
|
|
||||||
|
Cover retrieveRecord(Cover&, coverFilter);
|
||||||
|
|
||||||
void saveRecord(const Cover&);
|
void saveRecord(const Cover&);
|
||||||
private:
|
private:
|
||||||
|
Cover parseRecord(MYSQL_RES*);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,14 +1,25 @@
|
|||||||
#ifndef SONGREPOSITORY_H_
|
#ifndef SONGREPOSITORY_H_
|
||||||
#define SONGREPOSITORY_H_
|
#define SONGREPOSITORY_H_
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <mysql/mysql.h>
|
#include <mysql/mysql.h>
|
||||||
|
|
||||||
|
#include "database/base_repository.h"
|
||||||
#include "models/models.h"
|
#include "models/models.h"
|
||||||
|
#include "types/songFilter.h"
|
||||||
|
|
||||||
class songRespository
|
class songRepository : public base_repository
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
songRepository(const std::string&);
|
||||||
|
|
||||||
|
Song retrieveRecord(Song&, songFilter);
|
||||||
|
|
||||||
|
void saveRecord(const Song&);
|
||||||
private:
|
private:
|
||||||
|
std::vector<Song> parseRecords(MYSQL_RES*);
|
||||||
|
Song parseRecord(MYSQL_RES*);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ struct Song
|
|||||||
int duration;
|
int duration;
|
||||||
std::string songPath;
|
std::string songPath;
|
||||||
std::vector<unsigned char> data;
|
std::vector<unsigned char> data;
|
||||||
|
int coverArtId;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Cover
|
struct Cover
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef COVERFILTER_H_
|
||||||
|
#define COVERFILTER_H_
|
||||||
|
|
||||||
|
enum class coverFilter
|
||||||
|
{
|
||||||
|
id = 0,
|
||||||
|
songTitle,
|
||||||
|
imagePath
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef SONGFILTER_H_
|
||||||
|
#define SONGFILTER_H_
|
||||||
|
|
||||||
|
enum class songFilter
|
||||||
|
{
|
||||||
|
id = 0,
|
||||||
|
title,
|
||||||
|
album,
|
||||||
|
artist,
|
||||||
|
genre,
|
||||||
|
year
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "database/base_repository.h"
|
#include "database/base_repository.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
#include "managers/directory_manager.h"
|
#include "managers/directory_manager.h"
|
||||||
@@ -16,8 +18,10 @@ MYSQL* base_repository::setup_mysql_connection()
|
|||||||
{
|
{
|
||||||
MYSQL *conn = mysql_init(nullptr);
|
MYSQL *conn = mysql_init(nullptr);
|
||||||
|
|
||||||
auto res = mysql_real_connect(conn, details.server.c_str(), details.username.c_str(),
|
if (!mysql_real_connect(conn, details.server.c_str(), details.username.c_str(),
|
||||||
details.password.c_str(), details.database.c_str(), 0, nullptr, 0);
|
details.password.c_str(), details.database.c_str(), 0, nullptr, 0)) {
|
||||||
|
std::cout << "connection error" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
@@ -48,8 +52,9 @@ MYSQL_RES* base_repository::perform_mysql_query(MYSQL *conn, const std::string&
|
|||||||
void base_repository::intitalizeDetails()
|
void base_repository::intitalizeDetails()
|
||||||
{
|
{
|
||||||
auto databaseConfig = directory_manager::databaseConfigContent(path);
|
auto databaseConfig = directory_manager::databaseConfigContent(path);
|
||||||
details.database = databaseConfig.get<std::string>();
|
|
||||||
details.password = databaseConfig.get<std::string>();
|
details.database = databaseConfig["database"].get<std::string>();
|
||||||
details.server = databaseConfig.get<std::string>();
|
details.password = databaseConfig["password"].get<std::string>();
|
||||||
details.username = databaseConfig.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 "database/coverArtRepository.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <sstream>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
coverArtRepository::coverArtRepository(const std::string& path) : base_repository(path)
|
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)
|
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 "managers/coverArtManager.h"
|
||||||
|
|
||||||
#include "database/coverArtRepository.h"
|
#include "database/coverArtRepository.h"
|
||||||
|
#include "types/coverFilter.h"
|
||||||
#include "utilities/metadata_retriever.h"
|
#include "utilities/metadata_retriever.h"
|
||||||
|
|
||||||
coverArtManager::coverArtManager(const std::string& configPath) : path(configPath)
|
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;
|
cov.songTitle = song.title;
|
||||||
|
|
||||||
coverArtRepository covRepo(path);
|
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;
|
return cov;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
#include "database/songRepository.h"
|
||||||
#include "managers/coverArtManager.h"
|
#include "managers/coverArtManager.h"
|
||||||
#include "managers/directory_manager.h"
|
#include "managers/directory_manager.h"
|
||||||
#include "utilities/metadata_retriever.h"
|
#include "utilities/metadata_retriever.h"
|
||||||
@@ -34,6 +35,10 @@ void song_manager::saveSong(Song& song)
|
|||||||
stockCoverPath.append("/CoverArt.png");
|
stockCoverPath.append("/CoverArt.png");
|
||||||
|
|
||||||
auto cov = covMgr.saveCover(song, coverRootPath, stockCoverPath);
|
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)
|
void song_manager::printSong(const Song& song)
|
||||||
|
|||||||
Reference in New Issue
Block a user