mirror of
https://github.com/thiloho/archtika.git
synced 2025-11-22 10:51:36 +01:00
Add basic forms and routes
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { extname, join, relative } from "node:path";
|
||||
import { ALLOWED_MIME_TYPES } from "$lib/utils.js";
|
||||
import { handleFileUpload } from "$lib/server/utils.js";
|
||||
|
||||
export const load = async ({ params, fetch, cookies, url }) => {
|
||||
const globalSettingsData = await fetch(
|
||||
@@ -28,15 +25,6 @@ export const load = async ({ params, fetch, cookies, url }) => {
|
||||
}
|
||||
);
|
||||
|
||||
const homeData = await fetch(`http://localhost:3000/home?website_id=eq.${params.websiteId}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`,
|
||||
Accept: "application/vnd.pgrst.object+json"
|
||||
}
|
||||
});
|
||||
|
||||
const footerData = await fetch(`http://localhost:3000/footer?website_id=eq.${params.websiteId}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
@@ -46,54 +34,14 @@ export const load = async ({ params, fetch, cookies, url }) => {
|
||||
}
|
||||
});
|
||||
|
||||
const searchQuery = url.searchParams.get("article_search_query");
|
||||
const sortBy = url.searchParams.get("article_sort");
|
||||
|
||||
const parameters = new URLSearchParams();
|
||||
|
||||
const baseFetchUrl = `http://localhost:3000/article?website_id=eq.${params.websiteId}&select=*,media(*)`;
|
||||
|
||||
if (searchQuery) {
|
||||
parameters.append("title", `ilike.*${searchQuery}*`);
|
||||
}
|
||||
|
||||
switch (sortBy) {
|
||||
case "creation-time":
|
||||
parameters.append("order", "created_at.desc");
|
||||
break;
|
||||
case "last-modified":
|
||||
parameters.append("order", "last_modified_at.desc");
|
||||
break;
|
||||
case "title-a-to-z":
|
||||
parameters.append("order", "title.asc");
|
||||
break;
|
||||
case "title-z-to-a":
|
||||
parameters.append("order", "title.desc");
|
||||
break;
|
||||
}
|
||||
|
||||
const constructedFetchUrl = `${baseFetchUrl}&${parameters.toString()}`;
|
||||
|
||||
const articlesData = await fetch(constructedFetchUrl, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`
|
||||
}
|
||||
});
|
||||
|
||||
const globalSettings = await globalSettingsData.json();
|
||||
const header = await headerData.json();
|
||||
const home = await homeData.json();
|
||||
const footer = await footerData.json();
|
||||
const articles = await articlesData.json();
|
||||
|
||||
return {
|
||||
globalSettings,
|
||||
header,
|
||||
home,
|
||||
footer,
|
||||
articles
|
||||
footer
|
||||
};
|
||||
};
|
||||
|
||||
@@ -134,8 +82,7 @@ export const actions = {
|
||||
|
||||
return {
|
||||
success: true,
|
||||
operation: "updated",
|
||||
ressource: "global settings"
|
||||
message: "Successfully updated global settings"
|
||||
};
|
||||
},
|
||||
updateHeader: async ({ request, fetch, cookies, locals, params }) => {
|
||||
@@ -174,8 +121,7 @@ export const actions = {
|
||||
|
||||
return {
|
||||
success: true,
|
||||
operation: "updated",
|
||||
ressource: "header settings"
|
||||
message: "Successfully updated header"
|
||||
};
|
||||
},
|
||||
updateHome: async ({ request, fetch, cookies, params }) => {
|
||||
@@ -197,7 +143,7 @@ export const actions = {
|
||||
return { success: false, message: response.message };
|
||||
}
|
||||
|
||||
return { success: true, operation: "updated", ressource: "home settings" };
|
||||
return { success: true, message: "Successfully updated home" };
|
||||
},
|
||||
updateFooter: async ({ request, fetch, cookies, params }) => {
|
||||
const data = await request.formData();
|
||||
@@ -220,149 +166,7 @@ export const actions = {
|
||||
|
||||
return {
|
||||
success: true,
|
||||
operation: "updated",
|
||||
ressource: "footer settings"
|
||||
message: "Successfully updated footer"
|
||||
};
|
||||
},
|
||||
createArticle: async ({ request, fetch, cookies, params }) => {
|
||||
const data = await request.formData();
|
||||
|
||||
const res = await fetch("http://localhost:3000/article", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
website_id: params.websiteId,
|
||||
title: data.get("title")
|
||||
})
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const response = await res.json();
|
||||
return { success: false, message: response.message };
|
||||
}
|
||||
|
||||
return { success: true, operation: "created", ressource: "article" };
|
||||
},
|
||||
editArticle: async ({ request, fetch, cookies, locals, params }) => {
|
||||
const data = await request.formData();
|
||||
|
||||
const coverFile = data.get("cover-image") as File;
|
||||
const cover = await handleFileUpload(
|
||||
coverFile,
|
||||
params.websiteId,
|
||||
locals.user.id,
|
||||
cookies.get("session_token"),
|
||||
fetch
|
||||
);
|
||||
|
||||
if (cover?.success === false) {
|
||||
return cover;
|
||||
}
|
||||
|
||||
const res = await fetch(`http://localhost:3000/article?id=eq.${data.get("article-id")}`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
title: data.get("title"),
|
||||
meta_description: data.get("description"),
|
||||
meta_author: data.get("author"),
|
||||
cover_image: cover?.content,
|
||||
publication_date: data.get("publication-date"),
|
||||
main_content: data.get("main-content")
|
||||
})
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const response = await res.json();
|
||||
return { success: false, message: response.message };
|
||||
}
|
||||
|
||||
return { success: true, operation: "updated", ressource: "article" };
|
||||
},
|
||||
deleteArticle: async ({ request, fetch, cookies }) => {
|
||||
const data = await request.formData();
|
||||
|
||||
const res = await fetch(`http://localhost:3000/article?id=eq.${data.get("article-id")}`, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`
|
||||
}
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const response = await res.json();
|
||||
return { success: false, message: response.message };
|
||||
}
|
||||
|
||||
return { success: true, operation: "deleted", ressource: "article" };
|
||||
}
|
||||
};
|
||||
|
||||
const handleFileUpload = async (
|
||||
file: File,
|
||||
contentId: string,
|
||||
userId: string,
|
||||
session_token: string | undefined,
|
||||
customFetch: typeof fetch
|
||||
) => {
|
||||
if (file.size === 0) return undefined;
|
||||
|
||||
const MAX_FILE_SIZE = 1024 * 1024;
|
||||
|
||||
if (file.size > MAX_FILE_SIZE) {
|
||||
return {
|
||||
success: false,
|
||||
message: `File size exceeds the maximum limit of ${MAX_FILE_SIZE / 1024 / 1024} MB.`
|
||||
};
|
||||
}
|
||||
|
||||
if (!ALLOWED_MIME_TYPES.includes(file.type)) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Invalid file type. JPEG, PNG, SVG and WEBP are allowed."
|
||||
};
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
const uploadDir = join(process.cwd(), "static", "user-uploads", userId);
|
||||
await mkdir(uploadDir, { recursive: true });
|
||||
|
||||
const fileId = randomUUID();
|
||||
const fileExtension = extname(file.name);
|
||||
const filepath = join(uploadDir, `${fileId}${fileExtension}`);
|
||||
|
||||
await writeFile(filepath, buffer);
|
||||
|
||||
const relativePath = relative(join(process.cwd(), "static"), filepath);
|
||||
|
||||
const res = await customFetch("http://localhost:3000/media", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${session_token}`,
|
||||
Prefer: "return=representation",
|
||||
Accept: "application/vnd.pgrst.object+json"
|
||||
},
|
||||
body: JSON.stringify({
|
||||
website_id: contentId,
|
||||
user_id: userId,
|
||||
original_name: file.name,
|
||||
file_system_path: relativePath
|
||||
})
|
||||
});
|
||||
|
||||
const response = await res.json();
|
||||
|
||||
if (!res.ok) {
|
||||
return { success: false, message: response.message };
|
||||
}
|
||||
|
||||
return { success: true, content: response.id };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user