Files
housesof/src/routes/[country]/[location]/index.ts
Félix Péault 6a7c4a0c75 Restrict location page access to matching country slug from param
Location page would be accessible if the country page param was set to anything else than the country it should be linked to from the data
2022-07-29 23:46:03 +02:00

87 lines
2.3 KiB
TypeScript

import type { RequestEvent, RequestHandlerOutput } from '@sveltejs/kit'
import { fetchAPI } from '$utils/api'
export const photoFields = `
title
slug
city
image {
id
title
width, height
}
date_taken
date_created
`
export async function GET ({ params }: RequestEvent): Promise<RequestHandlerOutput> {
try {
const { location: slug } = params
// Query
const res = await fetchAPI(`
query {
location (
filter: {
slug: { _eq: "${slug}" },
status: { _eq: "published" },
},
) {
id
name
slug
description
date_updated
illustration_desktop { id }
illustration_desktop_2x { id }
illustration_mobile { id }
credits {
credit_id {
name
website
}
}
country {
name
slug
}
has_poster
}
photos: photo (
filter: {
location: { slug: { _eq: "${slug}" }}
},
sort: "-date_created",
limit: ${import.meta.env.VITE_LIST_AMOUNT},
page: 1,
) {
${photoFields}
}
# Total
total_published: photo_aggregated (filter: { location: { slug: { _eq: "${slug}" }}}) {
count { location }
}
}
`)
const { data: { location: location, photos, total_published }} = res
if (params.country !== location[0].country.slug || !location.length) {
return { status: 404 }
}
return {
body: {
location: location[0],
photos: photos,
totalPhotos: photos.length ? total_published[0].count.location : 0,
}
}
} catch (error) {
return {
status: 404,
body: error,
}
}
}