Skip to content

Commit 4ac2342

Browse files
committed
feat: users
1 parent 7585f53 commit 4ac2342

File tree

2 files changed

+88
-5
lines changed

2 files changed

+88
-5
lines changed

src/app/lib/data.ts renamed to src/app/lib/posts_controller.ts

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+
};

0 commit comments

Comments
 (0)