Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/api/posts/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NextResponse } from "next/server"

export const GET = async (req: Request) => {
try {
// Aguarda a resolução da promessa

const posts = await getPosts();

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

try {
const post = { title, slug, author_id };
await addPost(post); // Garantir que a função addPost aguarde a inserção do post
await addPost(post);
return NextResponse.json({ message: "Ok", post }, { status: 201 });
} catch (err) {
if (err instanceof Error) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/users/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NextResponse } from "next/server"

export const GET = async (req: Request) => {
try {
// Aguarda a resolução da promessa

const users = await getUsers();

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

try {
const user = { username, email, password, is_admin };
await addUser(user); // Garantir que a função addPost aguarde a inserção do post
await addUser(user);
return NextResponse.json({ message: "Ok", user }, { status: 201 });
} catch (err) {
if (err instanceof Error) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })

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

Expand Down
31 changes: 13 additions & 18 deletions src/app/lib/posts_controller.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import supabase from '../../config/database'; // Importando o cliente Supabase
import { UserInput } from '@/models/models';
import supabase from '../../config/database';


export interface UserInput{
id?: string
title: string
slug: string
author_id: number
}

// Obter todos os posts
export const getPosts = async () => {
const { data, error } = await supabase
.from('posts') // Nome da tabela
.select('*'); // Seleciona todos os campos
.from('posts')
.select('*');

console.log(data)

if (error) throw new Error(error.message);
return data;
};

// Adicionar um novo post

export const addPost = async ({title, slug, author_id}:UserInput) => {
const { data, error } = await supabase
.from('posts') // Nome da tabela
.from('posts')
.insert([
{title, slug, author_id }
]);
Expand All @@ -31,12 +26,12 @@ export const addPost = async ({title, slug, author_id}:UserInput) => {
return data;
};

// Deletar um post pelo id

export const deletePost = async (id: string) => {
const { data, error } = await supabase
.from('posts')
.delete()
.eq('id', id); // A condição de deletar pelo id
.eq('id', id);

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

// Atualizar um post pelo id

export const updatePost = async ({id, title, slug, author_id}:UserInput) => {
const { data, error } = await supabase
.from('posts')
.update({ title, slug, author_id })
.eq('id', id); // A condição de atualizar pelo id
.eq('id', id);

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

// Obter um post pelo id

export const getPostById = async (id: string) => {
const { data, error } = await supabase
.from('posts')
.select('*')
.eq('id', id)
.single(); // Para pegar apenas um resultado (usamos .single() para retornar um único item)
.single();

if (error) throw new Error(error.message);
if (!data) throw new Error("NO POST FOUND");
Expand Down
33 changes: 13 additions & 20 deletions src/app/lib/users_controller.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import supabase from '../../config/database'; // Importando o cliente Supabase
import { UserInputProps } from '@/models/models';
import supabase from '../../config/database';


interface UserInputProps{
id?:number | string
username: string
email: string
password: string
is_admin: boolean
}
// Obter todos os posts
export const getUsers = async () => {
const { data, error } = await supabase
.from('users') // Nome da tabela
.select('*'); // Seleciona todos os campos
.from('users')
.select('*');

console.log(data)

if (error) throw new Error(error.message);
return data;
};

// Adicionar um novo post

export const addUser = async ({username, password, is_admin, email}:UserInputProps) => {
const { data, error } = await supabase
.from('users') // Nome da tabela
.from('users')
.insert([
{username, password, is_admin, email}
]);
Expand All @@ -31,13 +25,12 @@ export const addUser = async ({username, password, is_admin, email}:UserInputPro
return data;
};

// Deletar um post pelo id

export const deleteUser = async (id: string) => {
const { data, error } = await supabase
.from('users')
.delete()
.eq('id', id); // A condição de deletar pelo id

.eq('id', id);
if (error) throw new Error(error.message);
if (!data) {
throw new Error("NO USER FOUND");
Expand All @@ -47,27 +40,27 @@ export const deleteUser = async (id: string) => {
return data;
};

// Atualizar um post pelo id

export const updateUser = async ({username, password, is_admin, email, id}:UserInputProps) => {
const { data, error } = await supabase
.from('users')
.update({ username, email, password, is_admin })
.eq('id', id); // A condição de atualizar pelo id
.eq('id', id);

if (error) throw new Error(error.message);
if (!data) throw new Error("NO USER FOUND");
return data;
};

// Obter um post pelo id

export const getUserById = async (id: string) => {

console.log(id);
const { data, error } = await supabase
.from('users')
.select('*')
.eq('id', id)
.single(); // Para pegar apenas um resultado (usamos .single() para retornar um único item)
.single();


if (error) throw new Error(error.message);
Expand Down
81 changes: 0 additions & 81 deletions src/models/Post.ts

This file was deleted.

88 changes: 0 additions & 88 deletions src/models/User.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/models/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface UserInput{
id?: string
title: string
slug: string
author_id: number
}


export interface UserInputProps{
id?:number | string
username: string
email: string
password: string
is_admin: boolean
}
Loading