wasp/waspc/examples/todoApp/ext/actions.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

import HttpError from '@wasp/core/HttpError.js'
2020-10-19 15:45:54 +03:00
import { createNewUser } from '@wasp/core/auth.js'
export const signUp = async (args, context) => {
await createNewUser({ email: args.email, password: args.password })
}
2020-09-19 17:11:09 +03:00
export const createTask = async (task, context) => {
2020-10-19 15:45:54 +03:00
if (!context.user) {
throw new HttpError(403)
}
const Task = context.entities.Task
return Task.create({
2020-09-19 17:11:09 +03:00
data: {
description: task.description,
user: {
connect: { id: context.user.id }
}
2020-09-19 17:11:09 +03:00
}
})
}
export const updateTaskIsDone = async ({ taskId, newIsDoneVal }, context) => {
2020-10-19 15:45:54 +03:00
if (!context.user) {
throw new HttpError(403)
}
const Task = context.entities.Task
return Task.updateMany({
where: { id: taskId, user: { id: context.user.id } },
data: { isDone: newIsDoneVal }
})
}
export const deleteCompletedTasks = async (args, context) => {
2020-10-19 15:45:54 +03:00
if (!context.user) {
throw new HttpError(403)
}
const Task = context.entities.Task
await Task.deleteMany({
where: { isDone: true, user: { id: context.user.id } }
})
}
export const toggleAllTasks = async (args, context) => {
2020-10-19 15:45:54 +03:00
if (!context.user) {
throw new HttpError(403)
}
const whereIsDone = isDone => ({ isDone, user: { id: context.user.id } })
const Task = context.entities.Task
const notDoneTasksCount = await Task.count({ where: whereIsDone(false) })
if (notDoneTasksCount > 0) {
await Task.updateMany({ where: whereIsDone(false), data: { isDone: true } })
} else {
await Task.updateMany({ where: whereIsDone(true), data: { isDone: false } })
}
}