Skip to content

Commit

Permalink
#20 POST /song now writes to Album, Artist, Lyrics, Song, SongArtists
Browse files Browse the repository at this point in the history
  • Loading branch information
tolik518 committed Jun 21, 2023
1 parent d908d63 commit c887fee
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 2 deletions.
6 changes: 5 additions & 1 deletion code/src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion code/src/Handler/SongWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class SongWriter
{
public function __construct(
private MySQLSongWriter $mySQLSongWriter
) {
}

Expand All @@ -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);
}
}
File renamed without changes.
137 changes: 137 additions & 0 deletions code/src/database/MySQLSongWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace QazaqGenius\LyricsApi;

use PDO;
use QazaqGenius\Transliterator;
use Slim\Exception\HttpNotImplementedException;

class MySQLSongWriter
{
public function __construct(
private PDO $mySqlConnection
) {
}

public function writeAllSongData(
string $release_date,
string $title_cyr,
string $title_lat,
string $cover_art,
array $lyrics
)
{
return null;
}

public function insertSongToArtist(array $artistIds, int $songId)
{
foreach ($artistIds as $artistId){
$sql = $this->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;
}

}
8 changes: 8 additions & 0 deletions code/src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit c887fee

Please sign in to comment.