feat: filter artist discography + counts to core releases unless showAllTypes
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { prisma } from "@/lib/db";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { isCoreRelease } from "@/lib/release-filter";
|
||||
|
||||
export async function GET(_request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await params;
|
||||
@@ -8,12 +9,14 @@ export async function GET(_request: Request, { params }: { params: Promise<{ id:
|
||||
include: { releases: { orderBy: [{ firstReleaseDate: "desc" }, { album: "asc" }] } },
|
||||
});
|
||||
if (!artist) return Response.json({ error: "not found" }, { status: 404 });
|
||||
const visibleReleases = artist.showAllTypes ? artist.releases : artist.releases.filter(isCoreRelease);
|
||||
return Response.json({
|
||||
id: artist.id,
|
||||
mbid: artist.mbid,
|
||||
name: artist.name,
|
||||
autoMonitorFuture: artist.autoMonitorFuture,
|
||||
releases: artist.releases.map((r) => ({
|
||||
showAllTypes: artist.showAllTypes,
|
||||
releases: visibleReleases.map((r) => ({
|
||||
id: r.id,
|
||||
rgMbid: r.rgMbid,
|
||||
album: r.album,
|
||||
@@ -35,13 +38,26 @@ export async function PATCH(request: Request, { params }: { params: Promise<{ id
|
||||
} catch {
|
||||
return Response.json({ error: "invalid JSON" }, { status: 400 });
|
||||
}
|
||||
const { autoMonitorFuture } = (body ?? {}) as { autoMonitorFuture?: unknown };
|
||||
if (typeof autoMonitorFuture !== "boolean") {
|
||||
return Response.json({ error: "autoMonitorFuture (boolean) is required" }, { status: 400 });
|
||||
const { autoMonitorFuture, showAllTypes } = (body ?? {}) as {
|
||||
autoMonitorFuture?: unknown;
|
||||
showAllTypes?: unknown;
|
||||
};
|
||||
const data: { autoMonitorFuture?: boolean; showAllTypes?: boolean } = {};
|
||||
if (typeof autoMonitorFuture === "boolean") data.autoMonitorFuture = autoMonitorFuture;
|
||||
if (typeof showAllTypes === "boolean") data.showAllTypes = showAllTypes;
|
||||
if (Object.keys(data).length === 0) {
|
||||
return Response.json(
|
||||
{ error: "autoMonitorFuture or showAllTypes (boolean) is required" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
try {
|
||||
const updated = await prisma.watchedArtist.update({ where: { id }, data: { autoMonitorFuture } });
|
||||
return Response.json({ id: updated.id, autoMonitorFuture: updated.autoMonitorFuture });
|
||||
const updated = await prisma.watchedArtist.update({ where: { id }, data });
|
||||
return Response.json({
|
||||
id: updated.id,
|
||||
autoMonitorFuture: updated.autoMonitorFuture,
|
||||
showAllTypes: updated.showAllTypes,
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
|
||||
return Response.json({ error: "not found" }, { status: 404 });
|
||||
|
||||
Reference in New Issue
Block a user