TaskFlow - Todo App with Next.js, Prisma, NextAuth

This commit is contained in:
PhongMacbook
2025-12-16 23:48:54 +07:00
parent fecae546e9
commit 81a38eef72
2 changed files with 128 additions and 119 deletions

View File

@@ -7,9 +7,10 @@ import { updateTaskSchema } from '@/lib/validations'
// GET /api/tasks/[id] - Lấy chi tiết task
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
@@ -21,7 +22,7 @@ export async function GET(
const task = await prisma.task.findFirst({
where: {
id: params.id,
id: id,
userId: session.user.id, // Chỉ lấy task của user hiện tại
},
})
@@ -49,9 +50,10 @@ export async function GET(
// PUT /api/tasks/[id] - Cập nhật task
export async function PUT(
request: NextRequest,
{ params }: { params: { id: string } }
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
@@ -64,7 +66,7 @@ export async function PUT(
// Check if task belongs to user
const existingTask = await prisma.task.findFirst({
where: {
id: params.id,
id: id,
userId: session.user.id,
},
})
@@ -94,7 +96,7 @@ export async function PUT(
// Update task
const task = await prisma.task.update({
where: { id: params.id },
where: { id: id },
data: {
...(title && { title }),
...(description !== undefined && { description }),
@@ -122,9 +124,10 @@ export async function PUT(
// DELETE /api/tasks/[id] - Xóa task
export async function DELETE(
request: NextRequest,
{ params }: { params: { id: string } }
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
@@ -137,7 +140,7 @@ export async function DELETE(
// Check if task belongs to user
const existingTask = await prisma.task.findFirst({
where: {
id: params.id,
id: id,
userId: session.user.id,
},
})
@@ -151,7 +154,7 @@ export async function DELETE(
// Delete task
await prisma.task.delete({
where: { id: params.id },
where: { id: id },
})
return NextResponse.json({