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,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)

View file

@ -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)
}
}

View file

@ -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'
}
}
}
}

View file

@ -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)