Skip to content

Commit

Permalink
Server: Album Service: allow getting many by slug or id + allow delet…
Browse files Browse the repository at this point in the history
…ing many
  • Loading branch information
Arthi-chaud committed Jan 10, 2025
1 parent 2b87faf commit a0892e6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 44 deletions.
9 changes: 2 additions & 7 deletions server/src/album/album.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,24 +289,19 @@ describe("Album Service", () => {
});

describe("Delete Album", () => {
it("should throw, as the album does not exist (by id)", () => {
const test = async () => albumService.delete({ id: -1 });
return expect(test()).rejects.toThrow(AlbumNotFoundException);
});

it("should not delete the album, as it has releases", async () => {
const albumQueryParameters = {
id: dummyRepository.compilationAlbumA.id,
};

const test = async () =>
await albumService.delete(albumQueryParameters);
await albumService.delete([albumQueryParameters]);
return expect(test()).rejects.toThrow(AlbumNotEmptyException);
});

it("should delete the album", async () => {
const tmpAlbum = await albumService.create({ name: "1234" });
await albumService.delete({ id: tmpAlbum.id });
await albumService.delete([{ id: tmpAlbum.id }]);
const test = async () => albumService.get({ id: tmpAlbum.id });
return expect(test()).rejects.toThrow(AlbumNotFoundException);
});
Expand Down
71 changes: 41 additions & 30 deletions server/src/album/album.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export default class AlbumService extends SearchableRepositoryService {
) {
const matchingIds = await this.getMatchingIds(token, pagination);
const artists = await this.getMany(
{ ...where, id: { in: matchingIds } },
{ ...where, albums: matchingIds.map((id) => ({ id })) },
{},
{},
include,
Expand All @@ -232,7 +232,7 @@ export default class AlbumService extends SearchableRepositoryService {
pagination,
);
return this.getMany(
{ ...where, id: { in: randomIds } },
{ ...where, albums: randomIds.map((id) => ({ id })) },
undefined,
undefined,
include,
Expand Down Expand Up @@ -278,8 +278,12 @@ export default class AlbumService extends SearchableRepositoryService {
name: buildStringSearchParameters(where.name),
};

if (where.id) {
query = deepmerge(query, { id: where.id });
if (where.albums) {
query = deepmerge(query, {
OR: where.albums.map((album) =>
AlbumService.formatWhereInput(album),
),
});
}
if (where.related) {
// Related albums have at least one song group in common
Expand Down Expand Up @@ -510,42 +514,49 @@ export default class AlbumService extends SearchableRepositoryService {
* Deletes an album
* @param where the query parameter
*/
async delete(where: AlbumQueryParameters.DeleteInput): Promise<AlbumModel> {
try {
const album = await this.prismaService.album.delete({
where: AlbumService.formatWhereInput(where),
});
async delete(where: AlbumQueryParameters.DeleteInput[]): Promise<number> {
const albums = await this.getMany(
{ albums: where },
undefined,
undefined,
{ releases: true },
);

this.meiliSearch.index(this.indexName).deleteDocument(album.id);
this.logger.warn(`Album '${album.slug}' deleted`);
return album;
} catch (error) {
if (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.code == PrismaError.ForeignConstraintViolation
) {
throw new AlbumNotEmptyException(where.id);
for (const album of albums) {
if (album.releases!.length) {
throw new AlbumNotEmptyException(album.id);
}
throw await this.onNotFound(error, where);
}
const deletedAlbum = await this.prismaService.album
.deleteMany({
where: AlbumService.formatManyWhereInput({ albums: where }),
})
.catch((error) => {
throw new UnhandledORMErrorException(error, where);
});
this.meiliSearch
.index(this.indexName)
.deleteDocuments(albums.map(({ id }) => id));
return deletedAlbum.count;
}

/**
* Delete all albums that do not have relaed releases
*/
async housekeeping(): Promise<void> {
const emptyAlbums = await this.prismaService.album
.findMany({
select: {
id: true,
_count: {
select: { releases: true },
},
},
})
.then((albums) => albums.filter((album) => !album._count.releases));
const emptyAlbums = await this.prismaService.album.findMany({
select: {
id: true,
},
where: { releases: { none: {} } },
});
const deletedAlbumCount = await this.delete(
emptyAlbums.map(({ id }) => ({ id })),
);

await Promise.all(emptyAlbums.map(({ id }) => this.delete({ id })));
if (deletedAlbumCount) {
this.logger.warn(`Deleted ${deletedAlbumCount} albums`);
}
await this.resolveMasterReleases();
}

Expand Down
1 change: 1 addition & 0 deletions server/src/album/models/album.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export type AlbumModel = {
type: AlbumType;
artistId: number | null;
artist?: ArtistModel | null;
releases?: Release[];
};
2 changes: 1 addition & 1 deletion server/src/album/models/album.query-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace AlbumQueryParameters {
releaseDate: SearchDateInput;
genre: GenreQueryParameters.WhereInput;
type: AlbumType;
id: { in: number[] };
albums: AlbumQueryParameters.WhereInput[];
// Get albums with a song in common. Does not include the given album
related: AlbumQueryParameters.WhereInput;
}>
Expand Down
8 changes: 3 additions & 5 deletions server/src/search/search-history.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,9 @@ export class SearchHistoryService {
);
const albums = await this.albumService.getMany(
{
id: {
in: history
.filter((item) => item.albumId !== null)
.map(({ albumId }) => albumId!),
},
albums: history
.filter((item) => item.albumId !== null)
.map(({ albumId }) => ({ id: albumId! })),
},
undefined,
undefined,
Expand Down
2 changes: 1 addition & 1 deletion server/src/search/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class SearchService {
[
(ids: number[]) =>
this.albumService.getMany(
{ id: { in: ids } },
{ albums: ids.map((id) => ({ id })) },
undefined,
undefined,
{
Expand Down

0 comments on commit a0892e6

Please sign in to comment.