mirror of
https://github.com/apricote/Listory.git
synced 2026-02-08 02:37:03 +00:00
feat(server): save genres for artists and albums
This can later be used for reports
This commit is contained in:
parent
9b96d0fab4
commit
3c6f3289f1
12 changed files with 225 additions and 0 deletions
|
|
@ -8,6 +8,7 @@ import {
|
|||
} from "typeorm";
|
||||
import { SpotifyLibraryDetails } from "../sources/spotify/spotify-library-details.entity";
|
||||
import { Artist } from "./artist.entity";
|
||||
import { Genre } from "./genre.entity";
|
||||
import { Track } from "./track.entity";
|
||||
|
||||
@Entity()
|
||||
|
|
@ -25,6 +26,9 @@ export class Album {
|
|||
@OneToMany(() => Track, (track) => track.album)
|
||||
tracks?: Track[];
|
||||
|
||||
@ManyToMany(() => Genre)
|
||||
genres?: Genre[];
|
||||
|
||||
@Column(() => SpotifyLibraryDetails)
|
||||
spotify: SpotifyLibraryDetails;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { Column, Entity, ManyToMany, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { SpotifyLibraryDetails } from "../sources/spotify/spotify-library-details.entity";
|
||||
import { Album } from "./album.entity";
|
||||
import { Genre } from "./genre.entity";
|
||||
|
||||
@Entity()
|
||||
export class Artist {
|
||||
|
|
@ -13,6 +14,9 @@ export class Artist {
|
|||
@ManyToMany(() => Album, (album) => album.artists)
|
||||
albums?: Album[];
|
||||
|
||||
@ManyToMany(() => Genre)
|
||||
genres?: Genre[];
|
||||
|
||||
@Column(() => SpotifyLibraryDetails)
|
||||
spotify: SpotifyLibraryDetails;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
import { SpotifyLibraryDetails } from "../../sources/spotify/spotify-library-details.entity";
|
||||
import { Artist } from "../artist.entity";
|
||||
import { Genre } from "../genre.entity";
|
||||
|
||||
export class CreateAlbumDto {
|
||||
name: string;
|
||||
artists: Artist[];
|
||||
genres: Genre[];
|
||||
spotify?: SpotifyLibraryDetails;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { SpotifyLibraryDetails } from "../../sources/spotify/spotify-library-details.entity";
|
||||
import { Genre } from "../genre.entity";
|
||||
|
||||
export class CreateArtistDto {
|
||||
name: string;
|
||||
genres: Genre[];
|
||||
spotify?: SpotifyLibraryDetails;
|
||||
}
|
||||
|
|
|
|||
3
src/music-library/dto/create-genre.dto.ts
Normal file
3
src/music-library/dto/create-genre.dto.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export class CreateGenreDto {
|
||||
name: string;
|
||||
}
|
||||
3
src/music-library/dto/find-genre.dto.ts
Normal file
3
src/music-library/dto/find-genre.dto.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export class FindGenreDto {
|
||||
name: string;
|
||||
}
|
||||
10
src/music-library/genre.entity.ts
Normal file
10
src/music-library/genre.entity.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||
|
||||
@Entity()
|
||||
export class Genre {
|
||||
@PrimaryGeneratedColumn("uuid")
|
||||
id: string;
|
||||
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
}
|
||||
5
src/music-library/genre.repository.ts
Normal file
5
src/music-library/genre.repository.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import { EntityRepository, Repository } from "typeorm";
|
||||
import { Genre } from "./genre.entity";
|
||||
|
||||
@EntityRepository(Genre)
|
||||
export class GenreRepository extends Repository<Genre> {}
|
||||
|
|
@ -2,6 +2,7 @@ import { Module } from "@nestjs/common";
|
|||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { AlbumRepository } from "./album.repository";
|
||||
import { ArtistRepository } from "./artist.repository";
|
||||
import { GenreRepository } from "./genre.repository";
|
||||
import { MusicLibraryService } from "./music-library.service";
|
||||
import { TrackRepository } from "./track.repository";
|
||||
|
||||
|
|
@ -10,6 +11,7 @@ import { TrackRepository } from "./track.repository";
|
|||
TypeOrmModule.forFeature([
|
||||
AlbumRepository,
|
||||
ArtistRepository,
|
||||
GenreRepository,
|
||||
TrackRepository,
|
||||
]),
|
||||
],
|
||||
|
|
|
|||
|
|
@ -6,10 +6,14 @@ import { Artist } from "./artist.entity";
|
|||
import { ArtistRepository } from "./artist.repository";
|
||||
import { CreateAlbumDto } from "./dto/create-album.dto";
|
||||
import { CreateArtistDto } from "./dto/create-artist.dto";
|
||||
import { CreateGenreDto } from "./dto/create-genre.dto";
|
||||
import { CreateTrackDto } from "./dto/create-track.dto";
|
||||
import { FindAlbumDto } from "./dto/find-album.dto";
|
||||
import { FindArtistDto } from "./dto/find-artist.dto";
|
||||
import { FindGenreDto } from "./dto/find-genre.dto";
|
||||
import { FindTrackDto } from "./dto/find-track.dto";
|
||||
import { Genre } from "./genre.entity";
|
||||
import { GenreRepository } from "./genre.repository";
|
||||
import { Track } from "./track.entity";
|
||||
import { TrackRepository } from "./track.repository";
|
||||
|
||||
|
|
@ -18,6 +22,7 @@ export class MusicLibraryService {
|
|||
constructor(
|
||||
private readonly albumRepository: AlbumRepository,
|
||||
private readonly artistRepository: ArtistRepository,
|
||||
private readonly genreRepository: GenreRepository,
|
||||
private readonly trackRepository: TrackRepository
|
||||
) {}
|
||||
|
||||
|
|
@ -80,6 +85,35 @@ export class MusicLibraryService {
|
|||
return album;
|
||||
}
|
||||
|
||||
async findGenre(query: FindGenreDto): Promise<Genre | undefined> {
|
||||
return this.genreRepository.findOne({
|
||||
where: { name: query.name },
|
||||
});
|
||||
}
|
||||
|
||||
async createGenre(data: CreateGenreDto): Promise<Genre> {
|
||||
const genre = this.genreRepository.create();
|
||||
|
||||
genre.name = data.name;
|
||||
|
||||
try {
|
||||
await this.genreRepository.save(genre);
|
||||
} catch (err) {
|
||||
if (
|
||||
err.code === PostgresErrorCodes.UNIQUE_VIOLATION &&
|
||||
err.constraint === "IDX_GENRE_NAME"
|
||||
) {
|
||||
// Multiple simultaneous importGenre calls for the same genre were
|
||||
// executed and it is now available in the database for use to retrieve
|
||||
return this.findGenre({ name: data.name });
|
||||
}
|
||||
|
||||
throw err;
|
||||
}
|
||||
|
||||
return genre;
|
||||
}
|
||||
|
||||
async findTrack(query: FindTrackDto): Promise<Track | undefined> {
|
||||
return this.trackRepository.findOne({
|
||||
where: { spotify: { id: query.spotify.id } },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue