CRUD Albums + Spotify API requests into DB.
All checks were successful
CI - Build Tonehaus Docker image / tonehaus-ci-build (push) Successful in 2m17s

This commit is contained in:
2025-11-20 19:53:45 +00:00
parent cd13f1478a
commit cd04fa5212
26 changed files with 6180 additions and 66 deletions

151
src/Entity/Album.php Normal file
View File

@@ -0,0 +1,151 @@
<?php
namespace App\Entity;
use App\Repository\AlbumRepository;
use App\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: AlbumRepository::class)]
#[ORM\Table(name: 'albums')]
#[ORM\HasLifecycleCallbacks]
class Album
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
// For Spotify-sourced albums; null for user-created
#[ORM\Column(type: 'string', length: 64, unique: true, nullable: true)]
private ?string $spotifyId = null;
// Public identifier for user-created albums (e.g., "u_abc123"); null for Spotify
#[ORM\Column(type: 'string', length: 64, unique: true, nullable: true)]
private ?string $localId = null;
// 'spotify' or 'user'
#[ORM\Column(type: 'string', length: 16)]
private string $source = 'spotify';
#[ORM\Column(type: 'string', length: 255)]
#[Assert\NotBlank]
private string $name = '';
/**
* @var list<string>
*/
#[ORM\Column(type: 'json')]
private array $artists = [];
// Stored as given by Spotify: YYYY or YYYY-MM or YYYY-MM-DD
#[ORM\Column(type: 'string', length: 20, nullable: true)]
private ?string $releaseDate = null;
#[ORM\Column(type: 'integer')]
private int $totalTracks = 0;
#[ORM\Column(type: 'string', length: 1024, nullable: true)]
private ?string $coverUrl = null;
#[ORM\Column(type: 'string', length: 1024, nullable: true)]
private ?string $externalUrl = null;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?User $createdBy = null;
#[ORM\Column(type: 'datetime_immutable')]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column(type: 'datetime_immutable')]
private ?\DateTimeImmutable $updatedAt = null;
#[ORM\PrePersist]
public function onPrePersist(): void
{
$now = new \DateTimeImmutable();
$this->createdAt = $now;
$this->updatedAt = $now;
}
#[ORM\PreUpdate]
public function onPreUpdate(): void
{
$this->updatedAt = new \DateTimeImmutable();
}
public function getId(): ?int { return $this->id; }
public function getSpotifyId(): ?string { return $this->spotifyId; }
public function setSpotifyId(?string $spotifyId): void { $this->spotifyId = $spotifyId; }
public function getLocalId(): ?string { return $this->localId; }
public function setLocalId(?string $localId): void { $this->localId = $localId; }
public function getSource(): string { return $this->source; }
public function setSource(string $source): void { $this->source = $source; }
public function getName(): string { return $this->name; }
public function setName(string $name): void { $this->name = $name; }
/**
* @return list<string>
*/
public function getArtists(): array { return $this->artists; }
/**
* @param list<string> $artists
*/
public function setArtists(array $artists): void { $this->artists = array_values($artists); }
public function getReleaseDate(): ?string { return $this->releaseDate; }
public function setReleaseDate(?string $releaseDate): void { $this->releaseDate = $releaseDate; }
public function getTotalTracks(): int { return $this->totalTracks; }
public function setTotalTracks(int $totalTracks): void { $this->totalTracks = $totalTracks; }
public function getCoverUrl(): ?string { return $this->coverUrl; }
public function setCoverUrl(?string $coverUrl): void { $this->coverUrl = $coverUrl; }
public function getExternalUrl(): ?string { return $this->externalUrl; }
public function setExternalUrl(?string $externalUrl): void { $this->externalUrl = $externalUrl; }
public function getCreatedBy(): ?User { return $this->createdBy; }
public function setCreatedBy(?User $user): void { $this->createdBy = $user; }
public function getCreatedAt(): ?\DateTimeImmutable { return $this->createdAt; }
public function getUpdatedAt(): ?\DateTimeImmutable { return $this->updatedAt; }
/**
* Shape the album like the Spotify payload expected by Twig templates.
*
* @return array<string,mixed>
*/
public function toTemplateArray(): array
{
$images = [];
if ($this->coverUrl) {
$images = [
['url' => $this->coverUrl],
['url' => $this->coverUrl],
];
}
$artists = array_map(static fn(string $n) => ['name' => $n], $this->artists);
$external = $this->externalUrl;
if ($external === null && $this->source === 'spotify' && $this->spotifyId) {
$external = 'https://open.spotify.com/album/' . $this->spotifyId;
}
$publicId = $this->source === 'user' ? (string) $this->localId : (string) $this->spotifyId;
return [
'id' => $publicId,
'name' => $this->name,
'images' => $images,
'artists' => $artists,
'release_date' => $this->releaseDate,
'total_tracks' => $this->totalTracks,
'external_urls' => [ 'spotify' => $external ],
'source' => $this->source,
];
}
}