Admin done
This commit is contained in:
+47
-2
@@ -1,7 +1,8 @@
|
||||
"use server";
|
||||
|
||||
import { eq, inArray } from "drizzle-orm";
|
||||
import { eq, inArray, not } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { apiAuthCheck } from "./auth";
|
||||
import { uidRegex } from "./const";
|
||||
import { db } from "./db";
|
||||
import {
|
||||
@@ -34,7 +35,7 @@ export async function submitArtifact(formData: FormData) {
|
||||
if (!name || !uid || !character) throw "กรุณากรอกข้อมูลให้ครบถ้วน";
|
||||
if (name.length > 32) throw "ชื่อยาวเกินไป ต้องไม่เกิน 32 ตัวอักษร";
|
||||
if (!uidRegex.test(uid)) throw "UID ไม่ถูกต้อง ต้องเป็นเลข 9 หลัก";
|
||||
if (comment.length > 256) throw "ข้อความเพิ่มเติมยาวเกินไป ต้องไม่เกิน 256 ตัวอักษร";
|
||||
if (comment.length > 256) throw "ข้อความเพิ่มเติมยาวเกินไป ต้องไม่เกิน 512 ตัวอักษร";
|
||||
if (config.locked) throw "ปิดรับลงทะเบียนชั่วคราว เนื่องจากมีผู้ลงจำนวนมาก";
|
||||
const count = await db.$count(submissions);
|
||||
if (config.limit !== -1 && count >= config.limit)
|
||||
@@ -63,3 +64,47 @@ export async function submitArtifact(formData: FormData) {
|
||||
revalidatePath("/artifact/admin");
|
||||
return queue;
|
||||
}
|
||||
|
||||
export async function toggleCheck(submissionId: string) {
|
||||
if (!(await apiAuthCheck())) throw "Unauthorized";
|
||||
await db
|
||||
.update(submissions)
|
||||
.set({
|
||||
checked: not(submissions.checked),
|
||||
})
|
||||
.where(eq(submissions.id, submissionId));
|
||||
revalidatePath("/artifact/admin");
|
||||
}
|
||||
|
||||
export async function toggleLock() {
|
||||
if (!(await apiAuthCheck())) throw "Unauthorized";
|
||||
if (
|
||||
(
|
||||
await db
|
||||
.update(artifactSettings)
|
||||
.set({
|
||||
locked: not(artifactSettings.locked),
|
||||
})
|
||||
.returning({ id: artifactSettings.id })
|
||||
).length === 0
|
||||
)
|
||||
await db.insert(artifactSettings).values({ locked: true });
|
||||
revalidatePath("/artifact/admin");
|
||||
}
|
||||
|
||||
export async function setLimit(limit: number) {
|
||||
if (!(await apiAuthCheck())) throw "Unauthorized";
|
||||
|
||||
if (
|
||||
(
|
||||
await db
|
||||
.update(artifactSettings)
|
||||
.set({
|
||||
limit,
|
||||
})
|
||||
.returning({ id: artifactSettings.id })
|
||||
).length === 0
|
||||
)
|
||||
await db.insert(artifactSettings).values({ limit });
|
||||
revalidatePath("/artifact/admin");
|
||||
}
|
||||
|
||||
+13
-2
@@ -1,5 +1,6 @@
|
||||
import { betterAuth } from "better-auth";
|
||||
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
||||
import { headers } from "next/headers";
|
||||
import { db } from "$/db"; // your drizzle instance
|
||||
import * as schema from "$/db/schema";
|
||||
|
||||
@@ -9,6 +10,16 @@ export const auth = betterAuth({
|
||||
},
|
||||
database: drizzleAdapter(db, {
|
||||
provider: "pg", // or "mysql", "sqlite"
|
||||
schema
|
||||
schema,
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
export async function apiAuthCheck() {
|
||||
"use server";
|
||||
|
||||
const session = await auth.api.getSession({
|
||||
headers: await headers(),
|
||||
});
|
||||
|
||||
return session.user;
|
||||
}
|
||||
@@ -78,6 +78,7 @@ export const submissions = artifact.table("submissions", {
|
||||
.notNull()
|
||||
.references(() => characters.name),
|
||||
queue: serial(),
|
||||
checked: boolean().notNull().default(false),
|
||||
});
|
||||
|
||||
export const artifactSettings = artifact.table("settings", {
|
||||
|
||||
Reference in New Issue
Block a user