by id routing
This commit is contained in:
@@ -35,16 +35,52 @@ const getPostByTitle = query(
|
|||||||
const userID = await getUserID(event.nativeEvent);
|
const userID = await getUserID(event.nativeEvent);
|
||||||
const conn = ConnectionFactory();
|
const conn = ConnectionFactory();
|
||||||
|
|
||||||
let query;
|
// Handle by-id route: lookup post by ID and redirect to title-based URL
|
||||||
//TODO: get id from url param instead of title param
|
if (title === "by-id") {
|
||||||
//
|
const url = new URL(event.request.url);
|
||||||
if (title == "by-id") {
|
const id = url.searchParams.get("id");
|
||||||
const [searchParams, setSearchParams] = useSearchParams();
|
|
||||||
|
|
||||||
query = "SELECT * FROM Post WHERE id = ?";
|
if (!id) {
|
||||||
} else {
|
return {
|
||||||
query = "SELECT * FROM Post WHERE title = ?";
|
post: null,
|
||||||
|
exists: false,
|
||||||
|
comments: [],
|
||||||
|
likes: [],
|
||||||
|
tags: [],
|
||||||
|
userCommentArray: [],
|
||||||
|
reactionArray: [],
|
||||||
|
privilegeLevel: "anonymous" as const,
|
||||||
|
userID: null
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const idQuery = "SELECT title FROM Post WHERE id = ?";
|
||||||
|
const idResult = await conn.execute({
|
||||||
|
sql: idQuery,
|
||||||
|
args: [id]
|
||||||
|
});
|
||||||
|
|
||||||
|
const postData = idResult.rows[0] as any;
|
||||||
|
if (postData?.title) {
|
||||||
|
return {
|
||||||
|
redirect: `/blog/${encodeURIComponent(postData.title)}${sortBy !== "newest" ? `?sortBy=${sortBy}` : ""}`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
post: null,
|
||||||
|
exists: false,
|
||||||
|
comments: [],
|
||||||
|
likes: [],
|
||||||
|
tags: [],
|
||||||
|
userCommentArray: [],
|
||||||
|
reactionArray: [],
|
||||||
|
privilegeLevel: "anonymous" as const,
|
||||||
|
userID: null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let query = "SELECT * FROM Post WHERE title = ?";
|
||||||
if (privilegeLevel !== "admin") {
|
if (privilegeLevel !== "admin") {
|
||||||
query += ` AND published = TRUE`;
|
query += ` AND published = TRUE`;
|
||||||
}
|
}
|
||||||
@@ -264,7 +300,13 @@ export default function PostPage() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Show when={data()} fallback={<TerminalSplash />}>
|
<Show when={data()} fallback={<TerminalSplash />}>
|
||||||
{(loadedData) => (
|
{(loadedData) => {
|
||||||
|
// Handle redirect for by-id route
|
||||||
|
if ("redirect" in loadedData()) {
|
||||||
|
return <Navigate href={(loadedData() as any).redirect} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
<Show when={loadedData().post} fallback={<Navigate href="/404" />}>
|
<Show when={loadedData().post} fallback={<Navigate href="/404" />}>
|
||||||
{(p) => {
|
{(p) => {
|
||||||
const postData = loadedData();
|
const postData = loadedData();
|
||||||
@@ -434,7 +476,8 @@ export default function PostPage() {
|
|||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
</Show>
|
</Show>
|
||||||
)}
|
);
|
||||||
|
}}
|
||||||
</Show>
|
</Show>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user