Left off at adding album record to the database and retrieving the album record to assign the album id to the song record's album id foreign key

This commit is contained in:
kdeng00
2019-09-01 18:46:02 -04:00
parent ab3000c2a0
commit fafb39326b
17 changed files with 320 additions and 59 deletions
-34
View File
@@ -1,34 +0,0 @@
#ifndef APPCOMPONENT_H_
#define APPCOMPONENT_H_
#include <memory>
#include "oatpp/core/macro/component.hpp"
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
class appComponent
{
public:
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
return oatpp::network::server::SimpleTCPConnectionProvider::createShared(5002);
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
return oatpp::web::server::HttpRouter::createShared();
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, serverConnectionHandler)([] {
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
return oatpp::web::server::HttpConnectionHandler::createShared(router);
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
return oatpp::parser::json::mapping::ObjectMapper::createShared();
}());
private:
};
#endif
+117
View File
@@ -1,5 +1,122 @@
#include "database/albumRepository.h"
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <cstring>
Database::albumRepository::albumRepository(const Model::BinaryPath& bConf)
: base_repository(bConf)
{ }
// TODO: implement this later on
std::vector<Model::Album> Database::albumRepository::retrieveRecords()
{
std::vector<Model::Album> albums;
return albums;
}
Model::Album Database::albumRepository::retrieveRecord(Model::Album& album, Type::albumFilter filter)
{
std::stringstream qry;
auto conn = setup_mysql_connection();
qry << "SELECT alb.* FROM Album alb WHERE ";
std::unique_ptr<char*> param;
switch (filter) {
case Type::albumFilter::id:
qry << "alb.AlbumId = " << album.id;
break;
case Type::albumFilter::title:
param = std::make_unique<char*>(new char[album.title.size()]);
mysql_real_escape_string(conn, *param, album.title.c_str(), album.title.size());
qry << "alb.Title = '" << *param << "'";
break;
case Type::albumFilter::year:
break;
default:
break;
}
const std::string query = qry.str();
auto results = perform_mysql_query(conn, query);
album = parseRecord(results);
mysql_close(conn);
std::cout << "done" << std::endl;
return album;
}
void Database::albumRepository::saveAlbum(const Model::Album& album)
{
std::cout << "beginning to insert album record" << std::endl;
auto conn = setup_mysql_connection();
auto status = 0;
MYSQL_STMT *stmt = mysql_stmt_init(conn);
std::string query = "INSERT INTO Album(Title, Year) VALUES(?, ?)";
status = mysql_stmt_prepare(stmt, query.c_str(), query.size());
MYSQL_BIND params[2];
memset(params, 0, sizeof(params));
params[0].buffer_type = MYSQL_TYPE_STRING;
params[0].buffer = (char*)album.title.c_str();
auto titleLength = album.title.size();
params[0].buffer = &titleLength;
params[0].is_null = 0;
params[1].buffer_type = MYSQL_TYPE_LONG;
params[1].buffer = (char*)&album.year;
params[1].length = 0;
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 inserting album record" << std::endl;
}
// TODO: implement this
std::vector<Model::Album> Database::albumRepository::parseRecords(MYSQL_RES* results)
{
std::vector<Model::Album> albums;
return albums;
}
Model::Album Database::albumRepository::parseRecord(MYSQL_RES* results)
{
Model::Album album;
auto fieldNum = mysql_num_fields(results);
auto row = mysql_fetch_row(results);
for (auto i = 0; i != fieldNum; ++i) {
const std::string field(mysql_fetch_field(results)->name);
if (field.compare("AlbumId") == 0) {
album.id = std::stoi(row[i]);
}
if (field.compare("Title") == 0) {
album.title = row[i];
}
if (field.compare("Year") == 0) {
album.year = std::stoi(row[i]);
}
}
return album;
}
+1
View File
@@ -74,6 +74,7 @@ void Database::songRepository::deleteRecord(const Model::Song& song)
void Database::songRepository::saveRecord(const Model::Song& song)
{
std::cout << "beginning to insert song record" << std::endl;
auto conn = setup_mysql_connection();
auto status = 0;
+3 -6
View File
@@ -9,7 +9,7 @@
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "appComponent.hpp"
#include "component/appComponent.hpp"
#include "controller/loginController.hpp"
#include "controller/songController.hpp"
#include "database/base_repository.h"
@@ -20,7 +20,7 @@ namespace fs = std::filesystem;
//void run(const std::string& working_path)
void run(const Model::BinaryPath& bConf)
{
appComponent component;
Component::appComponent component;
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
@@ -43,11 +43,8 @@ void run(const Model::BinaryPath& bConf)
int main(int argc, char **argv)
{
oatpp::base::Environment::init();
//const std::string working_path = argv[0];
Model::BinaryPath bConf;
bConf.path = argv[0];
Model::BinaryPath bConf(argv[0]);
//run(working_path);
run(bConf);
oatpp::base::Environment::destroy();
+14
View File
@@ -1,5 +1,19 @@
#include "managers/albumManager.h"
#include "database/albumRepository.h"
#include "models/models.h"
Manager::albumManager::albumManager(const Model::BinaryPath& bConf)
: m_bConf(bConf)
{ }
void Manager::albumManager::saveAlbum(const Model::Song& song)
{
Model::Album album;
album.title = song.album;
album.year = song.year;
Database::albumRepository albRepo(m_bConf);
albRepo.saveAlbum(album);
}
+32 -17
View File
@@ -8,8 +8,12 @@
#include "database/coverArtRepository.h"
#include "database/songRepository.h"
#include "managers/albumManager.h"
#include "managers/artistManager.h"
#include "managers/coverArtManager.h"
#include "managers/directory_manager.h"
#include "managers/genreManager.h"
#include "managers/yearManager.h"
#include "utilities/metadata_retriever.h"
namespace fs = std::filesystem;
@@ -31,25 +35,9 @@ void Manager::song_manager::saveSong(Model::Song& song)
song = meta.retrieve_metadata(song.songPath);
song.data = std::move(data);
coverArtManager covMgr(m_bConf);
auto pathConfigContent = Manager::directory_manager::pathConfigContent(m_bConf);
auto coverRootPath = pathConfigContent["cover_root_path"].get<std::string>();
auto musicRootPath = pathConfigContent["root_music_path"].get<std::string>();
auto stockCoverPath = Manager::directory_manager::configPath(m_bConf);
stockCoverPath.append("/CoverArt.png");
saveMisc(song);
auto cov = covMgr.saveCover(song, coverRootPath, stockCoverPath);
song.coverArtId = cov.id;
auto songPath = Manager::directory_manager::create_directory_process(song, musicRootPath);
songPath.append(song.title);
songPath.append(".mp3");
std::cout << "\n\ntemp path: " << song.songPath << std::endl;
std::cout << "new path: " << songPath << std::endl;
fs::copy(song.songPath, songPath);
fs::remove(song.songPath);
song.songPath = std::move(songPath);
printSong(song);
@@ -120,3 +108,30 @@ void Manager::song_manager::saveSongTemp(Model::Song& song)
song.songPath = tmp_song;
}
void Manager::song_manager::saveMisc(Model::Song& song)
{
coverArtManager covMgr(m_bConf);
auto pathConfigContent = Manager::directory_manager::pathConfigContent(m_bConf);
auto coverRootPath = pathConfigContent["cover_root_path"].get<std::string>();
auto musicRootPath = pathConfigContent["root_music_path"].get<std::string>();
auto stockCoverPath = Manager::directory_manager::configPath(m_bConf);
stockCoverPath.append("/CoverArt.png");
auto cov = covMgr.saveCover(song, coverRootPath, stockCoverPath);
song.coverArtId = cov.id;
auto songPath = Manager::directory_manager::create_directory_process(song, musicRootPath);
songPath.append(song.title);
songPath.append(".mp3");
std::cout << "\n\ntemp path: " << song.songPath << std::endl;
std::cout << "new path: " << songPath << std::endl;
fs::copy(song.songPath, songPath);
fs::remove(song.songPath);
song.songPath = std::move(songPath);
artistManager artMgr(m_bConf);
albumManager albMgr(m_bConf);
genreManager gnrMgr(m_bConf);
yearManager yrMgr(m_bConf);
}