Skip to content

Commit c312504

Browse files
authored
Merge pull request #2 from jesieldotdev/new-db
New db
2 parents 57aee09 + 5bf09b4 commit c312504

File tree

12 files changed

+277
-157
lines changed

12 files changed

+277
-157
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.
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 });
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+
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import supabase from '../../config/database'; // Importando o cliente Supabase
22

3+
export interface UserInput{
4+
id?: string
5+
title: string
6+
slug: string
7+
author_id: number
8+
}
9+
310
// Obter todos os posts
411
export const getPosts = async () => {
512
const { data, error } = await supabase
@@ -13,11 +20,11 @@ export const getPosts = async () => {
1320
};
1421

1522
// Adicionar um novo post
16-
export const addPost = async (post: { title: string; }) => {
23+
export const addPost = async ({title, slug, author_id}:UserInput) => {
1724
const { data, error } = await supabase
1825
.from('posts') // Nome da tabela
1926
.insert([
20-
{title: post.title }
27+
{title, slug, author_id }
2128
]);
2229

2330
if (error) throw new Error(error.message);
@@ -41,10 +48,10 @@ export const deletePost = async (id: string) => {
4148
};
4249

4350
// Atualizar um post pelo id
44-
export const updatePost = async (id: string, title: string, desc: string) => {
51+
export const updatePost = async ({id, title, slug, author_id}:UserInput) => {
4552
const { data, error } = await supabase
4653
.from('posts')
47-
.update({ title, desc })
54+
.update({ title, slug, author_id })
4855
.eq('id', id); // A condição de atualizar pelo id
4956

5057
if (error) throw new Error(error.message);
@@ -53,7 +60,7 @@ export const updatePost = async (id: string, title: string, desc: string) => {
5360
};
5461

5562
// Obter um post pelo id
56-
export const getById = async (id: string) => {
63+
export const getPostById = async (id: string) => {
5764
const { data, error } = await supabase
5865
.from('posts')
5966
.select('*')

src/app/lib/users_controller.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import supabase from '../../config/database'; // Importando o cliente Supabase
2+
3+
interface UserInputProps{
4+
id?:number
5+
username: string
6+
email: string
7+
password: string
8+
is_admin: boolean
9+
}
10+
// Obter todos os posts
11+
export const getUsers = async () => {
12+
const { data, error } = await supabase
13+
.from('users') // Nome da tabela
14+
.select('*'); // Seleciona todos os campos
15+
16+
console.log(data)
17+
18+
if (error) throw new Error(error.message);
19+
return data;
20+
};
21+
22+
// Adicionar um novo post
23+
export const addUser = async ({username, password, is_admin, email}:UserInputProps) => {
24+
const { data, error } = await supabase
25+
.from('users') // Nome da tabela
26+
.insert([
27+
{username, password, is_admin, email}
28+
]);
29+
30+
if (error) throw new Error(error.message);
31+
return data;
32+
};
33+
34+
// Deletar um post pelo id
35+
export const deleteUser = async (id: string) => {
36+
const { data, error } = await supabase
37+
.from('users')
38+
.delete()
39+
.eq('id', id); // A condição de deletar pelo id
40+
41+
if (error) throw new Error(error.message);
42+
if (!data) {
43+
throw new Error("NO USER FOUND");
44+
}
45+
46+
47+
return data;
48+
};
49+
50+
// Atualizar um post pelo id
51+
export const updateUser = async ({username, password, is_admin, email}:UserInputProps) => {
52+
const { data, error } = await supabase
53+
.from('users')
54+
.update({ username, email, password, is_admin })
55+
.eq('id', id); // A condição de atualizar pelo id
56+
57+
if (error) throw new Error(error.message);
58+
if (!data) throw new Error("NO USER FOUND");
59+
return data;
60+
};
61+
62+
// Obter um post pelo id
63+
export const getUserById = async (id: string) => {
64+
65+
console.log(id);
66+
const { data, error } = await supabase
67+
.from('users')
68+
.select('*')
69+
.eq('id', id)
70+
.single(); // Para pegar apenas um resultado (usamos .single() para retornar um único item)
71+
72+
73+
if (error) throw new Error(error.message);
74+
if (!data) throw new Error("NO USER FOUND");
75+
return data;
76+
};

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
}

0 commit comments

Comments
 (0)