feat(api): update existing artists in MusicLibrary

This commit is contained in:
Julian Tölle 2021-11-21 15:53:27 +01:00
parent a0c28e2324
commit a0ffe108e1
11 changed files with 176 additions and 1 deletions

View file

@ -4,6 +4,7 @@ import {
JoinTable,
ManyToMany,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from "typeorm";
import { SpotifyLibraryDetails } from "../sources/spotify/spotify-library-details.entity";
import { Album } from "./album.entity";
@ -26,4 +27,7 @@ export class Artist {
@Column(() => SpotifyLibraryDetails)
spotify: SpotifyLibraryDetails;
@UpdateDateColumn({ type: "timestamp" })
updatedAt: Date;
}

View file

@ -0,0 +1,10 @@
import { Artist } from "../artist.entity";
import { Genre } from "../genre.entity";
export class UpdateArtistDto {
artist: Artist;
updatedFields: {
name: string;
genres: Genre[];
};
}

View file

@ -8,6 +8,7 @@ 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 { UpdateArtistDto } from "./dto/update-artist.dto";
import { Genre } from "./genre.entity";
import { GenreRepository } from "./genre.repository";
import { MusicLibraryService } from "./music-library.service";
@ -133,6 +134,59 @@ describe("MusicLibraryService", () => {
);
});
});
describe("updateArtist", () => {
let updateArtistDto: UpdateArtistDto;
let artist: Artist;
beforeEach(() => {
artist = {
id: "ARTIST",
name: "Foo",
genres: [{ id: "GENRE_POP", name: "Baz Pop" }],
spotify: {
id: "SPOTIFY_ID",
},
} as Artist;
updateArtistDto = {
artist,
updatedFields: {
name: "Bar",
genres: [
{ id: "GENRE_METAL", name: "Foo Metal" },
{ id: "GENRE_ROCK", name: "Bar Rock" },
],
},
} as UpdateArtistDto;
artistRepository.save = jest
.fn()
.mockImplementation(async (_artist) => _artist);
});
it("updates the entity", async () => {
await expect(service.updateArtist(updateArtistDto)).resolves.toEqual(
artist
);
expect(artistRepository.save).toHaveBeenCalledTimes(1);
expect(artistRepository.save).toHaveBeenCalledWith(artist);
expect(artist).toHaveProperty("name", "Bar");
expect(artist).toHaveProperty("genres", expect.arrayContaining([]));
});
it("throws on generic errors", async () => {
artistRepository.save = jest
.fn()
.mockRejectedValue(new Error("Generic Error"));
await expect(service.updateArtist(updateArtistDto)).rejects.toThrow(
"Generic Error"
);
});
});
});
describe("Album", () => {

View file

@ -12,6 +12,7 @@ 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 { UpdateArtistDto } from "./dto/update-artist.dto";
import { Genre } from "./genre.entity";
import { GenreRepository } from "./genre.repository";
import { Track } from "./track.entity";
@ -32,6 +33,17 @@ export class MusicLibraryService {
});
}
async getArtistWithOldestUpdate(): Promise<Artist | undefined> {
const [oldestArtist] = await this.artistRepository.find({
take: 1,
order: {
updatedAt: "ASC",
},
});
return oldestArtist;
}
async createArtist(data: CreateArtistDto): Promise<Artist> {
const artist = this.artistRepository.create();
@ -57,6 +69,17 @@ export class MusicLibraryService {
return artist;
}
async updateArtist({
artist,
updatedFields,
}: UpdateArtistDto): Promise<Artist> {
artist.name = updatedFields.name;
artist.genres = updatedFields.genres;
artist.updatedAt = new Date();
return this.artistRepository.save(artist);
}
async findAlbum(query: FindAlbumDto): Promise<Album | undefined> {
return this.albumRepository.findOne({
where: { spotify: { id: query.spotify.id } },