Finish fixing photos architecture

This commit is contained in:
Justin Edmund 2025-06-13 07:13:06 -04:00
parent 6b44c1b7d0
commit 00fc9b90cc
4 changed files with 68 additions and 33 deletions

View file

@ -15,12 +15,10 @@ export const GET: RequestHandler = async (event) => {
const album = await prisma.album.findUnique({ const album = await prisma.album.findUnique({
where: { slug }, where: { slug },
include: { include: {
photos: { media: {
where: {
status: 'published',
showInPhotos: true
},
orderBy: { displayOrder: 'asc' }, orderBy: { displayOrder: 'asc' },
include: {
media: {
select: { select: {
id: true, id: true,
filename: true, filename: true,
@ -28,29 +26,21 @@ export const GET: RequestHandler = async (event) => {
thumbnailUrl: true, thumbnailUrl: true,
width: true, width: true,
height: true, height: true,
caption: true, photoCaption: true,
displayOrder: true, photoTitle: true,
mediaId: true, photoDescription: true,
media: {
select: {
id: true,
altText: true,
description: true, description: true,
isPhotography: true, isPhotography: true,
mimeType: true, mimeType: true,
size: true size: true,
exifData: true
} }
} }
} }
}, },
_count: { _count: {
select: { select: {
photos: { media: true
where: {
status: 'published',
showInPhotos: true
}
}
} }
} }
} }
@ -60,7 +50,26 @@ export const GET: RequestHandler = async (event) => {
return errorResponse('Album not found', 404) 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) { } catch (error) {
logger.error('Failed to retrieve album by slug', error as Error) logger.error('Failed to retrieve album by slug', error as Error)
return errorResponse('Failed to retrieve album', 500) return errorResponse('Failed to retrieve album', 500)

View file

@ -11,6 +11,8 @@ export const GET: RequestHandler = async (event) => {
} }
try { try {
logger.info('Fetching photo', { mediaId: id })
const media = await prisma.media.findUnique({ const media = await prisma.media.findUnique({
where: { id }, where: { id },
include: { include: {
@ -25,13 +27,23 @@ export const GET: RequestHandler = async (event) => {
}) })
if (!media) { if (!media) {
logger.warn('Media not found', { mediaId: id })
return errorResponse('Photo not found', 404) 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 // For public access, only return media marked as photography
const isAdminRequest = checkAdminAuth(event) const isAdminRequest = checkAdminAuth(event)
logger.info('Authorization check', { isAdmin: isAdminRequest })
if (!isAdminRequest) { if (!isAdminRequest) {
if (!media.isPhotography) { if (!media.isPhotography) {
logger.warn('Media not marked as photography', { mediaId: id })
return errorResponse('Photo not found', 404) return errorResponse('Photo not found', 404)
} }
// If media is in an album, check album is published and isPhotography // 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({ const fullAlbum = await prisma.album.findUnique({
where: { id: album.id } where: { id: album.id }
}) })
logger.info('Album check', {
albumId: album.id,
status: fullAlbum?.status,
isPhotography: fullAlbum?.isPhotography
})
if (!fullAlbum || fullAlbum.status !== 'published' || !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) return errorResponse('Photo not found', 404)
} }
} }

View file

@ -2,11 +2,19 @@ import type { PageLoad } from './$types'
export const load: PageLoad = async ({ params, fetch }) => { export const load: PageLoad = async ({ params, fetch }) => {
try { 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.ok) {
if (response.status === 404) { 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') throw new Error('Failed to fetch photo')
} }

View file

@ -2,13 +2,13 @@ import type { PageLoad } from './$types'
export const load: PageLoad = async ({ params, fetch }) => { export const load: PageLoad = async ({ params, fetch }) => {
try { try {
const photoId = parseInt(params.id) const mediaId = parseInt(params.id)
if (isNaN(photoId)) { if (isNaN(mediaId)) {
throw new Error('Invalid photo ID') throw new Error('Invalid media ID')
} }
// Fetch the photo by ID // Fetch the photo by media ID
const photoResponse = await fetch(`/api/photos/${photoId}`) const photoResponse = await fetch(`/api/photos/${mediaId}`)
if (!photoResponse.ok) { if (!photoResponse.ok) {
if (photoResponse.status === 404) { if (photoResponse.status === 404) {
throw new Error('Photo not found') throw new Error('Photo not found')
@ -29,7 +29,7 @@ export const load: PageLoad = async ({ params, fetch }) => {
return { return {
photo, photo,
photoItems, photoItems,
currentPhotoId: `media-${photoId}` // Updated to use media prefix currentPhotoId: `media-${mediaId}` // Updated to use media prefix
} }
} catch (error) { } catch (error) {
console.error('Error loading photo:', error) console.error('Error loading photo:', error)