update homepage draft and other pages

This commit is contained in:
2026-08-29 11:38:16 +00:00
parent 6452331ee7
commit 97ff9852cd
38 changed files with 2185 additions and 111 deletions

View File

@@ -0,0 +1,128 @@
import { db } from '$lib/db';
import { tasks, tasksToTopics, topics } from '$lib/db/schema';
import { eq, and } from 'drizzle-orm';
export const load = async ({ cookies, params }) => {
return {
task: await db.query.tasks.findFirst({
with: {
tasksToTopics: {
with: {
topic: true
}
}
},
where: eq(tasks.id, params.id)
}),
topics: await db.query.topics.findMany(),
editable: cookies.get('token')
};
};
export const actions = {
createTopic: async ({ cookies, request, params }) => {
if (!cookies.get('token')) {
return { status: 401 };
}
const data = await request.formData();
const topic = await db
.insert(topics)
.values({ name: data.get('name') })
.returning();
const existing = await db.query.tasksToTopics.findFirst({
where: and(
eq(tasksToTopics.taskId, Number(params.id)),
eq(tasksToTopics.topicId, topic[0].id)
)
});
if (!existing) {
await db
.insert(tasksToTopics)
.values({ taskId: Number(params.id), topicId: topic[0].id })
.returning();
}
},
addTopic: async ({ cookies, request, params }) => {
if (!cookies.get('token')) {
return { status: 401 };
}
const data = await request.formData();
const topicId = Number(data.get('topic-id'));
const existing = await db.query.tasksToTopics.findFirst({
where: and(eq(tasksToTopics.taskId, Number(params.id)), eq(tasksToTopics.topicId, topicId))
});
console.log(existing);
if (!existing) {
await db
.insert(tasksToTopics)
.values({ taskId: Number(params.id), topicId })
.returning();
}
},
removeTopic: async ({ cookies, request, params }) => {
if (!cookies.get('token')) {
return { status: 401 };
}
const data = await request.formData();
const topicId = Number(data.get('topic-id'));
const existing = await db.query.tasksToTopics.findFirst({
where: and(eq(tasksToTopics.taskId, Number(params.id)), eq(tasksToTopics.topicId, topicId))
});
if (existing) {
await db
.delete(tasksToTopics)
.where(
and(eq(tasksToTopics.taskId, Number(params.id)), eq(tasksToTopics.topicId, topicId))
);
}
},
updateProgress: async ({ cookies, request, params }) => {
if (!cookies.get('token')) {
return { status: 401 };
}
const data = await request.formData();
const currentPoints =
Number(data.get('currentPoints')) <= Number(data.get('totalPoints'))
? Number(data.get('currentPoints'))
: Number(data.get('totalPoints'));
await db
.update(tasks)
.set({
currentPoints,
totalPoints: Number(data.get('totalPoints')),
updatedAt: new Date()
})
.where(eq(tasks.id, Number(params.id)));
},
updateNotes: async ({ cookies, request, params }) => {
if (!cookies.get('token')) {
return { status: 401 };
}
const data = await request.formData();
await db
.update(tasks)
.set({
notes: data.get('notes') as string,
updatedAt: new Date()
})
.where(eq(tasks.id, Number(params.id)));
}
};