From ad73ab56721a43448ce478350d1e011abc090427 Mon Sep 17 00:00:00 2001 From: Quinn Hegeman <102350242+itsfinniii@users.noreply.github.com> Date: Sun, 8 Mar 2026 17:24:39 +0100 Subject: [PATCH] Add a function to get all photo albums --- astro/src/content/photos/albums.ts | 78 ++++++++++++++++++++++ astro/src/graphql/photos/getAlbums.graphql | 51 ++++++++++++++ astro/src/pages/index.astro | 6 +- astro/src/types/photos/album.d.ts | 27 ++++++++ astro/src/types/photos/category.d.ts | 3 + astro/src/types/photos/photo.d.ts | 3 + 6 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 astro/src/content/photos/albums.ts create mode 100644 astro/src/graphql/photos/getAlbums.graphql create mode 100644 astro/src/types/photos/album.d.ts create mode 100644 astro/src/types/photos/category.d.ts create mode 100644 astro/src/types/photos/photo.d.ts diff --git a/astro/src/content/photos/albums.ts b/astro/src/content/photos/albums.ts new file mode 100644 index 0000000..4e05e67 --- /dev/null +++ b/astro/src/content/photos/albums.ts @@ -0,0 +1,78 @@ +import { createDirectusConnection } from "@/lib/directus"; +import { print } from "graphql"; +import getAlbums from '@/graphql/photos/getAlbums.graphql'; +import { formatDate } from "@/lib/dates"; + +export async function getAllAlbums(settings: GlobalSettings): Promise { + const client = await createDirectusConnection(); + const result = await client.query(print(getAlbums), { + date: formatDate(new Date(), "%Y-%M-%D") + }); + + let albums: PhotoAlbum[] = []; + + result["Photo_Albums"].forEach((albumRecord: any) => { + let dates: string[] = [ + settings.website.lastModified.toISOString(), + settings.photo.lastModified.toISOString(), + albumRecord["date_created"], + albumRecord["date_updated"], + albumRecord["thumbnail"]["created_on"], + ]; + + const album: PhotoAlbum = { + title: albumRecord["title"], + description: albumRecord["description"], + url: albumRecord["url"], + startDate: albumRecord["start_date"], + endDate: albumRecord["end_date"], + location: albumRecord["location"], + category: { + title: albumRecord["category"][0]["Photo_Categories_id"]["title"], + url: albumRecord["category"][0]["Photo_Categories_id"]["url"], + thumbnail: { + url: albumRecord["category"][0]["Photo_Categories_id"]["thumbnail"]["filename_disk"], + height: albumRecord["category"][0]["Photo_Categories_id"]["thumbnail"]["height"], + width: albumRecord["category"][0]["Photo_Categories_id"]["thumbnail"]["width"] + } + }, + thumbnail: { + url: albumRecord["thumbnail"]["filename_download"], + height: albumRecord["thumbnail"]["height"], + width: albumRecord["thumbnail"]["width"] + }, + photos: [], + lastModified: new Date() + }; + + albumRecord["photos"].forEach((photoRecord: any) => { + album.photos.push({ + photo: { + url: photoRecord["photo"]["filename_download"], + height: photoRecord["photo"]["filename_download"]["height"], + width: photoRecord["photo"]["filename_download"]["width"] + }, + text: photoRecord["text"] + }); + + dates.push(photoRecord["date_created"]); + dates.push(photoRecord["date_updated"]); + dates.push(photoRecord["photo"]["created_on"]); + }); + + if (dates.filter(e => e !== null).length === 0) { + album.lastModified = new Date(); + } + else { + const sortedDates: string[] = dates.sort((a: string, b: string) => { + return new Date(b).getTime() - new Date(a).getTime(); + }); + + album.lastModified = new Date(sortedDates[0]); + } + + albums.push(album); + }); + + return albums; +} diff --git a/astro/src/graphql/photos/getAlbums.graphql b/astro/src/graphql/photos/getAlbums.graphql new file mode 100644 index 0000000..41cf3a1 --- /dev/null +++ b/astro/src/graphql/photos/getAlbums.graphql @@ -0,0 +1,51 @@ +query getAllAlbums($date: String!) { + Photo_Albums(sort: ["-start_date", "-date_created"], filter: { status: { _eq: "published" }, start_date: { _lte: $date }, category: { Photo_Categories_id: { status: { _eq: "published" } } } }) { + id, + date_created, + date_updated, + title, + description, + url, + thumbnail { + id, + created_on, + filename_download, + width, + height + }, + start_date, + end_date, + location, + category { + Photo_Categories_id { + id, + status, + date_created, + date_updated, + title, + url, + thumbnail { + id, + created_on, + filename_download, + width, + height + } + } + }, + photos(filter: { status: { _eq: "published" } }) { + id, + date_created, + date_updated, + photo { + id, + created_on, + filename_download, + width, + height + }, + text, + sort + } + } +} diff --git a/astro/src/pages/index.astro b/astro/src/pages/index.astro index 20cc011..03efb7d 100644 --- a/astro/src/pages/index.astro +++ b/astro/src/pages/index.astro @@ -1,10 +1,12 @@ --- -import { getAllBlogs } from "@/content/blogs/blogs"; +import { getAllAlbums } from "@/content/photos/albums"; import { getSettings } from "@/content/settings/settings" import WebpageLayout from "@/layouts/WebpageLayout.astro"; const settings = await getSettings(); -const blogs = await getAllBlogs(settings); +const albums = await getAllAlbums(settings); + +console.log(albums); --- diff --git a/astro/src/types/photos/album.d.ts b/astro/src/types/photos/album.d.ts new file mode 100644 index 0000000..a98afba --- /dev/null +++ b/astro/src/types/photos/album.d.ts @@ -0,0 +1,27 @@ +type PhotoAlbum = { + title: string; + url: string; + description: string | null; + + thumbnail: PhotoProps; + + startDate: string; + endDate: string | null; + location: string | null; + + category: PhotoAlbumCategory; + photos: PhotoAlbumPhoto[]; + + lastModified: Date; +} + +type PhotoAlbumCategory = { + title: string; + url: string; + thumbnail: PhotoProps; +} + +type PhotoAlbumPhoto = { + photo: PhotoProps; + text: string | null; +} diff --git a/astro/src/types/photos/category.d.ts b/astro/src/types/photos/category.d.ts new file mode 100644 index 0000000..d449e9a --- /dev/null +++ b/astro/src/types/photos/category.d.ts @@ -0,0 +1,3 @@ +type PhotoCategory = { + +} diff --git a/astro/src/types/photos/photo.d.ts b/astro/src/types/photos/photo.d.ts new file mode 100644 index 0000000..9c24dc5 --- /dev/null +++ b/astro/src/types/photos/photo.d.ts @@ -0,0 +1,3 @@ +type PhotoPhoto = { + +}