63 lines
1.3 KiB
TypeScript
63 lines
1.3 KiB
TypeScript
'use server'
|
|
|
|
import { prisma } from './db'
|
|
import { revalidatePath } from 'next/cache'
|
|
|
|
export async function getLists() {
|
|
return await prisma.list.findMany({
|
|
include: {
|
|
items: true,
|
|
},
|
|
orderBy: {
|
|
updatedAt: 'desc',
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function createList(title: string, color: string = '#3B82F6') {
|
|
const list = await prisma.list.create({
|
|
data: {
|
|
title,
|
|
color,
|
|
},
|
|
})
|
|
revalidatePath('/')
|
|
return list
|
|
}
|
|
|
|
export async function getListWithItems(listId: string) {
|
|
return await prisma.list.findUnique({
|
|
where: { id: listId },
|
|
include: {
|
|
items: {
|
|
orderBy: {
|
|
createdAt: 'desc',
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function createItem(listId: string, title: string, description?: string) {
|
|
const item = await prisma.item.create({
|
|
data: {
|
|
title,
|
|
description,
|
|
listId,
|
|
},
|
|
})
|
|
revalidatePath(`/list/${listId}`)
|
|
return item
|
|
}
|
|
|
|
export async function toggleItem(itemId: string) {
|
|
const item = await prisma.item.findUnique({ where: { id: itemId } })
|
|
if (!item) return null
|
|
|
|
const updated = await prisma.item.update({
|
|
where: { id: itemId },
|
|
data: { completed: !item.completed },
|
|
})
|
|
revalidatePath(`/list/${item.listId}`)
|
|
return updated
|
|
} |