Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions server/src/graphQL/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { pool } from '../database/connection.js';

const formatCoverUrl = (filename: string | null) => {
if (!filename) return null;
if (filename.startsWith('http://') || filename.startsWith('https://')) {
return filename;
}
const baseUrl = process.env.SERVER_URL || 'http://it2810-05.idi.ntnu.no/project2';
const cleanFilename = filename.replace(/^\/+/, '');
return `${baseUrl}/covers/${cleanFilename}`;
};

export const resolvers = {
Query: {
albums: async (_: unknown, args: { search?: string; genre?: string; decade?: number; first?: number }) => {
Expand All @@ -10,7 +20,7 @@ export const resolvers = {
a.artist,
a.release_year AS "releaseYear",
g.name AS genre,
a.cover_url AS "coverUrl"
a.cover_path AS "coverUrl"
FROM albums a
LEFT JOIN album_genres ag ON ag.album_id = a.id
LEFT JOIN genres g ON g.id = ag.genre_id
Expand Down Expand Up @@ -47,10 +57,15 @@ export const resolvers = {
const hasNextPage = rows.length > limit;
const sliced = hasNextPage ? rows.slice(0, limit) : rows;

const formattedRows = sliced.map((album) => ({
...album,
coverUrl: formatCoverUrl(album.coverUrl),
}));

return {
edges: sliced,
edges: formattedRows,
pageInfo: {
endCursor: sliced.length > 0 ? sliced[sliced.length - 1].id : null,
endCursor: formattedRows.length > 0 ? formattedRows[formattedRows.length - 1].id : null,
hasNextPage,
},
};
Expand Down Expand Up @@ -92,6 +107,7 @@ export const resolvers = {

return {
...album,
coverUrl: formatCoverUrl(album.coverUrl),
tracks: trackRows,
};
},
Expand Down