Add page routing and content fetchers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { createDirectusConnection } from "@/lib/directus";
|
||||
import { print } from 'graphql';
|
||||
import getBlogs from '@/graphql/blogs/getBlogs.graphql';
|
||||
import getBlogPost from '@/graphql/blogs/getBlog.graphql';
|
||||
import { formatDate } from "@/lib/dates";
|
||||
|
||||
export async function getAllBlogs(settings: GlobalSettings): Promise<BlogPost[]> {
|
||||
@@ -23,6 +24,8 @@ export async function getAllBlogs(settings: GlobalSettings): Promise<BlogPost[]>
|
||||
];
|
||||
|
||||
const blog: BlogPost = {
|
||||
type: "BlogPost",
|
||||
id: blogRecord["id"],
|
||||
lastModified: new Date(),
|
||||
title: blogRecord["title"],
|
||||
content: blogRecord["content"],
|
||||
@@ -70,3 +73,69 @@ export async function getAllBlogs(settings: GlobalSettings): Promise<BlogPost[]>
|
||||
|
||||
return blogs;
|
||||
}
|
||||
|
||||
export async function getBlog(settings: GlobalSettings, route: string): Promise<BlogPost> {
|
||||
const client = await createDirectusConnection();
|
||||
const result = await client.query(print(getBlogPost), {
|
||||
route: route
|
||||
});
|
||||
|
||||
const blogRecord = result["Blogs"][0];
|
||||
|
||||
let dates: string[] = [
|
||||
settings.blog.lastModified.toISOString(),
|
||||
settings.website.lastModified.toISOString(),
|
||||
blogRecord["date_created"],
|
||||
blogRecord["date_updated"],
|
||||
blogRecord["search_engine"][0]["date_created"],
|
||||
blogRecord["search_engine"][0]["date_updated"],
|
||||
blogRecord["search_engine"][0]["thumbnail"]["created_on"]
|
||||
];
|
||||
|
||||
const blog: BlogPost = {
|
||||
type: "BlogPost",
|
||||
id: blogRecord["id"],
|
||||
lastModified: new Date(),
|
||||
title: blogRecord["title"],
|
||||
content: blogRecord["content"],
|
||||
date: blogRecord["date"],
|
||||
url: blogRecord["url"],
|
||||
searchEngine: {
|
||||
title: blogRecord["search_engine"][0]["title"],
|
||||
description: blogRecord["search_engine"][0]["description"],
|
||||
allowCrawlers: blogRecord["search_engine"][0]["allow_crawler"],
|
||||
canonical: blogRecord["search_engine"][0]["canonical"],
|
||||
priority: blogRecord["search_engine"][0]["priority"],
|
||||
thumbnail: {
|
||||
url: blogRecord["search_engine"][0]["thumbnail"]["filename_disk"],
|
||||
height: blogRecord["search_engine"][0]["thumbnail"]["height"],
|
||||
width: blogRecord["search_engine"][0]["thumbnail"]["width"]
|
||||
}
|
||||
},
|
||||
tags: []
|
||||
};
|
||||
|
||||
blogRecord["tags"].forEach((tagRecord: any) => {
|
||||
blog["tags"].push({
|
||||
text: tagRecord["Tags_id"]["text"],
|
||||
code: tagRecord["Tags_id"]["code"],
|
||||
color: tagRecord["Tags_id"]["color"]
|
||||
});
|
||||
|
||||
dates.push(tagRecord["Tags_id"]["date_created"]);
|
||||
dates.push(tagRecord["Tags_id"]["date_updated"]);
|
||||
});
|
||||
|
||||
if (dates.filter(e => e !== null).length === 0) {
|
||||
blog.lastModified = new Date();
|
||||
}
|
||||
else {
|
||||
const sortedDates: string[] = dates.sort((a: string, b: string) => {
|
||||
return new Date(b).getTime() - new Date(a).getTime();
|
||||
});
|
||||
|
||||
blog.lastModified = new Date(sortedDates[0]);
|
||||
}
|
||||
|
||||
return blog;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createDirectusConnection } from "@/lib/directus";
|
||||
import { print } from 'graphql';
|
||||
import { formatDate } from "@/lib/dates";
|
||||
import getAllPages from "@/graphql/pages/getAllPages.graphql";
|
||||
import getPage from "@/graphql/pages/getPage.graphql";
|
||||
|
||||
export function dataToPage(pageRecord: any): WebPage {
|
||||
let dates: string[] = [
|
||||
@@ -285,6 +286,7 @@ export function dataToPage(pageRecord: any): WebPage {
|
||||
}
|
||||
|
||||
let page: WebPage = {
|
||||
type: "Webpage",
|
||||
id: pageRecord["id"],
|
||||
lastModified: lastModified,
|
||||
url: pageRecord["url"],
|
||||
@@ -320,3 +322,13 @@ export async function getAllWebpages(): Promise<WebPage[]> {
|
||||
|
||||
return pages;
|
||||
}
|
||||
|
||||
export async function getWebpage(route: string): Promise<WebPage | null> {
|
||||
const client = await createDirectusConnection();
|
||||
const result = await client.query(print(getPage), {
|
||||
date: formatDate(new Date(), "%Y-%M-%D"),
|
||||
route: route
|
||||
});
|
||||
|
||||
return dataToPage(result["Pages"][0]);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { createDirectusConnection } from "@/lib/directus";
|
||||
import { print } from "graphql";
|
||||
import getAlbums from '@/graphql/photos/getAlbums.graphql';
|
||||
import getAlbumItem from '@/graphql/photos/getAlbum.graphql';
|
||||
import { formatDate } from "@/lib/dates";
|
||||
|
||||
export async function getAllAlbums(settings: GlobalSettings): Promise<PhotoAlbum[]> {
|
||||
@@ -21,6 +22,7 @@ export async function getAllAlbums(settings: GlobalSettings): Promise<PhotoAlbum
|
||||
];
|
||||
|
||||
const album: PhotoAlbum = {
|
||||
type: "PhotoAlbum",
|
||||
title: albumRecord["title"],
|
||||
description: albumRecord["description"],
|
||||
url: albumRecord["url"],
|
||||
@@ -78,3 +80,76 @@ export async function getAllAlbums(settings: GlobalSettings): Promise<PhotoAlbum
|
||||
|
||||
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 = {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -22,4 +22,4 @@ export async function getAllCategories(settings: GlobalSettings): Promise<PhotoA
|
||||
});
|
||||
|
||||
return categories;
|
||||
}
|
||||
}
|
||||
|
||||
30
astro/src/content/photos/photos.ts
Normal file
30
astro/src/content/photos/photos.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { createDirectusConnection } from "@/lib/directus";
|
||||
import { print } from "graphql";
|
||||
import getPhotos from '@/graphql/photos/getPhotos.graphql';
|
||||
import md5 from "md5";
|
||||
|
||||
export async function getPhotoFromHash(albumUrl: string, hash: string): Promise<PhotoAlbumPhoto | null> {
|
||||
const client = await createDirectusConnection();
|
||||
const result = await client.query(print(getPhotos));
|
||||
|
||||
result["Photo_Albums"][0]["photos"].forEach((photo: any) => {
|
||||
const hashObject = md5(JSON.stringify({
|
||||
id: photo.id,
|
||||
url: photo.photo.url,
|
||||
width: photo.photo.width,
|
||||
height: photo.photo.height
|
||||
}));
|
||||
|
||||
if (hash.substring(hash.length - 10) === hash) {
|
||||
return {
|
||||
id: photo.id,
|
||||
text: photo.text,
|
||||
photo: {
|
||||
url: photo.photo.url,
|
||||
width: photo.photo.width,
|
||||
height: photo.photo.height
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { formatDate } from "@/lib/dates";
|
||||
import { createDirectusConnection } from "@/lib/directus";
|
||||
import { print } from "graphql";
|
||||
import getProjects from '@/graphql/projects/getProjects.graphql';
|
||||
|
||||
import getProjectPost from '@/graphql/projects/getProject.graphql';
|
||||
|
||||
export async function getAllProjects(settings: GlobalSettings): Promise<ProjectPost[]> {
|
||||
const client = await createDirectusConnection();
|
||||
@@ -71,3 +71,67 @@ export async function getAllProjects(settings: GlobalSettings): Promise<ProjectP
|
||||
|
||||
return projects;
|
||||
}
|
||||
|
||||
export async function getProject(settings: GlobalSettings, route: string): Promise<ProjectPost> {
|
||||
const client = await createDirectusConnection();
|
||||
const result = await client.query(print(getProjectPost), {
|
||||
route: route
|
||||
});
|
||||
|
||||
const projectRecord = result["Projects"][0];
|
||||
|
||||
let dates: string[] = [
|
||||
settings.project.lastModified.toISOString(),
|
||||
settings.website.lastModified.toISOString(),
|
||||
projectRecord["date_created"],
|
||||
projectRecord["date_updated"],
|
||||
projectRecord["search_engine"][0]["date_created"],
|
||||
projectRecord["search_engine"][0]["date_updated"],
|
||||
projectRecord["search_engine"][0]["thumbnail"]["created_on"]
|
||||
];
|
||||
|
||||
const project: ProjectPost = {
|
||||
lastModified: new Date(),
|
||||
title: projectRecord["title"],
|
||||
content: projectRecord["content"],
|
||||
date: projectRecord["date"],
|
||||
url: projectRecord["url"],
|
||||
searchEngine: {
|
||||
title: projectRecord["search_engine"][0]["title"],
|
||||
description: projectRecord["search_engine"][0]["description"],
|
||||
allowCrawlers: projectRecord["search_engine"][0]["allow_crawler"],
|
||||
canonical: projectRecord["search_engine"][0]["canonical"],
|
||||
priority: projectRecord["search_engine"][0]["priority"],
|
||||
thumbnail: {
|
||||
url: projectRecord["search_engine"][0]["thumbnail"]["filename_disk"],
|
||||
height: projectRecord["search_engine"][0]["thumbnail"]["height"],
|
||||
width: projectRecord["search_engine"][0]["thumbnail"]["width"]
|
||||
}
|
||||
},
|
||||
tags: []
|
||||
};
|
||||
|
||||
projectRecord["tags"].forEach((tagRecord: any) => {
|
||||
project["tags"].push({
|
||||
text: tagRecord["Tags_id"]["text"],
|
||||
code: tagRecord["Tags_id"]["code"],
|
||||
color: tagRecord["Tags_id"]["color"]
|
||||
});
|
||||
|
||||
dates.push(tagRecord["Tags_id"]["date_created"]);
|
||||
dates.push(tagRecord["Tags_id"]["date_updated"]);
|
||||
});
|
||||
|
||||
if (dates.filter(e => e !== null).length === 0) {
|
||||
project.lastModified = new Date();
|
||||
}
|
||||
else {
|
||||
const sortedDates: string[] = dates.sort((a: string, b: string) => {
|
||||
return new Date(b).getTime() - new Date(a).getTime();
|
||||
});
|
||||
|
||||
project.lastModified = new Date(sortedDates[0]);
|
||||
}
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user