|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Uxmp\Core\Api\Album; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use Teapot\StatusCode\Http; |
|
10
|
|
|
use Uxmp\Core\Api\AbstractApiApplication; |
|
11
|
|
|
use Uxmp\Core\Api\Lib\Message\JsonEnabledResponseInterface; |
|
12
|
|
|
use Uxmp\Core\Component\Config\ConfigProviderInterface; |
|
13
|
|
|
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface; |
|
14
|
|
|
use Uxmp\Core\Orm\Repository\GenreMapRepositoryInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class AlbumApplication extends AbstractApiApplication |
|
17
|
|
|
{ |
|
18
|
2 |
|
public function __construct( |
|
19
|
|
|
private readonly AlbumRepositoryInterface $albumRepository, |
|
20
|
|
|
private readonly ConfigProviderInterface $config, |
|
21
|
|
|
private readonly GenreMapRepositoryInterface $genreMapRepository, |
|
22
|
|
|
) { |
|
23
|
2 |
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
protected function run( |
|
26
|
|
|
ServerRequestInterface $request, |
|
27
|
|
|
JsonEnabledResponseInterface $response, |
|
28
|
|
|
array $args |
|
29
|
|
|
): ResponseInterface { |
|
30
|
2 |
|
$albumId = (int) ($args['albumId'] ?? 0); |
|
31
|
|
|
|
|
32
|
2 |
|
$album = $this->albumRepository->find($albumId); |
|
33
|
|
|
|
|
34
|
2 |
|
if ($album === null) { |
|
35
|
1 |
|
return $response->withStatus(Http::NOT_FOUND); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
$artist = $album->getArtist(); |
|
39
|
1 |
|
$genres = []; |
|
40
|
|
|
|
|
41
|
1 |
|
foreach ($this->genreMapRepository->findByAlbum($album) as $mapped_genre) { |
|
42
|
1 |
|
$genres[] = [ |
|
43
|
1 |
|
'id' => $mapped_genre->getGenreId(), |
|
44
|
1 |
|
'title' => $mapped_genre->getGenreTitle(), |
|
45
|
1 |
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
return $response->withJson([ |
|
49
|
1 |
|
'id' => $albumId, |
|
50
|
1 |
|
'name' => $album->getTitle(), |
|
51
|
1 |
|
'artistId' => $artist->getId(), |
|
52
|
1 |
|
'artistName' => $artist->getTitle(), |
|
53
|
1 |
|
'cover' => sprintf('%s/art/album/%d', $this->config->getBaseUrl(), $albumId), |
|
54
|
1 |
|
'length' => $album->getLength(), |
|
55
|
1 |
|
'mbId' => $album->getMbid(), |
|
56
|
1 |
|
'year' => $album->getYear(), |
|
57
|
1 |
|
'genres' => $genres, |
|
58
|
1 |
|
]); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|