New IDs
This commit is contained in:
parent
f98583a77d
commit
b9320d9529
@ -1,4 +1,5 @@
|
|||||||
<script>
|
<script>
|
||||||
|
import { getNamedId } from '$lib/util';
|
||||||
import Rating from './rating.svelte';
|
import Rating from './rating.svelte';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -8,7 +9,7 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<a href="/posts/{post.id}">{post.name}</a>
|
<a href="/posts/{getNamedId(post.id, post.name)}">{post.name}</a>
|
||||||
<p>{post.author?.name}</p>
|
<p>{post.author?.name}</p>
|
||||||
<p>{post.category.name}</p>
|
<p>{post.category.name}</p>
|
||||||
<Rating rating={post.rating}></Rating>
|
<Rating rating={post.rating}></Rating>
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { getCategoriesCached, getCategoriesCachedByRef, getCategoryCached } from './category';
|
import { getCategoriesCachedByRef, getCategoryCached } from './category';
|
||||||
import { getUser, getUsersCached, getUsersCachedByRef } from './user';
|
import { getUser, getUsersCachedByRef } from './user';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('$types/base').Post} Post
|
* @typedef {import('$types/base').Post} Post
|
||||||
@ -40,7 +40,7 @@ export async function getPosts(sql, category = undefined, limit = 10, offset = 0
|
|||||||
filter = sql``;
|
filter = sql``;
|
||||||
} else {
|
} else {
|
||||||
filter = sql`WHERE category_id = ${ category.id }`;
|
filter = sql`WHERE category_id = ${ category.id }`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const query = sql`
|
const query = sql`
|
||||||
SELECT id, author_id, name, category_id, latest_content, created_date, likes, dislikes
|
SELECT id, author_id, name, category_id, latest_content, created_date, likes, dislikes
|
||||||
|
|||||||
31
src/lib/util.js
Normal file
31
src/lib/util.js
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import { goto } from "$app/navigation";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string | undefined} name
|
||||||
|
* @returns {number | null}
|
||||||
|
*/
|
||||||
|
export function getIdFromName(name) {
|
||||||
|
const parts = (name || "").split("-", 2);
|
||||||
|
const parsed = parseInt(parts[0]);
|
||||||
|
|
||||||
|
return Number.isNaN(parsed) ? null : parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} id
|
||||||
|
* @param {string} name
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
export function getNamedId(id, name) {
|
||||||
|
const convertedName = name.replace(/[^\w\d]/g, '-').toLowerCase();
|
||||||
|
return `${id}-${convertedName}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} id
|
||||||
|
* @param {string} name
|
||||||
|
*/
|
||||||
|
export function gotoNamedId(id, name) {
|
||||||
|
const page = getNamedId(id, name);
|
||||||
|
goto(page, { replaceState: true });
|
||||||
|
}
|
||||||
@ -1,9 +1,15 @@
|
|||||||
import { getCommentsForPost } from "$lib/server/db/comment";
|
import { getCommentsForPost } from "$lib/server/db/comment";
|
||||||
import { getPost } from "$lib/server/db/post";
|
import { getPost } from "$lib/server/db/post";
|
||||||
|
import { getIdFromName } from "$lib/util";
|
||||||
|
import { error } from "@sveltejs/kit";
|
||||||
|
|
||||||
/** @type {import("@sveltejs/kit").ServerLoad} */
|
/** @type {import("@sveltejs/kit").ServerLoad} */
|
||||||
export async function load({ params, locals }) {
|
export async function load({ params, locals }) {
|
||||||
const post_id = Number(params.id);
|
const post_id = getIdFromName(params.name);
|
||||||
|
|
||||||
|
if (post_id === null) {
|
||||||
|
error(404, `No Post of ID ${params.name}`);
|
||||||
|
}
|
||||||
|
|
||||||
const post = await getPost(locals.sql, post_id);
|
const post = await getPost(locals.sql, post_id);
|
||||||
|
|
||||||
@ -1,6 +1,8 @@
|
|||||||
<script>
|
<script>
|
||||||
import Comment from "$comp/comment.svelte";
|
import Comment from "$comp/comment.svelte";
|
||||||
import { buildCommentTree } from "$lib/client/nodetree";
|
import { buildCommentTree } from "$lib/client/nodetree";
|
||||||
|
import { getNamedId, gotoNamedId } from "$lib/util";
|
||||||
|
import { onMount } from "svelte";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {{
|
* @type {{
|
||||||
@ -10,18 +12,22 @@
|
|||||||
*/
|
*/
|
||||||
export let data;
|
export let data;
|
||||||
|
|
||||||
console.log(data);
|
$: post = data.post;
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {import('$types/base').CommentTreeNode[]}
|
|
||||||
*/
|
|
||||||
let commentTree;
|
|
||||||
$: commentTree = buildCommentTree(data.comments);
|
$: commentTree = buildCommentTree(data.comments);
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
gotoNamedId(post.id, post.name);
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>{data.post.name}</h1>
|
<h1>{post.name}</h1>
|
||||||
<a href="#">{data.post.author?.name}</a>
|
{#if post.author}
|
||||||
<p>{data.post.content}</p>
|
<a href="/users/{getNamedId(post.author.id, post.author.name)}">{post.author.name}</a>
|
||||||
|
{:else}
|
||||||
|
<p>Deleted</p>
|
||||||
|
{/if}
|
||||||
|
<p>{post.content}</p>
|
||||||
<div>
|
<div>
|
||||||
{#each commentTree as reply}
|
{#each commentTree as reply}
|
||||||
<Comment commentNode={reply}></Comment>
|
<Comment commentNode={reply}></Comment>
|
||||||
Loading…
x
Reference in New Issue
Block a user