Pull Request branch dev-clone to main #1

Merged
gitea merged 429 commits from dev-clone into main 2024-12-23 09:31:34 +00:00
Showing only changes of commit 1e3d112e0f - Show all commits

View File

@ -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 { Hono } from "hono";
import { z } from "zod"; import { z } from "zod";
import { HTTPException } from "hono/http-exception"; import { HTTPException } from "hono/http-exception";
import db from "../../drizzle"; import db from "../../drizzle";
@ -280,6 +279,24 @@ const questionsRoute = new Hono<HonoEnv>()
return c.json({ message: "Sub aspect not found" }, 404); 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 // Insert question data into the questions table
const question = await db const question = await db
.insert(questions) .insert(questions)
@ -294,17 +311,16 @@ const questionsRoute = new Hono<HonoEnv>()
// Insert options data if provided // Insert options data if provided
if (questionData.options && questionData.options.length > 0) { if (questionData.options && questionData.options.length > 0) {
const optionsData = questionData.options.map((option) => ({ const optionsData = questionData.options.map((option) => ({
questionId: questionId, questionId: questionId,
text: option.text, text: option.text,
score: option.score, score: option.score,
})); }));
await db.insert(options).values(optionsData); await db.insert(options).values(optionsData);
} }
return c.json( return c.json({
{
message: "Question and options created successfully", message: "Question and options created successfully",
data: question[0], data: question[0],
}, },
@ -333,6 +349,26 @@ const questionsRoute = new Hono<HonoEnv>()
.where(and(eq(questions.id, questionId), isNull(questions.deletedAt))); .where(and(eq(questions.id, questionId), isNull(questions.deletedAt)));
if (!question[0]) throw notFound(); 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 // Update question data
await db await db