Skip to content

Commit 5bf09b4

Browse files
committed
s
1 parent 4ac2342 commit 5bf09b4

File tree

10 files changed

+189
-152
lines changed

10 files changed

+189
-152
lines changed

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@
1010
},
1111
"dependencies": {
1212
"@supabase/supabase-js": "^2.46.1",
13-
"bufferutil": "^4.0.8",
1413
"next": "latest",
1514
"pg": "^8.13.1",
1615
"pg-hstore": "^2.3.4",
1716
"react": "latest",
18-
"react-dom": "latest",
19-
"sequelize": "^6.37.5",
20-
"utf-8-validate": "^6.0.5"
17+
"react-dom": "latest"
2118
},
2219
"devDependencies": {
2320
"@types/node": "latest",

src/app/api/blog/posts.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/app/api/posts.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/app/api/blog/[id]/route.ts renamed to src/app/api/posts/[id]/route.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { deletePost, getById, updatePost } from "@/app/lib/data";
1+
import { deletePost, getPostById, updatePost } from "@/app/lib/posts_controller";
22
import { NextResponse } from "next/server";
33

44
export const GET = async (req: Request) => {
55
try {
6-
const id = req.url.split("blog/")[1];
7-
const post = getById(id);
6+
const id = req.url.split("posts/")[1];
7+
const post = await getPostById(id);
88
console.log(post);
99
if (!post) {
1010
return NextResponse.json({ message: "Error" }, { status: 404 });
@@ -18,9 +18,9 @@ export const GET = async (req: Request) => {
1818

1919
export const PUT = async (req: Request) => {
2020
try {
21-
const { title, desc } = await req.json();
22-
const id = req.url.split("blog/")[1];
23-
updatePost(id, title, desc);
21+
const { title, slug, author_id} = await req.json();
22+
const id = req.url.split("posts/")[1];
23+
updatePost(id, title, slug, author_id);
2424
return NextResponse.json({ message: "OK" }, { status: 200 });
2525
} catch (err) {
2626
return NextResponse.json({message: 'Error'}, {status: 500})
@@ -29,9 +29,9 @@ export const PUT = async (req: Request) => {
2929

3030
export const DELETE = async (req: Request) => {
3131
try {
32-
const id = req.url.split("blog/")[1];
32+
const id = req.url.split("posts/")[1];
3333

34-
const post = getById(id);
34+
const post = getPostById(id);
3535

3636
if (!post) {
3737
return NextResponse.json({ message: "Error" }, { status: 404 });

src/app/api/blog/route.ts renamed to src/app/api/posts/route.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getPosts, addPost, deletePost } from "@/app/lib/data"
1+
import { getPosts, addPost, deletePost } from "@/app/lib/posts_controller"
22
import { NextResponse } from "next/server"
33

44
export const GET = async (req: Request) => {
@@ -18,10 +18,10 @@ export const GET = async (req: Request) => {
1818
}
1919

2020
export const POST = async (req: Request) => {
21-
const { title } = await req.json();
21+
const { title, slug, author_id } = await req.json();
2222

2323
try {
24-
const post = { title };
24+
const post = { title, slug, author_id };
2525
await addPost(post); // Garantir que a função addPost aguarde a inserção do post
2626
return NextResponse.json({ message: "Ok", post }, { status: 201 });
2727
} catch (err) {

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { deleteUser, getUserById, updateUser } from "@/app/lib/users_controller";
2+
import { NextResponse } from "next/server";
3+
4+
export const GET = async (req: Request) => {
5+
try {
6+
const id = req.url.split("users/")[1]
7+
const user = await getUserById(id)
8+
9+
10+
if (!user) {
11+
return NextResponse.json({ message: "Error" }, { status: 404 });
12+
}
13+
14+
15+
return NextResponse.json({ message: "OK", user}, { status: 200 });
16+
} catch (err) {
17+
NextResponse.json({ message: "Error", err }, { status: 500 });
18+
}
19+
};
20+
21+
export const PUT = async (req: Request) => {
22+
try {
23+
const { username, password, email, is_admin } = await req.json();
24+
const id = req.url.split("users/")[1];
25+
updateUser(id, username, password, is_admin);
26+
return NextResponse.json({ message: "OK" }, { status: 200 });
27+
} catch (err) {
28+
return NextResponse.json({message: 'Error'}, {status: 500})
29+
}
30+
};
31+
32+
export const DELETE = async (req: Request) => {
33+
try {
34+
const id = req.url.split("users/")[1];
35+
36+
const user = getUserById(id);
37+
38+
if (!post) {
39+
return NextResponse.json({ message: "Error" }, { status: 404 });
40+
}
41+
deleteUser(id);
42+
43+
return NextResponse.json({ message: "OK" }, { status: 200 });
44+
} catch (err) {
45+
NextResponse.json({ message: "Error", err }, { status: 500 });
46+
}
47+
};
48+
49+
export const POST = async (req: Request) => {
50+
console.log("GET");
51+
};

src/app/api/users/route.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { getUsers, addUser, deleteUser } from "@/app/lib/users_controller"
2+
import { NextResponse } from "next/server"
3+
4+
export const GET = async (req: Request) => {
5+
try {
6+
// Aguarda a resolução da promessa
7+
const users = await getUsers();
8+
9+
return NextResponse.json({ message: 'OK', users }, { 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+
}
16+
}
17+
18+
}
19+
20+
export const POST = async (req: Request) => {
21+
const { username, email, password, is_admin } = await req.json();
22+
23+
try {
24+
const user = { username, email, password, is_admin };
25+
await addUser(user); // Garantir que a função addPost aguarde a inserção do post
26+
return NextResponse.json({ message: "Ok", user }, { 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+
}
33+
}
34+
35+
}

src/app/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export default function Home() {
44
return (
55
<main>
66
<p>My Api</p>
7-
<a href='/api/blog'>Blog</a>
7+
<a href='/api/posts'>posts</a><br/>
8+
<a href='/api/users'>users</a>
89
</main>
910
)
1011
}

src/models/User.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import supabase from "../config/database"; // Caminho para o seu cliente Supabase
2+
3+
interface User {
4+
id: string;
5+
username: string;
6+
password: string;
7+
createsAt: Date
8+
is_admin: boolean; // O Supabase geralmente lida com datas como strings em formato ISO
9+
}
10+
11+
interface UserInputProps{
12+
username: string
13+
password: string
14+
is_admin: boolean
15+
}
16+
17+
class UserModel {
18+
// Função para obter todos os posts
19+
static async getAllUsers(): Promise<User[]> {
20+
const { data, error } = await supabase
21+
.from("users")
22+
.select("*");
23+
24+
if (error) {
25+
throw new Error(error.message);
26+
}
27+
28+
return data as User[];
29+
}
30+
31+
// Função para criar um novo post
32+
static async createUser(UserInputProps): Promise<User> {
33+
const { data, error } = await supabase
34+
.from("users")
35+
.insert([{ username, password, is_admin }])
36+
.single();
37+
38+
if (error) {
39+
throw new Error(error.message);
40+
}
41+
42+
return data as User;
43+
}
44+
45+
// Função para obter um post por ID
46+
static async getUserById(userId: string): Promise<User | null> {
47+
const { data, error } = await supabase
48+
.from("users")
49+
.select("*")
50+
.eq("id", userId)
51+
.single();
52+
53+
if (error) {
54+
throw new Error(error.message);
55+
}
56+
57+
return data as User | null;
58+
}
59+
60+
// Função para atualizar um post
61+
static async updateUser(UserInputProps): Promise<User> {
62+
const { data, error } = await supabase
63+
.from("users")
64+
.update({ username, password, is_admin})
65+
.eq("id", userId)
66+
.single();
67+
68+
if (error) {
69+
throw new Error(error.message);
70+
}
71+
72+
return data as User;
73+
}
74+
75+
// Função para deletar um post
76+
static async deleteUser(userId: string): Promise<void> {
77+
const { error } = await supabase
78+
.from("users")
79+
.delete()
80+
.eq("id", userId);
81+
82+
if (error) {
83+
throw new Error(error.message);
84+
}
85+
}
86+
}
87+
88+
export default UserModel;

0 commit comments

Comments
 (0)