實作邏輯題結果與資料庫整合
This commit is contained in:
@@ -84,13 +84,13 @@ export async function findLogicQuestionById(id: number): Promise<LogicQuestion |
|
||||
// 獲取所有題目
|
||||
export async function getAllLogicQuestions(): Promise<LogicQuestion[]> {
|
||||
const query = 'SELECT * FROM logic_questions ORDER BY created_at'
|
||||
return await executeQuery<LogicQuestion[]>(query)
|
||||
return await executeQuery<LogicQuestion>(query)
|
||||
}
|
||||
|
||||
// 獲取隨機題目
|
||||
export async function getRandomLogicQuestions(limit: number = 10): Promise<LogicQuestion[]> {
|
||||
const query = 'SELECT * FROM logic_questions ORDER BY RAND() LIMIT ?'
|
||||
return await executeQuery<LogicQuestion[]>(query, [limit])
|
||||
return await executeQuery<LogicQuestion>(query, [limit])
|
||||
}
|
||||
|
||||
// 清空所有題目
|
||||
|
140
lib/database/models/logic_test_answer.ts
Normal file
140
lib/database/models/logic_test_answer.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import { executeQuery, executeQueryOne } from '../connection'
|
||||
|
||||
export interface LogicTestAnswer {
|
||||
id: string
|
||||
test_result_id: string
|
||||
question_id: number
|
||||
user_answer: 'A' | 'B' | 'C' | 'D' | 'E'
|
||||
is_correct: boolean
|
||||
created_at?: string
|
||||
}
|
||||
|
||||
export interface CreateLogicTestAnswerData {
|
||||
test_result_id: string
|
||||
question_id: number
|
||||
user_answer: 'A' | 'B' | 'C' | 'D' | 'E'
|
||||
is_correct: boolean
|
||||
}
|
||||
|
||||
// 建立邏輯測驗答案表(如果不存在)
|
||||
export async function createLogicTestAnswersTable(): Promise<void> {
|
||||
const createTableQuery = `
|
||||
CREATE TABLE IF NOT EXISTS logic_test_answers (
|
||||
id VARCHAR(50) PRIMARY KEY,
|
||||
test_result_id VARCHAR(50) NOT NULL,
|
||||
question_id INT NOT NULL,
|
||||
user_answer ENUM('A', 'B', 'C', 'D', 'E') NOT NULL,
|
||||
is_correct TINYINT(1) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
INDEX idx_test_result_id (test_result_id),
|
||||
INDEX idx_question_id (question_id),
|
||||
FOREIGN KEY (test_result_id) REFERENCES test_results(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (question_id) REFERENCES logic_questions(id) ON DELETE CASCADE
|
||||
)
|
||||
`
|
||||
|
||||
await executeQuery(createTableQuery)
|
||||
console.log('✅ 邏輯測驗答案表建立成功')
|
||||
}
|
||||
|
||||
// 建立新邏輯測驗答案
|
||||
export async function createLogicTestAnswer(answerData: CreateLogicTestAnswerData): Promise<LogicTestAnswer | null> {
|
||||
try {
|
||||
const id = `answer_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
|
||||
|
||||
const insertQuery = `
|
||||
INSERT INTO logic_test_answers (
|
||||
id, test_result_id, question_id, user_answer, is_correct
|
||||
) VALUES (?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
await executeQuery(insertQuery, [
|
||||
id,
|
||||
answerData.test_result_id,
|
||||
answerData.question_id,
|
||||
answerData.user_answer,
|
||||
answerData.is_correct ? 1 : 0
|
||||
])
|
||||
|
||||
return await getLogicTestAnswerById(id)
|
||||
} catch (error) {
|
||||
console.error('建立邏輯測驗答案失敗:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// 批量建立邏輯測驗答案
|
||||
export async function createLogicTestAnswers(answersData: CreateLogicTestAnswerData[]): Promise<LogicTestAnswer[]> {
|
||||
try {
|
||||
const results: LogicTestAnswer[] = []
|
||||
|
||||
for (const answerData of answersData) {
|
||||
const result = await createLogicTestAnswer(answerData)
|
||||
if (result) {
|
||||
results.push(result)
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
} catch (error) {
|
||||
console.error('批量建立邏輯測驗答案失敗:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
// 根據ID獲取邏輯測驗答案
|
||||
export async function getLogicTestAnswerById(id: string): Promise<LogicTestAnswer | null> {
|
||||
try {
|
||||
const query = 'SELECT * FROM logic_test_answers WHERE id = ?'
|
||||
return await executeQueryOne<LogicTestAnswer>(query, [id])
|
||||
} catch (error) {
|
||||
console.error('獲取邏輯測驗答案失敗:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// 根據測試結果ID獲取所有答案
|
||||
export async function getLogicTestAnswersByTestResultId(testResultId: string): Promise<LogicTestAnswer[]> {
|
||||
try {
|
||||
const query = 'SELECT * FROM logic_test_answers WHERE test_result_id = ? ORDER BY question_id'
|
||||
return await executeQuery<LogicTestAnswer>(query, [testResultId])
|
||||
} catch (error) {
|
||||
console.error('獲取測試結果答案失敗:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
// 根據問題ID獲取所有答案
|
||||
export async function getLogicTestAnswersByQuestionId(questionId: number): Promise<LogicTestAnswer[]> {
|
||||
try {
|
||||
const query = 'SELECT * FROM logic_test_answers WHERE question_id = ? ORDER BY created_at DESC'
|
||||
return await executeQuery<LogicTestAnswer>(query, [questionId])
|
||||
} catch (error) {
|
||||
console.error('獲取問題答案失敗:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
// 刪除測試結果的所有答案
|
||||
export async function deleteLogicTestAnswersByTestResultId(testResultId: string): Promise<boolean> {
|
||||
try {
|
||||
const query = 'DELETE FROM logic_test_answers WHERE test_result_id = ?'
|
||||
await executeQuery(query, [testResultId])
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('刪除測試結果答案失敗:', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// 刪除邏輯測驗答案
|
||||
export async function deleteLogicTestAnswer(id: string): Promise<boolean> {
|
||||
try {
|
||||
const query = 'DELETE FROM logic_test_answers WHERE id = ?'
|
||||
await executeQuery(query, [id])
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('刪除邏輯測驗答案失敗:', error)
|
||||
return false
|
||||
}
|
||||
}
|
130
lib/database/models/test_result.ts
Normal file
130
lib/database/models/test_result.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { executeQuery, executeQueryOne } from '../connection'
|
||||
|
||||
export interface TestResult {
|
||||
id: string
|
||||
user_id: string
|
||||
test_type: 'logic' | 'creative' | 'combined'
|
||||
score: number
|
||||
total_questions: number
|
||||
correct_answers: number
|
||||
completed_at: string
|
||||
created_at?: string
|
||||
}
|
||||
|
||||
export interface CreateTestResultData {
|
||||
user_id: string
|
||||
test_type: 'logic' | 'creative' | 'combined'
|
||||
score: number
|
||||
total_questions: number
|
||||
correct_answers: number
|
||||
completed_at: string
|
||||
}
|
||||
|
||||
// 建立測試結果表(如果不存在)
|
||||
export async function createTestResultsTable(): Promise<void> {
|
||||
const createTableQuery = `
|
||||
CREATE TABLE IF NOT EXISTS test_results (
|
||||
id VARCHAR(50) PRIMARY KEY,
|
||||
user_id VARCHAR(50) NOT NULL,
|
||||
test_type ENUM('logic', 'creative', 'combined') NOT NULL,
|
||||
score INT NOT NULL,
|
||||
total_questions INT NOT NULL,
|
||||
correct_answers INT NOT NULL,
|
||||
completed_at TIMESTAMP NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
INDEX idx_user_id (user_id),
|
||||
INDEX idx_test_type (test_type),
|
||||
INDEX idx_completed_at (completed_at)
|
||||
)
|
||||
`
|
||||
|
||||
await executeQuery(createTableQuery)
|
||||
console.log('✅ 測試結果表建立成功')
|
||||
}
|
||||
|
||||
// 建立新測試結果
|
||||
export async function createTestResult(resultData: CreateTestResultData): Promise<TestResult | null> {
|
||||
try {
|
||||
const id = `test_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
|
||||
|
||||
const insertQuery = `
|
||||
INSERT INTO test_results (
|
||||
id, user_id, test_type, score, total_questions,
|
||||
correct_answers, completed_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
await executeQuery(insertQuery, [
|
||||
id,
|
||||
resultData.user_id,
|
||||
resultData.test_type,
|
||||
resultData.score,
|
||||
resultData.total_questions,
|
||||
resultData.correct_answers,
|
||||
resultData.completed_at
|
||||
])
|
||||
|
||||
const result = await getTestResultById(id)
|
||||
console.log('建立的測試結果:', result)
|
||||
return result
|
||||
} catch (error) {
|
||||
console.error('建立測試結果失敗:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// 根據ID獲取測試結果
|
||||
export async function getTestResultById(id: string): Promise<TestResult | null> {
|
||||
try {
|
||||
const query = 'SELECT * FROM test_results WHERE id = ?'
|
||||
return await executeQueryOne<TestResult>(query, [id])
|
||||
} catch (error) {
|
||||
console.error('獲取測試結果失敗:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// 根據用戶ID獲取測試結果
|
||||
export async function getTestResultsByUserId(userId: string): Promise<TestResult[]> {
|
||||
try {
|
||||
const query = 'SELECT * FROM test_results WHERE user_id = ? ORDER BY completed_at DESC'
|
||||
return await executeQuery<TestResult>(query, [userId])
|
||||
} catch (error) {
|
||||
console.error('獲取用戶測試結果失敗:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
// 根據測試類型獲取測試結果
|
||||
export async function getTestResultsByType(testType: 'logic' | 'creative' | 'combined'): Promise<TestResult[]> {
|
||||
try {
|
||||
const query = 'SELECT * FROM test_results WHERE test_type = ? ORDER BY completed_at DESC'
|
||||
return await executeQuery<TestResult>(query, [testType])
|
||||
} catch (error) {
|
||||
console.error('獲取測試結果失敗:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
// 獲取所有測試結果
|
||||
export async function getAllTestResults(): Promise<TestResult[]> {
|
||||
try {
|
||||
const query = 'SELECT * FROM test_results ORDER BY completed_at DESC'
|
||||
return await executeQuery<TestResult>(query)
|
||||
} catch (error) {
|
||||
console.error('獲取所有測試結果失敗:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
// 刪除測試結果
|
||||
export async function deleteTestResult(id: string): Promise<boolean> {
|
||||
try {
|
||||
const query = 'DELETE FROM test_results WHERE id = ?'
|
||||
await executeQuery(query, [id])
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('刪除測試結果失敗:', error)
|
||||
return false
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user