Refactor: Remove SQL from Locals

This commit is contained in:
2024-05-16 09:59:12 +03:00
parent 6e0e91b0ce
commit bc04506fe1
9 changed files with 54 additions and 38 deletions
+3 -3
View File
@@ -2,13 +2,13 @@ import { getPost, getPosts } from '$lib/server/db/post';
/** @type {import('./$types').PageServerLoad} */
export async function load({ locals, url }) {
const result = await getPosts(locals.sql);
export async function load({ url }) {
const result = await getPosts();
const glance = url.searchParams.get('glance');
const glanceID = glance ? parseInt(glance) : null
const glancePost = glanceID ? await getPost(locals.sql, glanceID, { withMetrics: true }) : null;
const glancePost = glanceID ? await getPost(glanceID, { withMetrics: true }) : null;
return {
posts: result,
@@ -4,16 +4,16 @@ import { getIdFromName } from "$lib/util";
import { error } from "@sveltejs/kit";
/** @type {import("@sveltejs/kit").ServerLoad} */
export async function load({ params, locals }) {
export async function load({ params }) {
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(post_id);
const comments = await getCommentsForPost(locals.sql, post_id);
const comments = await getCommentsForPost(post_id);
return {
post: post,
@@ -3,14 +3,14 @@ import { getIdFromName } from "$lib/util";
import { error } from "@sveltejs/kit";
/** @type {import("@sveltejs/kit").ServerLoad} */
export async function load({ locals, params }) {
export async function load({ params }) {
const user_id = getIdFromName(params.name);
if (user_id === null) {
return error(404, `Invalid Name ${params.name}`);
}
const user = await getUser(locals.sql, user_id);
const user = await getUser(user_id);
if ('error' in user) {
return error(404, `No User of ID ${user_id}`);