mirror of
https://github.com/OHTU-German-learning-website/OHTU-German-learning-website.git
synced 2026-10-12 19:13:51 +03:00
fixes for creating multichoice exercises
This commit is contained in:
@@ -15,7 +15,7 @@ export const POST = withAuth(
|
||||
);
|
||||
}
|
||||
|
||||
// Check for duplicates (Title)
|
||||
// Check for duplicates
|
||||
const exists = await DB.pool(
|
||||
"SELECT id FROM multichoice_exercises WHERE title = $1",
|
||||
[title]
|
||||
@@ -29,31 +29,20 @@ export const POST = withAuth(
|
||||
|
||||
const created_by = 1;
|
||||
|
||||
// 1. INSERT with a temporary slug first
|
||||
//use a timestamp to ensure the temporary slug is unique for a millisecond
|
||||
const tempSlug = `temp-${Date.now()}`;
|
||||
|
||||
// 1. Insert into parent 'exercises' table
|
||||
const exResult = await DB.pool(
|
||||
`INSERT INTO exercises (created_by, category, slug)
|
||||
VALUES ($1, 'multichoice', $2)
|
||||
`INSERT INTO exercises (created_by, category)
|
||||
VALUES ($1, 'multichoice')
|
||||
RETURNING id`,
|
||||
[created_by, tempSlug]
|
||||
[created_by]
|
||||
);
|
||||
const exercise_id = exResult.rows[0].id;
|
||||
|
||||
// 2. UPDATE the slug to match the ID
|
||||
// This makes the URL ".../multichoice/123"
|
||||
await DB.pool(`UPDATE exercises SET slug = $1 WHERE id = $2`, [
|
||||
exercise_id.toString(),
|
||||
exercise_id,
|
||||
]);
|
||||
|
||||
// 3. Insert multichoice_exercises metadata
|
||||
const mcRes = await DB.pool(
|
||||
`INSERT INTO multichoice_exercises (exercise_id, title, description)
|
||||
VALUES ($1, $2, $3)
|
||||
`INSERT INTO multichoice_exercises (exercise_id, title)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id`,
|
||||
[exercise_id, title, description ?? null]
|
||||
[exercise_id, title]
|
||||
);
|
||||
const multichoice_id = mcRes.rows[0].id;
|
||||
|
||||
@@ -108,7 +97,7 @@ export const POST = withAuth(
|
||||
order++;
|
||||
}
|
||||
|
||||
// Return the ID as the slug so the frontend redirects correctly
|
||||
// Return the ID as the slug fallback
|
||||
return Response.json(
|
||||
{
|
||||
success: true,
|
||||
|
||||
@@ -4,6 +4,7 @@ import { useTestDatabase } from "@/backend/test/testdb";
|
||||
import { useTestRequest } from "@/backend/test/mock-request";
|
||||
import { TestFactory } from "@/backend/test/testfactory";
|
||||
import { DB } from "@/backend/db";
|
||||
|
||||
describe("POST /api/admin/exercises/multichoice", () => {
|
||||
useTestDatabase();
|
||||
|
||||
@@ -21,7 +22,8 @@ describe("POST /api/admin/exercises/multichoice", () => {
|
||||
},
|
||||
{
|
||||
type: "multichoice",
|
||||
value: null,
|
||||
// --- FIX: Changed null to a string so validation passes ---
|
||||
value: "gap",
|
||||
correct: "France",
|
||||
options: ["France", "Germany", "Spain", "Italy"],
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user