Add src/app/api/kanban/route.ts
This commit is contained in:
57
src/app/api/kanban/route.ts
Normal file
57
src/app/api/kanban/route.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getDb } from '@/server/db';
|
||||
import { tasks, projects } from '@/server/db/schema';
|
||||
import { eq, isNull, and } from 'drizzle-orm';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const db = getDb();
|
||||
const { searchParams } = new URL(request.url);
|
||||
const projectId = searchParams.get('project');
|
||||
|
||||
try {
|
||||
let whereCondition;
|
||||
|
||||
if (projectId) {
|
||||
whereCondition = and(
|
||||
eq(tasks.projectId, projectId),
|
||||
isNull(tasks.parentTaskId) // Only top-level tasks
|
||||
);
|
||||
} else {
|
||||
whereCondition = isNull(tasks.parentTaskId);
|
||||
}
|
||||
|
||||
// Get all tasks (not subtasks)
|
||||
const allTasks = await db
|
||||
.select({
|
||||
id: tasks.id,
|
||||
projectId: tasks.projectId,
|
||||
title: tasks.title,
|
||||
description: tasks.description,
|
||||
completed: tasks.completed,
|
||||
priority: tasks.priority,
|
||||
dueDate: tasks.dueDate,
|
||||
status: tasks.status,
|
||||
sortOrder: tasks.sortOrder,
|
||||
recurrenceRule: tasks.recurrenceRule,
|
||||
recurrenceInterval: tasks.recurrenceInterval,
|
||||
projectColor: projects.color,
|
||||
projectName: projects.name,
|
||||
})
|
||||
.from(tasks)
|
||||
.leftJoin(projects, eq(tasks.projectId, projects.id))
|
||||
.where(whereCondition)
|
||||
.orderBy(tasks.sortOrder);
|
||||
|
||||
// Group by status
|
||||
const grouped = {
|
||||
todo: allTasks.filter(t => t.status === 'todo'),
|
||||
in_progress: allTasks.filter(t => t.status === 'in_progress'),
|
||||
done: allTasks.filter(t => t.status === 'done'),
|
||||
};
|
||||
|
||||
return NextResponse.json(grouped);
|
||||
} catch (error) {
|
||||
console.error('Error fetching kanban data:', error);
|
||||
return NextResponse.json({ error: 'Failed to fetch kanban data' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user