update: changes to the backed endpoint post and patch due to the addition of validation so that questions cannot be duplicated in question management
This commit is contained in:
parent
7dd5b41b40
commit
1e3d112e0f
|
|
@ -1,6 +1,5 @@
|
|||
import { and, eq, ilike, isNull, or, sql } from "drizzle-orm";
|
||||
import { and, eq, ne, ilike, isNull, or, sql } from "drizzle-orm";
|
||||
import { Hono } from "hono";
|
||||
|
||||
import { z } from "zod";
|
||||
import { HTTPException } from "hono/http-exception";
|
||||
import db from "../../drizzle";
|
||||
|
|
@ -280,6 +279,24 @@ const questionsRoute = new Hono<HonoEnv>()
|
|||
return c.json({ message: "Sub aspect not found" }, 404);
|
||||
}
|
||||
|
||||
// Cek apakah question dengan subAspectId yang sama sudah ada
|
||||
const duplicateQuestion = await db
|
||||
.select()
|
||||
.from(questions)
|
||||
.where(
|
||||
and(
|
||||
eq(questions.subAspectId, questionData.subAspectId),
|
||||
eq(questions.question, questionData.question)
|
||||
)
|
||||
);
|
||||
|
||||
if (duplicateQuestion.length > 0) {
|
||||
return c.json(
|
||||
{ message: "Pertanyaan dengan sub-aspek yang sama sudah ada" },
|
||||
409
|
||||
);
|
||||
}
|
||||
|
||||
// Insert question data into the questions table
|
||||
const question = await db
|
||||
.insert(questions)
|
||||
|
|
@ -294,17 +311,16 @@ const questionsRoute = new Hono<HonoEnv>()
|
|||
|
||||
// Insert options data if provided
|
||||
if (questionData.options && questionData.options.length > 0) {
|
||||
const optionsData = questionData.options.map((option) => ({
|
||||
questionId: questionId,
|
||||
text: option.text,
|
||||
score: option.score,
|
||||
const optionsData = questionData.options.map((option) => ({
|
||||
questionId: questionId,
|
||||
text: option.text,
|
||||
score: option.score,
|
||||
}));
|
||||
|
||||
await db.insert(options).values(optionsData);
|
||||
}
|
||||
|
||||
return c.json(
|
||||
{
|
||||
return c.json({
|
||||
message: "Question and options created successfully",
|
||||
data: question[0],
|
||||
},
|
||||
|
|
@ -333,6 +349,26 @@ const questionsRoute = new Hono<HonoEnv>()
|
|||
.where(and(eq(questions.id, questionId), isNull(questions.deletedAt)));
|
||||
|
||||
if (!question[0]) throw notFound();
|
||||
|
||||
// Check if the combination of subAspectId and question already exists (except for this question)
|
||||
const duplicateQuestion = await db
|
||||
.select()
|
||||
.from(questions)
|
||||
.where(
|
||||
and(
|
||||
eq(questions.subAspectId, questionData.subAspectId),
|
||||
eq(questions.question, questionData.question),
|
||||
ne(questions.id, questionId), // Ignore questions that are being updated
|
||||
isNull(questions.deletedAt)
|
||||
)
|
||||
);
|
||||
|
||||
if (duplicateQuestion.length > 0) {
|
||||
return c.json(
|
||||
{ message: "Pertanyaan dengan sub-aspek yang sama sudah ada" },
|
||||
409
|
||||
);
|
||||
}
|
||||
|
||||
// Update question data
|
||||
await db
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user