diff --git a/CMakeLists.txt b/CMakeLists.txt index d604782..d22d96e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ set(SOURCES ) set(HEADERS include/component/AppComponent.hpp + include/controller/AlbumController.hpp include/controller/CoverArtController.hpp include/controller/LoginController.hpp include/controller/SongController.hpp @@ -37,9 +38,13 @@ set(HEADERS include/database/GenreRepository.h include/database/SongRepository.h include/database/YearRepository.h + include/dto/AlbumDto.hpp + include/dto/ArtistDto.hpp include/dto/CoverArtDto.hpp + include/dto/GenreDto.hpp include/dto/LoginResultDto.hpp include/dto/SongDto.hpp + include/dto/YearDto.hpp include/manager/AlbumManager.h include/manager/ArtistManager.h include/manager/CoverArtManager.h diff --git a/include/controller/AlbumController.hpp b/include/controller/AlbumController.hpp new file mode 100644 index 0000000..892d028 --- /dev/null +++ b/include/controller/AlbumController.hpp @@ -0,0 +1,92 @@ +#ifndef COVERARTCONTROLLER_H_ +#define COVERARTCONTROLLER_H_ + +#include +#include +#include +#include +#include +#include +#include + +#include "oatpp/core/data/stream/ChunkedBuffer.hpp" +#include "oatpp/core/data/stream/FileStream.hpp" +#include "oatpp/core/macro/codegen.hpp" +#include "oatpp/core/macro/component.hpp" +#include "oatpp/web/mime/multipart/InMemoryPartReader.hpp" +#include "oatpp/web/mime/multipart/Reader.hpp" +#include "oatpp/web/server/api/ApiController.hpp" + +#include "database/AlbumRepository.h" +#include "dto/AlbumDto.hpp" +#include "manager/AlbumManager.h" +#include "model/Models.h" +#include "type/Scopes.h" +#include "type/AlbumFilter.h" + +namespace fs = std::filesystem; + +namespace controller +{ + class AlbumController : public oatpp::web::server::api::ApiController + { + public: + AlbumController(std::string p, OATPP_COMPONENT(std::shared_ptr, objectMapper)) + : oatpp::web::server::api::ApiController(objectMapper), m_exe_path(p) + { } + + AlbumController(const model::BinaryPath& bConf, OATPP_COMPONENT(std::shared_ptr, objectMapper)) + : oatpp::web::server::api::ApiController(objectMapper), m_bConf(bConf) + { } + + #include OATPP_CODEGEN_BEGIN(ApiController) + + // endpoint for retrieving all album records in json format + ENDPOINT("GET", "/api/v1/album", albumRecords, + REQUEST(std::shared_ptr, request)) + { + std::cout << "starting process of retrieving album" << std::endl; + database::AlbumRepository albRepo(m_bConf); + auto albsDb = albRepo.retrieveRecords(); + auto albums = oatpp::data::mapping::type::List::createShared(); + + for (auto& albDb : albsDb) { + auto alb = dto::AlbumDto::createShared(); + alb->id = albDb.id; + alb->title = albDb.songTitle.c_str(); + alb->year = albDb.year; + + albums->pushBack(alb); + } + + return createDtoResponse(Status::CODE_200, albums); + } + + // endpoint for retrieving single album record by the album id in json format + ENDPOINT("GET", "/api/v1/album/{id}", albumRecord, + PATH(Int32, id)) { + + database::AlbumRepository albRepo(m_bConf); + model::Album albDb(id); + + OATPP_ASSERT_HTTP(albRepo.doesAlbumExist(albDb, type::AlbumFilter::id) , Status::CODE_403, "album does not exist"); + + std::cout << "album exists" << std::endl; + albDb = albRepo.retrieveRecord(albDb, type::AlbumFilter::id); + + auto album = dto::AlbumDto::createShared(); + album->id = albDb.id; + album->title = albDb.songTitle.c_str(); + album->year = albDb.year; + + return createDtoResponse(Status::CODE_200, album); + } + + #include OATPP_CODEGEN_END(ApiController) + private: + std::string m_exe_path; + model::BinaryPath m_bConf; + const long m_dataSize = std::numeric_limits::max(); + }; +} +#endif diff --git a/include/controller/SongController.hpp b/include/controller/SongController.hpp index 80fa93f..3e94d8b 100644 --- a/include/controller/SongController.hpp +++ b/include/controller/SongController.hpp @@ -114,8 +114,7 @@ namespace controller PATH(Int32, id)) { database::SongRepository songRepo(m_bConf); - model::Song songDb; - songDb.id = id; + model::Song songDb(id); OATPP_ASSERT_HTTP(songRepo.doesSongExist(songDb, type::SongFilter::id) , Status::CODE_403, "song does not exist"); @@ -140,8 +139,7 @@ namespace controller PATH(Int32, id)) { database::SongRepository songRepo(m_bConf); - model::Song songDb; - songDb.id = id; + model::Song songDb(id); songDb = songRepo.retrieveRecord(songDb, type::SongFilter::id); std::ifstream fl(songDb.songPath.c_str(), std::ios::in | std::ios::binary | std::ios::ate); @@ -153,7 +151,7 @@ namespace controller std::ostreambuf_iterator(buf)); fl.close(); - // TODO: no need to make this shared + // TODO: no need to make this shared. Why? Because it already is. Sharing shared? Like wetting water auto rawSong = std::make_shared(oatpp::String(buf.str().data(), (v_int32)buf.str().size(), true)); auto response = createResponse(Status::CODE_200, *rawSong); diff --git a/include/dto/AlbumDto.hpp b/include/dto/AlbumDto.hpp new file mode 100644 index 0000000..56e6b21 --- /dev/null +++ b/include/dto/AlbumDto.hpp @@ -0,0 +1,23 @@ +#ifndef ALBUMDTO_H_ +#define ALBUMDTO_H_ + +#include "oatpp/core/data/mapping/type/Object.hpp" +#include "oatpp/core/macro/codegen.hpp" + +namespace dto +{ + #include OATPP_CODEGEN_BEGIN(DTO) + + class AlbumDto : public oatpp::data::mapping::type::Object + { + DTO_INIT(AlbumDto, Object) + + DTO_FIELD(Int32, id); + DTO_FIELD(String, title); + DTO_FIELD(Int32, year); + }; + + #include OATPP_CODEGEN_END(DTO) +} + +#endif diff --git a/include/dto/ArtistDto.hpp b/include/dto/ArtistDto.hpp new file mode 100644 index 0000000..08f9392 --- /dev/null +++ b/include/dto/ArtistDto.hpp @@ -0,0 +1,22 @@ +#ifndef ARTISTDTO_H_ +#define ARTISTDTO_H_ + +#include "oatpp/core/data/mapping/type/Object.hpp" +#include "oatpp/core/macro/codegen.hpp" + +namespace dto +{ + #include OATPP_CODEGEN_BEGIN(DTO) + + class ArtistDto : public oatpp::data::mapping::type::Object + { + DTO_INIT(ArtistDto, Object) + + DTO_FIELD(Int32, id); + DTO_FIELD(String, artist); + }; + + #include OATPP_CODEGEN_END(DTO) +} + +#endif diff --git a/include/dto/GenreDto.hpp b/include/dto/GenreDto.hpp new file mode 100644 index 0000000..b7235a8 --- /dev/null +++ b/include/dto/GenreDto.hpp @@ -0,0 +1,22 @@ +#ifndef GENREDTO_H_ +#define GENREDTO_H_ + +#include "oatpp/core/data/mapping/type/Object.hpp" +#include "oatpp/core/macro/codegen.hpp" + +namespace dto +{ + #include OATPP_CODEGEN_BEGIN(DTO) + + class GenreDto : public oatpp::data::mapping::type::Object + { + DTO_INIT(GenreDto, Object) + + DTO_FIELD(Int32, id); + DTO_FIELD(String, category); + }; + + #include OATPP_CODEGEN_END(DTO) +} + +#endif diff --git a/include/dto/YearDto.hpp b/include/dto/YearDto.hpp new file mode 100644 index 0000000..1cb102a --- /dev/null +++ b/include/dto/YearDto.hpp @@ -0,0 +1,22 @@ +#ifndef YEARDTO_H_ +#define YEARDTO_H_ + +#include "oatpp/core/data/mapping/type/Object.hpp" +#include "oatpp/core/macro/codegen.hpp" + +namespace dto +{ + #include OATPP_CODEGEN_BEGIN(DTO) + + class YearDto : public oatpp::data::mapping::type::Object + { + DTO_INIT(YearDto, Object) + + DTO_FIELD(Int32, id); + DTO_FIELD(Int32, year); + }; + + #include OATPP_CODEGEN_END(DTO) +} + +#endif diff --git a/include/model/Models.h b/include/model/Models.h index 3b1486b..b3f78bb 100644 --- a/include/model/Models.h +++ b/include/model/Models.h @@ -8,7 +8,10 @@ namespace model { struct Song { - int id; + Song() = default; + Song(const int id) : id(id) { } + + int id; std::string title; std::string artist; std::string album; @@ -34,6 +37,9 @@ namespace model struct Album { + Album() = default; + Album(const int id) : id(id) { } + int id; std::string title; int year; @@ -55,6 +61,9 @@ namespace model struct Cover { + Cover() = default; + Cover(const int id) : id(id) { } + int id; std::string songTitle; std::string imagePath; @@ -96,6 +105,7 @@ namespace model BinaryPath(const char *p) : path(std::move(p)) { } BinaryPath(std::string& p) : path(std::move(p)) { } BinaryPath(const std::string& p) : path(std::move(p)) { } + std::string path; }; }