import { execute, queryOne } from '../../../utils/db' interface UpdateTodoBody { todoTitle?: string todoContent?: string projectId?: number | null assigneeId?: number | null dueDate?: string | null status?: string priority?: number } /** * TODO 수정 * PUT /api/todo/[id]/update */ export default defineEventHandler(async (event) => { const todoId = Number(getRouterParam(event, 'id')) const body = await readBody(event) const existing = await queryOne('SELECT * FROM wr_todo WHERE todo_id = $1', [todoId]) if (!existing) { throw createError({ statusCode: 404, message: 'TODO를 찾을 수 없습니다.' }) } const updates: string[] = [] const values: any[] = [] let paramIndex = 1 if (body.todoTitle !== undefined) { updates.push(`todo_title = $${paramIndex++}`) values.push(body.todoTitle) } if (body.todoContent !== undefined) { updates.push(`todo_content = $${paramIndex++}`) values.push(body.todoContent) } if (body.projectId !== undefined) { updates.push(`project_id = $${paramIndex++}`) values.push(body.projectId) } if (body.assigneeId !== undefined) { updates.push(`assignee_id = $${paramIndex++}`) values.push(body.assigneeId) } if (body.dueDate !== undefined) { updates.push(`due_date = $${paramIndex++}`) values.push(body.dueDate) } if (body.status !== undefined) { updates.push(`status = $${paramIndex++}`) values.push(body.status) // 완료 상태로 변경 시 완료일시 기록 if (body.status === 'COMPLETED') { updates.push(`completed_at = NOW()`) } else if (existing.status === 'COMPLETED') { updates.push(`completed_at = NULL`) } } if (body.priority !== undefined) { updates.push(`priority = $${paramIndex++}`) values.push(body.priority) } if (updates.length === 0) { return { success: true, message: '변경된 내용이 없습니다.' } } updates.push('updated_at = NOW()') values.push(todoId) await execute(` UPDATE wr_todo SET ${updates.join(', ')} WHERE todo_id = $${paramIndex} `, values) return { success: true } })