Added functionality to check if the song already exists when uploading

This commit is contained in:
kdeng00
2019-11-13 20:22:29 -05:00
parent 3b0f199dca
commit ca1fcb801d
5 changed files with 56 additions and 27 deletions
+1 -4
View File
@@ -80,6 +80,7 @@ set(HEADERS
include/type/Scopes.h
include/type/SongChanged.h
include/type/SongFilter.h
include/type/SongUpload.h
include/type/UserFilter.h
include/type/YearFilter.h
include/verify/Initialization.h
@@ -122,9 +123,6 @@ set(JWT_CPP_INCLUDE
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jwt-cpp/include
)
set (ORM_DIR
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/ormpp
)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
@@ -136,7 +134,6 @@ include_directories(include ${CPR_INCLUDE_DIRS} ${TAGLIB} ${TAGLIB_HEADERS} ${BC
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cpr)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/taglib)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/oatpp)
#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/libbcrypt)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/appsettings.json ${CMAKE_BINARY_DIR}/bin/appsettings.json COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/authcredentials.json ${CMAKE_BINARY_DIR}/bin/authcredentials.json COPYONLY)
+11 -1
View File
@@ -27,6 +27,7 @@
#include "model/Models.h"
#include "type/Scopes.h"
#include "type/SongFilter.h"
#include "type/SongUpload.h"
namespace fs = std::filesystem;
@@ -74,7 +75,16 @@ public:
sng.data = std::move(data);
manager::SongManager songMgr(m_bConf);
songMgr.saveSong(sng);
const auto result = songMgr.saveSong(sng);
if (!result.first) {
switch (result.second) {
case type::SongUpload::AlreadyExist:
return createResponse(Status::CODE_400, "Song already exists");
default:
break;
}
}
auto songDto = dto::conversion::DtoConversions::toSongDto(sng);
return createDtoResponse(Status::CODE_200, songDto);
+3 -2
View File
@@ -4,12 +4,14 @@
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <nlohmann/json.hpp>
#include "dto/SongDto.hpp"
#include "model/Models.h"
#include "type/SongChanged.h"
#include "type/SongUpload.h"
namespace manager {
class SongManager
@@ -17,14 +19,13 @@ namespace manager {
public:
SongManager(const model::BinaryPath&);
std::pair<bool, type::SongUpload> saveSong(model::Song&);
bool didSongChange(const model::Song&, const model::Song&);
bool requiresFilesystemChange(const model::Song&, const model::Song&);
bool deleteSong(model::Song&);
bool updateSong(model::Song&);
void saveSong(model::Song&);
static void printSong(const model::Song&);
private:
std::map<type::SongChanged, bool> changesInSong(const model::Song&, const model::Song&);
+14
View File
@@ -0,0 +1,14 @@
#ifndef SONGUPLOAD_H_
#define SONGUPLOAD_H_
namespace type
{
enum class SongUpload
{
Successful = 0,
AlreadyExist = 1,
Failed = 2
};
}
#endif
+27 -20
View File
@@ -23,8 +23,33 @@ namespace fs = std::filesystem;
namespace manager {
SongManager::SongManager(const model::BinaryPath& bConf)
: m_bConf(bConf)
{ }
: m_bConf(bConf) { }
std::pair<bool, type::SongUpload> SongManager::saveSong(model::Song& song)
{
saveSongTemp(song);
utility::MetadataRetriever meta;
auto data = std::move(song.data);
song = meta.retrieveMetadata(song);
song.data = std::move(data);
database::SongRepository songRepo(m_bConf);
if (songRepo.doesSongExist(song, type::SongFilter::titleAndArtist)) {
std::cout << "\ntitle: " << song.title << "\nartist: " << song.artist << "\n";
std::cout << "does not exist\n";
return std::make_pair(false, type::SongUpload::AlreadyExist);
}
saveMisc(song);
printSong(song);
songRepo.saveRecord(song);
song = songRepo.retrieveRecord(song, type::SongFilter::titleAndArtist);
return std::make_pair(true, type::SongUpload::Successful);
}
bool SongManager::didSongChange(const model::Song& updatedSong,
@@ -123,24 +148,6 @@ bool SongManager::updateSong(model::Song& updatedSong)
}
void SongManager::saveSong(model::Song& song)
{
saveSongTemp(song);
utility::MetadataRetriever meta;
auto data = std::move(song.data);
song = meta.retrieveMetadata(song);
song.data = std::move(data);
saveMisc(song);
printSong(song);
database::SongRepository songRepo(m_bConf);
songRepo.saveRecord(song);
song = songRepo.retrieveRecord(song, type::SongFilter::titleAndArtist);
}
void SongManager::printSong(const model::Song& song)
{
std::cout << "\nsong" << std::endl;