Skip to content

Commit 447087b

Browse files
authored
Merge pull request #4 from jesieldotdev/new-db
New db
2 parents a08ddef + e3838d3 commit 447087b

File tree

8 files changed

+46
-212
lines changed

8 files changed

+46
-212
lines changed

src/app/api/posts/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NextResponse } from "next/server"
33

44
export const GET = async (req: Request) => {
55
try {
6-
// Aguarda a resolução da promessa
6+
77
const posts = await getPosts();
88

99
return NextResponse.json({ message: 'OK', posts }, { status: 200 });
@@ -22,7 +22,7 @@ export const POST = async (req: Request) => {
2222

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

src/app/api/users/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NextResponse } from "next/server"
33

44
export const GET = async (req: Request) => {
55
try {
6-
// Aguarda a resolução da promessa
6+
77
const users = await getUsers();
88

99
return NextResponse.json({ message: 'OK', users }, { status: 200 });
@@ -22,7 +22,7 @@ export const POST = async (req: Request) => {
2222

2323
try {
2424
const user = { username, email, password, is_admin };
25-
await addUser(user); // Garantir que a função addPost aguarde a inserção do post
25+
await addUser(user);
2626
return NextResponse.json({ message: "Ok", user }, { status: 201 });
2727
} catch (err) {
2828
if (err instanceof Error) {

src/app/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Inter } from 'next/font/google'
44
const inter = Inter({ subsets: ['latin'] })
55

66
export const metadata: Metadata = {
7-
title: 'Create Next App',
7+
title: 'posts-api-vercel',
88
description: 'Generated by create next app',
99
}
1010

src/app/lib/posts_controller.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
import supabase from '../../config/database'; // Importando o cliente Supabase
1+
import { UserInput } from '@/models/models';
2+
import supabase from '../../config/database';
3+
24

3-
export interface UserInput{
4-
id?: string
5-
title: string
6-
slug: string
7-
author_id: number
8-
}
95

10-
// Obter todos os posts
116
export const getPosts = async () => {
127
const { data, error } = await supabase
13-
.from('posts') // Nome da tabela
14-
.select('*'); // Seleciona todos os campos
8+
.from('posts')
9+
.select('*');
1510

1611
console.log(data)
1712

1813
if (error) throw new Error(error.message);
1914
return data;
2015
};
2116

22-
// Adicionar um novo post
17+
2318
export const addPost = async ({title, slug, author_id}:UserInput) => {
2419
const { data, error } = await supabase
25-
.from('posts') // Nome da tabela
20+
.from('posts')
2621
.insert([
2722
{title, slug, author_id }
2823
]);
@@ -31,12 +26,12 @@ export const addPost = async ({title, slug, author_id}:UserInput) => {
3126
return data;
3227
};
3328

34-
// Deletar um post pelo id
29+
3530
export const deletePost = async (id: string) => {
3631
const { data, error } = await supabase
3732
.from('posts')
3833
.delete()
39-
.eq('id', id); // A condição de deletar pelo id
34+
.eq('id', id);
4035

4136
if (error) throw new Error(error.message);
4237
if (!data) {
@@ -47,25 +42,25 @@ export const deletePost = async (id: string) => {
4742
return data;
4843
};
4944

50-
// Atualizar um post pelo id
45+
5146
export const updatePost = async ({id, title, slug, author_id}:UserInput) => {
5247
const { data, error } = await supabase
5348
.from('posts')
5449
.update({ title, slug, author_id })
55-
.eq('id', id); // A condição de atualizar pelo id
50+
.eq('id', id);
5651

5752
if (error) throw new Error(error.message);
5853
if (!data) throw new Error("NO POST FOUND");
5954
return data;
6055
};
6156

62-
// Obter um post pelo id
57+
6358
export const getPostById = async (id: string) => {
6459
const { data, error } = await supabase
6560
.from('posts')
6661
.select('*')
6762
.eq('id', id)
68-
.single(); // Para pegar apenas um resultado (usamos .single() para retornar um único item)
63+
.single();
6964

7065
if (error) throw new Error(error.message);
7166
if (!data) throw new Error("NO POST FOUND");

src/app/lib/users_controller.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1-
import supabase from '../../config/database'; // Importando o cliente Supabase
1+
import { UserInputProps } from '@/models/models';
2+
import supabase from '../../config/database';
3+
24

3-
interface UserInputProps{
4-
id?:number | string
5-
username: string
6-
email: string
7-
password: string
8-
is_admin: boolean
9-
}
10-
// Obter todos os posts
115
export const getUsers = async () => {
126
const { data, error } = await supabase
13-
.from('users') // Nome da tabela
14-
.select('*'); // Seleciona todos os campos
7+
.from('users')
8+
.select('*');
159

1610
console.log(data)
1711

1812
if (error) throw new Error(error.message);
1913
return data;
2014
};
2115

22-
// Adicionar um novo post
16+
2317
export const addUser = async ({username, password, is_admin, email}:UserInputProps) => {
2418
const { data, error } = await supabase
25-
.from('users') // Nome da tabela
19+
.from('users')
2620
.insert([
2721
{username, password, is_admin, email}
2822
]);
@@ -31,13 +25,12 @@ export const addUser = async ({username, password, is_admin, email}:UserInputPro
3125
return data;
3226
};
3327

34-
// Deletar um post pelo id
28+
3529
export const deleteUser = async (id: string) => {
3630
const { data, error } = await supabase
3731
.from('users')
3832
.delete()
39-
.eq('id', id); // A condição de deletar pelo id
40-
33+
.eq('id', id);
4134
if (error) throw new Error(error.message);
4235
if (!data) {
4336
throw new Error("NO USER FOUND");
@@ -47,27 +40,27 @@ export const deleteUser = async (id: string) => {
4740
return data;
4841
};
4942

50-
// Atualizar um post pelo id
43+
5144
export const updateUser = async ({username, password, is_admin, email, id}:UserInputProps) => {
5245
const { data, error } = await supabase
5346
.from('users')
5447
.update({ username, email, password, is_admin })
55-
.eq('id', id); // A condição de atualizar pelo id
48+
.eq('id', id);
5649

5750
if (error) throw new Error(error.message);
5851
if (!data) throw new Error("NO USER FOUND");
5952
return data;
6053
};
6154

62-
// Obter um post pelo id
55+
6356
export const getUserById = async (id: string) => {
6457

6558
console.log(id);
6659
const { data, error } = await supabase
6760
.from('users')
6861
.select('*')
6962
.eq('id', id)
70-
.single(); // Para pegar apenas um resultado (usamos .single() para retornar um único item)
63+
.single();
7164

7265

7366
if (error) throw new Error(error.message);

src/models/Post.ts

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

src/models/User.ts

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

src/models/models.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

0 commit comments

Comments
 (0)