by id routing
This commit is contained in:
@@ -35,16 +35,52 @@ const getPostByTitle = query(
|
||||
const userID = await getUserID(event.nativeEvent);
|
||||
const conn = ConnectionFactory();
|
||||
|
||||
let query;
|
||||
//TODO: get id from url param instead of title param
|
||||
//
|
||||
if (title == "by-id") {
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
// Handle by-id route: lookup post by ID and redirect to title-based URL
|
||||
if (title === "by-id") {
|
||||
const url = new URL(event.request.url);
|
||||
const id = url.searchParams.get("id");
|
||||
|
||||
query = "SELECT * FROM Post WHERE id = ?";
|
||||
} else {
|
||||
query = "SELECT * FROM Post WHERE title = ?";
|
||||
if (!id) {
|
||||
return {
|
||||
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") {
|
||||
query += ` AND published = TRUE`;
|
||||
}
|
||||
@@ -264,7 +300,13 @@ export default function PostPage() {
|
||||
return (
|
||||
<>
|
||||
<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" />}>
|
||||
{(p) => {
|
||||
const postData = loadedData();
|
||||
@@ -434,7 +476,8 @@ export default function PostPage() {
|
||||
);
|
||||
}}
|
||||
</Show>
|
||||
)}
|
||||
);
|
||||
}}
|
||||
</Show>
|
||||
</>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user