From 00fc9b90cc47907c916e8aa8ac3569056352283d Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Fri, 13 Jun 2025 07:13:06 -0400 Subject: [PATCH] Finish fixing photos architecture --- .../api/albums/by-slug/[slug]/+server.ts | 57 +++++++++++-------- src/routes/api/photos/[id]/+server.ts | 18 ++++++ .../photos/[albumSlug]/[photoId]/+page.ts | 14 ++++- src/routes/photos/p/[id]/+page.ts | 12 ++-- 4 files changed, 68 insertions(+), 33 deletions(-) diff --git a/src/routes/api/albums/by-slug/[slug]/+server.ts b/src/routes/api/albums/by-slug/[slug]/+server.ts index 121823f..0546763 100644 --- a/src/routes/api/albums/by-slug/[slug]/+server.ts +++ b/src/routes/api/albums/by-slug/[slug]/+server.ts @@ -15,42 +15,32 @@ export const GET: RequestHandler = async (event) => { const album = await prisma.album.findUnique({ where: { slug }, include: { - photos: { - where: { - status: 'published', - showInPhotos: true - }, + media: { orderBy: { displayOrder: 'asc' }, - select: { - id: true, - filename: true, - url: true, - thumbnailUrl: true, - width: true, - height: true, - caption: true, - displayOrder: true, - mediaId: true, + include: { media: { select: { id: true, - altText: true, + filename: true, + url: true, + thumbnailUrl: true, + width: true, + height: true, + photoCaption: true, + photoTitle: true, + photoDescription: true, description: true, isPhotography: true, mimeType: true, - size: true + size: true, + exifData: true } } } }, _count: { select: { - photos: { - where: { - status: 'published', - showInPhotos: true - } - } + media: true } } } @@ -60,7 +50,26 @@ export const GET: RequestHandler = async (event) => { return errorResponse('Album not found', 404) } - return jsonResponse(album) + // Transform the album data to include photos array + const transformedAlbum = { + ...album, + photos: album.media.map((albumMedia) => ({ + id: albumMedia.media.id, + filename: albumMedia.media.filename, + url: albumMedia.media.url, + thumbnailUrl: albumMedia.media.thumbnailUrl, + width: albumMedia.media.width, + height: albumMedia.media.height, + caption: albumMedia.media.photoCaption || albumMedia.media.description, + title: albumMedia.media.photoTitle, + description: albumMedia.media.photoDescription, + displayOrder: albumMedia.displayOrder, + exifData: albumMedia.media.exifData + })), + totalPhotos: album._count.media + } + + return jsonResponse(transformedAlbum) } catch (error) { logger.error('Failed to retrieve album by slug', error as Error) return errorResponse('Failed to retrieve album', 500) diff --git a/src/routes/api/photos/[id]/+server.ts b/src/routes/api/photos/[id]/+server.ts index 6fd1ffd..41affc7 100644 --- a/src/routes/api/photos/[id]/+server.ts +++ b/src/routes/api/photos/[id]/+server.ts @@ -11,6 +11,8 @@ export const GET: RequestHandler = async (event) => { } try { + logger.info('Fetching photo', { mediaId: id }) + const media = await prisma.media.findUnique({ where: { id }, include: { @@ -25,13 +27,23 @@ export const GET: RequestHandler = async (event) => { }) if (!media) { + logger.warn('Media not found', { mediaId: id }) return errorResponse('Photo not found', 404) } + + logger.info('Media found', { + mediaId: id, + isPhotography: media.isPhotography, + albumCount: media.albums.length + }) // For public access, only return media marked as photography const isAdminRequest = checkAdminAuth(event) + logger.info('Authorization check', { isAdmin: isAdminRequest }) + if (!isAdminRequest) { if (!media.isPhotography) { + logger.warn('Media not marked as photography', { mediaId: id }) return errorResponse('Photo not found', 404) } // If media is in an album, check album is published and isPhotography @@ -40,7 +52,13 @@ export const GET: RequestHandler = async (event) => { const fullAlbum = await prisma.album.findUnique({ where: { id: album.id } }) + logger.info('Album check', { + albumId: album.id, + status: fullAlbum?.status, + isPhotography: fullAlbum?.isPhotography + }) if (!fullAlbum || fullAlbum.status !== 'published' || !fullAlbum.isPhotography) { + logger.warn('Album not valid for public access', { albumId: album.id }) return errorResponse('Photo not found', 404) } } diff --git a/src/routes/photos/[albumSlug]/[photoId]/+page.ts b/src/routes/photos/[albumSlug]/[photoId]/+page.ts index e22c5c2..4e8a8de 100644 --- a/src/routes/photos/[albumSlug]/[photoId]/+page.ts +++ b/src/routes/photos/[albumSlug]/[photoId]/+page.ts @@ -2,11 +2,19 @@ import type { PageLoad } from './$types' export const load: PageLoad = async ({ params, fetch }) => { try { - const response = await fetch(`/api/photos/${params.albumSlug}/${params.photoId}`) + const { albumSlug, photoId } = params + const mediaId = parseInt(photoId) + + if (isNaN(mediaId)) { + throw new Error('Invalid photo ID') + } + // Fetch the photo and album data with navigation + const response = await fetch(`/api/photos/${albumSlug}/${mediaId}`) + if (!response.ok) { if (response.status === 404) { - throw new Error('Photo not found') + throw new Error('Photo or album not found') } throw new Error('Failed to fetch photo') } @@ -27,4 +35,4 @@ export const load: PageLoad = async ({ params, fetch }) => { error: error instanceof Error ? error.message : 'Failed to load photo' } } -} +} \ No newline at end of file diff --git a/src/routes/photos/p/[id]/+page.ts b/src/routes/photos/p/[id]/+page.ts index 1bdfe43..6daa0ce 100644 --- a/src/routes/photos/p/[id]/+page.ts +++ b/src/routes/photos/p/[id]/+page.ts @@ -2,13 +2,13 @@ import type { PageLoad } from './$types' export const load: PageLoad = async ({ params, fetch }) => { try { - const photoId = parseInt(params.id) - if (isNaN(photoId)) { - throw new Error('Invalid photo ID') + const mediaId = parseInt(params.id) + if (isNaN(mediaId)) { + throw new Error('Invalid media ID') } - // Fetch the photo by ID - const photoResponse = await fetch(`/api/photos/${photoId}`) + // Fetch the photo by media ID + const photoResponse = await fetch(`/api/photos/${mediaId}`) if (!photoResponse.ok) { if (photoResponse.status === 404) { throw new Error('Photo not found') @@ -29,7 +29,7 @@ export const load: PageLoad = async ({ params, fetch }) => { return { photo, photoItems, - currentPhotoId: `media-${photoId}` // Updated to use media prefix + currentPhotoId: `media-${mediaId}` // Updated to use media prefix } } catch (error) { console.error('Error loading photo:', error)