2020-09-03 18:28:29 +03:00
|
|
|
import HttpError from '@wasp/core/HttpError.js'
|
2020-09-19 17:11:09 +03:00
|
|
|
import Prisma from '@prisma/client'
|
2020-09-03 18:28:29 +03:00
|
|
|
|
2020-09-19 17:11:09 +03:00
|
|
|
const prisma = new Prisma.PrismaClient()
|
|
|
|
|
|
|
|
export const createTask = async (task, context) => {
|
2020-09-29 11:44:27 +03:00
|
|
|
/*
|
2020-08-31 15:41:49 +03:00
|
|
|
if (Math.random() < 0.5) {
|
2020-09-03 18:28:29 +03:00
|
|
|
throw new HttpError(400, 'Failed to create task, random error!')
|
2020-08-31 15:41:49 +03:00
|
|
|
}
|
2020-09-29 11:44:27 +03:00
|
|
|
*/
|
2020-09-19 17:11:09 +03:00
|
|
|
|
|
|
|
const newTask = await prisma.task.create({
|
|
|
|
data: {
|
|
|
|
description: task.description
|
|
|
|
}
|
|
|
|
})
|
2020-08-31 15:41:49 +03:00
|
|
|
}
|
2020-09-29 11:44:27 +03:00
|
|
|
|
|
|
|
export const updateTaskIsDone = async ({taskId, newIsDoneVal}, context) => {
|
|
|
|
await prisma.task.update({
|
|
|
|
where: { id: taskId },
|
|
|
|
data: { isDone: newIsDoneVal }
|
|
|
|
})
|
|
|
|
}
|
2020-09-30 11:08:20 +03:00
|
|
|
|
|
|
|
export const deleteCompletedTasks = async () => {
|
|
|
|
await prisma.task.deleteMany({
|
|
|
|
where: { isDone: true }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const toggleAllTasks = async () => {
|
|
|
|
const notDoneTasksCount = await prisma.task.count({ where: { isDone: false } })
|
|
|
|
|
|
|
|
if (notDoneTasksCount > 0) {
|
|
|
|
await prisma.task.updateMany({ where: { isDone: false }, data: { isDone: true } })
|
|
|
|
} else {
|
|
|
|
await prisma.task.updateMany({ data: { isDone: false } })
|
|
|
|
}
|
|
|
|
}
|