|
1 | | -import { getTasks, addTask, deleteTask } from "@/app/lib/tasks_controller" |
2 | | -import { NextResponse } from "next/server" |
| 1 | +import { getTasks, addTask } from "@/app/lib/tasks_controller"; |
| 2 | +import { FlattenKeys } from "@/@types/models"; |
| 3 | +import { NextResponse } from "next/server"; |
| 4 | + |
| 5 | + |
3 | 6 |
|
4 | 7 | export const GET = async (req: Request) => { |
5 | | - try { |
6 | | - |
7 | | - const tasks = await getTasks(); |
8 | | - |
9 | | - return NextResponse.json({ message: 'OK', tasks }, { status: 200 }); |
10 | | - } catch (err) { |
11 | | - if (err instanceof Error) { |
12 | | - return NextResponse.json({ message: 'Error', err: err.message }, { status: 500 }); |
13 | | - } else { |
14 | | - return NextResponse.json({ message: 'Unknown error' }, { status: 500 }); |
15 | | - } |
| 8 | + try { |
| 9 | + // Obtendo as tarefas da API do controller |
| 10 | + const tasks = await getTasks(); |
| 11 | + |
| 12 | + // Estrutura do retorno de acordo com o tipo `TaskState` |
| 13 | + const data = { |
| 14 | + filter: [], // Filtro vazio |
| 15 | + meta: { |
| 16 | + total: tasks.length // Número total de tarefas |
| 17 | + }, |
| 18 | + data: tasks |
| 19 | + }; |
| 20 | + |
| 21 | + return NextResponse.json({ data }, { status: 200 }); |
| 22 | + } catch (err) { |
| 23 | + if (err instanceof Error) { |
| 24 | + return NextResponse.json({ message: 'Error', err: err.message }, { status: 500 }); |
| 25 | + } else { |
| 26 | + return NextResponse.json({ message: 'Unknown error' }, { status: 500 }); |
16 | 27 | } |
17 | | - |
| 28 | + } |
18 | 29 | } |
19 | 30 |
|
20 | 31 | export const POST = async (req: Request) => { |
21 | | - const { title, description, author_id } = await req.json(); |
22 | | - |
23 | | - try { |
24 | | - const task = { title, description, author_id }; |
25 | | - await addTask(task); |
26 | | - return NextResponse.json({ message: "Ok", task }, { status: 201 }); |
27 | | - } catch (err) { |
28 | | - if (err instanceof Error) { |
29 | | - return NextResponse.json({ message: 'Error', err: err.message }, { status: 500 }); |
30 | | - } else { |
31 | | - return NextResponse.json({ message: 'Unknown error' }, { status: 500 }); |
32 | | - } |
| 32 | + const { title, description, author_id } = await req.json(); |
| 33 | + |
| 34 | + try { |
| 35 | + const task = { title, description, author_id }; |
| 36 | + |
| 37 | + // Adicionando a tarefa na API do controller |
| 38 | + await addTask(task); |
| 39 | + |
| 40 | + // Retorna a tarefa criada com a estrutura de `TaskState` |
| 41 | + // const taskState: TaskState = { |
| 42 | + // items: [ |
| 43 | + // { |
| 44 | + // title: task.title, |
| 45 | + // description: task.description, |
| 46 | + // start_date: null, |
| 47 | + // end_date: null, |
| 48 | + // status: "incomplete", |
| 49 | + // id: new Date().getTime(), // ID gerado com base no timestamp |
| 50 | + // created_at: new Date().toISOString(), |
| 51 | + // tags: [], // Tags podem ser uma lista vazia por enquanto |
| 52 | + // author_id: task.author_id |
| 53 | + // } |
| 54 | + // ], |
| 55 | + // selectedItemIndex: 0, |
| 56 | + // searchText: "", |
| 57 | + // filter: { |
| 58 | + // task: { |
| 59 | + // filter: [], |
| 60 | + // data: { |
| 61 | + // meta: { |
| 62 | + // total: 1 |
| 63 | + // }, |
| 64 | + // data: [taskState.items[0]] |
| 65 | + // } |
| 66 | + // } |
| 67 | + // }, |
| 68 | + // taskModalTable: false |
| 69 | + // }; |
| 70 | + |
| 71 | + // const tasks = await getTasks() |
| 72 | + |
| 73 | + return NextResponse.json({ message: "Ok", data: task }, { status: 201 }); |
| 74 | + } catch (err) { |
| 75 | + if (err instanceof Error) { |
| 76 | + return NextResponse.json({ message: 'Error', err: err.message }, { status: 500 }); |
| 77 | + } else { |
| 78 | + return NextResponse.json({ message: 'Unknown error' }, { status: 500 }); |
33 | 79 | } |
34 | | - |
| 80 | + } |
35 | 81 | } |
0 commit comments