diff --git a/code/src/Factory.php b/code/src/Factory.php index a76caea..871835d 100644 --- a/code/src/Factory.php +++ b/code/src/Factory.php @@ -32,7 +32,11 @@ public function createSongReader(): SongReader } public function createSongWriter(): SongWriter { - return new SongWriter(); + return new SongWriter( + new MySQLSongWriter( + $this->mySqlConnector->getConnection() + ) + ); } public function createArtistHandler(): ArtistHandler diff --git a/code/src/Handler/SongWriter.php b/code/src/Handler/SongWriter.php index 1f52a15..3e75d38 100644 --- a/code/src/Handler/SongWriter.php +++ b/code/src/Handler/SongWriter.php @@ -9,6 +9,7 @@ class SongWriter { public function __construct( + private MySQLSongWriter $mySQLSongWriter ) { } @@ -17,11 +18,31 @@ public function __construct( */ public function handle(Request $request, Response $response): Response { + $data = json_decode($request->getBody(), true); + + if ($data == null){ return ApiResponse::errorMissingData($response); } - return ApiResponse::errorMissingData($response, $data); + $artistIds = $this->mySQLSongWriter->insertArtists($data["artists"]); + $albumId = $this->mySQLSongWriter->insertAlbum($data["album"]); + $songId = $this->mySQLSongWriter->insertSong($data); + $lyricsIds = $this->mySQLSongWriter->insertLyrics($data["lyrics"], $songId); + $this->mySQLSongWriter->insertSongToArtist($artistIds, $songId); + + $responseData = [ + "song_id" => $songId, + "album_id" => $albumId, + "lyrics_id" => $lyricsIds, + "artists_id" => $artistIds, + "created_by" => "tolik518", + "created" => "2023-06-21 00:00:00" + ]; + + return ApiResponse::sucessful($response, $responseData, 201); + + //return ApiResponse::errorMissingData($response, $data); } } diff --git a/code/src/Mapper/SongDataMapper.php b/code/src/Mapper/SongDataMapperToJSON.php similarity index 100% rename from code/src/Mapper/SongDataMapper.php rename to code/src/Mapper/SongDataMapperToJSON.php diff --git a/code/src/database/MySQLSongWriter.php b/code/src/database/MySQLSongWriter.php new file mode 100644 index 0000000..5897122 --- /dev/null +++ b/code/src/database/MySQLSongWriter.php @@ -0,0 +1,137 @@ +mySqlConnection->prepare(' + INSERT SongArtists (artist_id, song_id) + VALUES (:artist_id, :song_id) + '); + + $sql->bindValue(":artist_id", $artistId); + $sql->bindValue(":song_id", $songId); + + $sql->execute(); + } + } + + public function insertLyrics(array $lyrics, int $songId): array + { + $linesId = []; + foreach ($lyrics as $verse_nr => $verse) + { + + foreach ($verse as $line) { + $sql = $this->mySqlConnection->prepare(' + INSERT Lyrics (verse_nr, line_nr, qazaq_cyr, qazaq_lat, english, russian, original_lang, song_id) + VALUES (:verse_nr, :line_nr, :qazaq_cyr, :qazaq_lat, :english, :russian, :original_lang, :song_id) + '); + + $sql->bindValue(":verse_nr", $verse_nr); + $sql->bindValue(":line_nr", $line["line_nr"]); + $sql->bindValue(":qazaq_cyr", $line["qazaq_cyr"]); + $sql->bindValue(":qazaq_lat", Transliterator::toLatin($line["qazaq_cyr"])); + $sql->bindValue(":english", $line["english"]); + $sql->bindValue(":russian", $line["russian"]); + $sql->bindValue(":original_lang", $line["original_lang"]); + $sql->bindValue(":song_id", $songId); + + $sql->execute(); + $linesId[] = (int) $this->mySqlConnection->lastInsertId(); + } + } + return $linesId; + } + + public function insertSong(array $song): int + { + $sql = $this->mySqlConnection->prepare(' + INSERT Song (title_cyr, title_lat, release_date, cover_art) + VALUES (:title_cyr, :title_lat, :release_date, :cover_art) + '); + $sql->bindValue(":title_cyr", $song["title_cyr"]); + $sql->bindValue(":title_lat", $song["title_lat"]); + $sql->bindValue(":release_date", $song["release_date"]); + $sql->bindValue(":cover_art", $song["cover_art"]); + + $sql->execute(); + $songId = (int) $this->mySqlConnection->lastInsertId(); + + return $songId; + } + + public function insertAlbum(array $album): ?int + { + if (!isset($album['id'])) { + if (isset($album['name_cyr']) || isset($album['name_lat'])) + { + $sql = $this->mySqlConnection->prepare(' + INSERT Album (name_cyr, name_lat) + VALUES (:name_cyr, :name_lat) + '); + $name_lat = isset($album["name_lat"]) ? $album["name_lat"] : Transliterator::toLatin($album["name_cyr"]); + $sql->bindValue(":name_lat", $name_lat); + $sql->bindValue(":name_cyr", $album["name_cyr"]); + $sql->execute(); + $albumId = (int) $this->mySqlConnection->lastInsertId(); + } + else { + $albumId = null; + } + } else { + $albumId = (int) $album['id']; + } + return $albumId; + } + + public function insertArtists(array $artists): array + { + $artistIds = []; + foreach ($artists as $artist) { + $artistIds[] = $this->insertArtistAndGetID($artist); + } + return $artistIds; + } + + private function insertArtistAndGetID(mixed $artist): int + { + if (!isset($artist['id'])) { + $sql = $this->mySqlConnection->prepare(' + INSERT Artist (name_cyr, name_lat) + VALUES (:name_cyr, :name_lat) + '); + $sql->bindValue(":name_lat", $artist["name_lat"]); + $sql->bindValue(":name_cyr", $artist["name_cyr"]); + $sql->execute(); + $artistId = (int) $this->mySqlConnection->lastInsertId(); + } else { + $artistId = (int) $artist['id']; + } + return $artistId; + } + +} \ No newline at end of file diff --git a/code/src/routes.php b/code/src/routes.php index 91d0c78..b709bbc 100644 --- a/code/src/routes.php +++ b/code/src/routes.php @@ -16,6 +16,13 @@ })->setName('healtcheck'); $app->group('/api/v1', function (Group $v1) use ($factory) { + + //TODO: implement /songs route + $v1->get('/songs', function (Request $request, Response $response) use ($factory) { + $response->getBody()->write("[50000001,50000001]"); + return $response->withStatus(200); + })->setName('healtcheck'); + $v1->group('/song', function (Group $song) use ($factory) { $song->post('', function (Request $request, Response $response) use ($factory) { return $factory->createSongWriter()->handle($request, $response); @@ -24,6 +31,7 @@ return $factory->createSongReader()->handle($request, $response); })->setName('v1.GET.song'); }); + $v1->group('/artist', function (Group $song) use ($factory) { $song->get('/{artist_id:\d+}', function (Request $request, Response $response) use ($factory) { return $factory->createArtistHandler()->handle($request, $response);