Skip to content

Commit 1e7a386

Browse files
committed
f
1 parent 8fd135c commit 1e7a386

File tree

11 files changed

+340
-97
lines changed

11 files changed

+340
-97
lines changed

.env

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
SUPABASE_URL=https://smjvdyhhwzdzvloweoxt.supabase.co
2-
SUPABASE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InNtanZkeWhod3pkenZsb3dlb3h0Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MzA5ODkzMDQsImV4cCI6MjA0NjU2NTMwNH0.-3NznwUsudhDwP03sXVOYufG-_ARGHDpXF99R9Ah8oc
1+
SUPABASE_URL=https://tvyadotzrhnhgthmvbcm.supabase.co
2+
SUPABASE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InR2eWFkb3R6cmhuaGd0aG12YmNtIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MzI1NDg5ODgsImV4cCI6MjA0ODEyNDk4OH0.uZTQDUAVqqg9syrO6Dcsfd1VftzwaWtT4SckcgMPhWs
3+
MONGODB_URI=mongodb+srv://jesiel364:[email protected]/?retryWrites=true&w=majority&appName=jesiel-db
4+
MONGODB_DB=task

bun.lockb

128 KB
Binary file not shown.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"@supabase/supabase-js": "^2.46.1",
1414
"cors": "^2.8.5",
15+
"mongodb": "^6.14.0",
1516
"next": "latest",
1617
"nextjs-cors": "^2.2.0",
1718
"pg": "^8.13.1",

src/@types/models.d.ts

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
export interface UserInput{
2+
id?: string
3+
title: string
4+
slug: string
5+
author_id: number
6+
}
7+
8+
9+
export interface UserInputProps{
10+
id?:number | string
11+
username: string
12+
email: string
13+
password: string
14+
is_admin: boolean
15+
}
16+
17+
export interface TaskProps{
18+
title: string;
19+
description: string;
20+
start_date: string;
21+
status?: "incomplete" | "completed";
22+
id: number | string;
23+
tags: string[];
24+
end_date: string;
25+
author_id: string;
26+
}
27+
28+
export interface TaskInput{
29+
title: string;
30+
description: string;
31+
start_date?: string;
32+
status?: "incomplete" | "completed";
33+
tags?: string[];
34+
end_date?: string;
35+
author_id: string;
36+
}
37+
38+
39+
type FlattenObject<T> = T extends Array<any>
40+
? number | `${number}.${FlattenObject<T[number]>}`
41+
: T extends object
42+
? {
43+
[K in keyof T & string]: K | `${K}.${FlattenObject<T[K]>}`;
44+
}[keyof T]
45+
: "";
46+
47+
type RemoveTrailingDot<T extends string> = T extends `${infer L}.${infer R}`
48+
? R extends ""
49+
? L
50+
: `${L}.${RemoveTrailingDot<R>}`
51+
: T;
52+
53+
type FlattenKeys<T> = RemoveTrailingDot<FlattenObject<T>>;
54+
55+
type DeepType<T, Key extends string | undefined | null | ""> = Key extends
56+
| undefined
57+
| null
58+
| ""
59+
? T
60+
: Key extends `${infer First}.${infer Rest}`
61+
? DeepType<T[First], Rest>
62+
: T[Key];
63+
64+
type DeepPartial<T> = T extends object
65+
? {
66+
[P in keyof T]?: DeepPartial<T[P]>;
67+
}
68+
: T;
69+
70+
type PaginationMeta = {
71+
total: number;
72+
};
73+
74+
type PaginatedResponse<D> = {
75+
meta: PaginationMeta;
76+
data: D[];
77+
};
78+
79+
type SearchFilter<T> = {
80+
page: number;
81+
limit: number;
82+
filter: SearchCriteria<T>[];
83+
orderBy: OrderByCriteria<T>[];
84+
};
85+
86+
type SearchCriteria<T> = {
87+
column: FlattenKeys<T>;
88+
value: string | number;
89+
operator: SearchOperatorSet;
90+
logicalOperator: SearchLogicalOperatorSet;
91+
};
92+
93+
class OrderByCriteria<T extends any | object> {
94+
column: FlattenKeys<T>;
95+
direction: OrderBySet;
96+
}
97+
98+
type SearchOperatorSet =
99+
| "cn"
100+
| "cp"
101+
| "nc"
102+
| "eq"
103+
| "ne"
104+
| "bw"
105+
| "bn"
106+
| "ew"
107+
| "en"
108+
| "nu"
109+
| "nn"
110+
| "gt"
111+
| "ge"
112+
| "lt"
113+
| "le"
114+
| "np";
115+
116+
type SearchLogicalOperatorSet = "and" | "or";
117+
type OrderBySet = "asc" | "desc";
118+
119+
type DirectionArg = OrderBySet | "" | undefined | null;
120+
121+
122+
123+
124+
// Definições de tipos
125+
type TaskState = {
126+
filter: {
127+
task: {
128+
filter: SearchFilter<TaskItem>;
129+
data: PaginatedResponse<TaskItem>;
130+
};
131+
};
132+
}
133+
134+
type TaskItem = {
135+
title: string;
136+
description: string;
137+
start_date: null;
138+
end_date: null;
139+
status?: "incomplete" | "completed";
140+
id: number | string;
141+
created_at: string;
142+
tags: string[];
143+
author_id: string;
144+
}
145+
146+
type PaginationMeta = {
147+
total: number;
148+
};
149+
150+
type PaginatedResponse<D> = {
151+
meta: PaginationMeta;
152+
data: D[];
153+
};
154+
155+
type SearchFilter<T> = {
156+
page: number;
157+
limit: number;
158+
filter: SearchCriteria<T>[];
159+
orderBy: OrderByCriteria<T>[];
160+
};
161+
162+
type SearchCriteria<T> = {
163+
column: FlattenKeys<T>;
164+
value: string | number;
165+
operator: SearchOperatorSet;
166+
logicalOperator: SearchLogicalOperatorSet;
167+
};
168+
169+
class OrderByCriteria<T extends any | object> {
170+
column: FlattenKeys<T>;
171+
direction: OrderBySet;
172+
}
173+
174+
type SearchOperatorSet =
175+
| "cn"
176+
| "cp"
177+
| "nc"
178+
| "eq"
179+
| "ne"
180+
| "bw"
181+
| "bn"
182+
| "ew"
183+
| "en"
184+
| "nu"
185+
| "nn"
186+
| "gt"
187+
| "ge"
188+
| "lt"
189+
| "le"
190+
| "np";
191+
192+
type SearchLogicalOperatorSet = "and" | "or";
193+
type OrderBySet = "asc" | "desc";
194+
195+
type DirectionArg = OrderBySet | "" | undefined | null;

src/app/api/tasks/[id]/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { deleteTask, getTaskById, updateTask } from "@/app/lib/tasks_controller";
2-
import { TaskInput } from "@/models/models";
2+
import { TaskInput } from "@/@types/models";
33
import { NextResponse } from "next/server";
44

55
export const GET = async (req: Request) => {

src/app/api/tasks/route.ts

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,81 @@
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+
36

47
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 });
1627
}
17-
28+
}
1829
}
1930

2031
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 });
3379
}
34-
80+
}
3581
}

src/app/lib/posts_controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UserInput } from '@/models/models';
1+
import { UserInput } from '@/@types/models';
22
import supabase from '../../config/database';
33

44

0 commit comments

Comments
 (0)