316 lines
12 KiB
TypeScript
316 lines
12 KiB
TypeScript
import { createDirectusConnection } from "@/lib/directus";
|
|
import { print } from "graphql";
|
|
import getAlbums from '@/graphql/photos/getAlbums.graphql';
|
|
import getAlbumItem from '@/graphql/photos/getAlbum.graphql';
|
|
import getLastAlbumsQuery from '@/graphql/photos/getLastAlbums.graphql';
|
|
import getCategoryAlbumQuery from '@/graphql/photos/getCategoryAlbum.graphql';
|
|
import { formatDate } from "@/lib/dates";
|
|
|
|
export async function getAllAlbums(settings: GlobalSettings): Promise<PhotoAlbum[]> {
|
|
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 = {
|
|
exists: true,
|
|
type: "PhotoAlbum",
|
|
title: albumRecord["title"],
|
|
description: albumRecord["description"],
|
|
url: albumRecord["url"],
|
|
startDate: albumRecord["start_date"],
|
|
endDate: albumRecord["end_date"],
|
|
location: albumRecord["location"],
|
|
category: {
|
|
id: albumRecord["category"][0]["Photo_Categories_id"]["id"],
|
|
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_disk"],
|
|
height: albumRecord["thumbnail"]["height"],
|
|
width: albumRecord["thumbnail"]["width"]
|
|
},
|
|
photos: [],
|
|
lastModified: new Date()
|
|
};
|
|
|
|
albumRecord["photos"].forEach((photoRecord: any) => {
|
|
album.photos.push({
|
|
id: photoRecord["id"],
|
|
photo: {
|
|
url: photoRecord["photo"]["filename_disk"],
|
|
width: photoRecord["photo"]["width"],
|
|
height: photoRecord["photo"]["height"]
|
|
},
|
|
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;
|
|
}
|
|
|
|
export async function getAlbum(settings: GlobalSettings, route: string): Promise<PhotoAlbum> {
|
|
const client = await createDirectusConnection();
|
|
const result = await client.query(print(getAlbumItem), {
|
|
route: route
|
|
});
|
|
|
|
const albumRecord = result["Photo_Albums"][0];
|
|
|
|
let dates: string[] = [
|
|
settings.website.lastModified.toISOString(),
|
|
settings.photo.lastModified.toISOString(),
|
|
albumRecord["date_created"],
|
|
albumRecord["date_updated"],
|
|
albumRecord["thumbnail"]["created_on"],
|
|
];
|
|
|
|
const album: PhotoAlbum = {
|
|
exists: true,
|
|
type: "PhotoAlbum",
|
|
title: albumRecord["title"],
|
|
description: albumRecord["description"],
|
|
url: albumRecord["url"],
|
|
startDate: albumRecord["start_date"],
|
|
endDate: albumRecord["end_date"],
|
|
location: albumRecord["location"],
|
|
category: {
|
|
id: albumRecord["category"][0]["Photo_Categories_id"]["id"],
|
|
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({
|
|
id: photoRecord["id"],
|
|
photo: {
|
|
url: photoRecord["photo"]["filename_disk"],
|
|
width: photoRecord["photo"]["width"],
|
|
height: photoRecord["photo"]["height"]
|
|
},
|
|
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]);
|
|
}
|
|
|
|
return album;
|
|
}
|
|
|
|
export async function getLastAlbums(amount: number) {
|
|
const client = await createDirectusConnection();
|
|
const result = await client.query(print(getLastAlbumsQuery), {
|
|
date: formatDate(new Date(), "%Y-%M-%D"),
|
|
limit: amount
|
|
});
|
|
|
|
let albums: PhotoAlbum[] = [];
|
|
|
|
result["Photo_Albums"].forEach((albumRecord: any) => {
|
|
let dates: string[] = [
|
|
albumRecord["date_created"],
|
|
albumRecord["date_updated"],
|
|
albumRecord["thumbnail"]["created_on"],
|
|
];
|
|
|
|
const album: PhotoAlbum = {
|
|
exists: true,
|
|
type: "PhotoAlbum",
|
|
title: albumRecord["title"],
|
|
description: albumRecord["description"],
|
|
url: albumRecord["url"],
|
|
startDate: albumRecord["start_date"],
|
|
endDate: albumRecord["end_date"],
|
|
location: albumRecord["location"],
|
|
category: {
|
|
id: albumRecord["category"][0]["Photo_Categories_id"]["id"],
|
|
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_disk"],
|
|
height: albumRecord["thumbnail"]["height"],
|
|
width: albumRecord["thumbnail"]["width"]
|
|
},
|
|
photos: [],
|
|
lastModified: new Date()
|
|
};
|
|
|
|
albumRecord["photos"].forEach((photoRecord: any) => {
|
|
album.photos.push({
|
|
id: photoRecord["id"],
|
|
photo: {
|
|
url: photoRecord["photo"]["filename_disk"],
|
|
width: photoRecord["photo"]["width"],
|
|
height: photoRecord["photo"]["height"]
|
|
},
|
|
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;
|
|
}
|
|
|
|
export async function getCategoryAlbums(settings: GlobalSettings, route: string): Promise<PhotoAlbum[]> {
|
|
const client = await createDirectusConnection();
|
|
const result = await client.query(print(getCategoryAlbumQuery), {
|
|
date: formatDate(new Date(), "%Y-%M-%D"),
|
|
categoryUrl: `/${route}`
|
|
});
|
|
|
|
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 = {
|
|
exists: true,
|
|
type: "PhotoAlbum",
|
|
title: albumRecord["title"],
|
|
description: albumRecord["description"],
|
|
url: albumRecord["url"],
|
|
startDate: albumRecord["start_date"],
|
|
endDate: albumRecord["end_date"],
|
|
location: albumRecord["location"],
|
|
category: {
|
|
id: albumRecord["category"][0]["Photo_Categories_id"]["id"],
|
|
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_disk"],
|
|
height: albumRecord["thumbnail"]["height"],
|
|
width: albumRecord["thumbnail"]["width"]
|
|
},
|
|
photos: [],
|
|
lastModified: new Date()
|
|
};
|
|
|
|
albumRecord["photos"].forEach((photoRecord: any) => {
|
|
album.photos.push({
|
|
id: photoRecord["id"],
|
|
photo: {
|
|
url: photoRecord["photo"]["filename_disk"],
|
|
width: photoRecord["photo"]["width"],
|
|
height: photoRecord["photo"]["height"]
|
|
},
|
|
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;
|
|
}
|