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
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
},
"dependencies": {
"@supabase/supabase-js": "^2.46.1",
"bufferutil": "^4.0.8",
"next": "latest",
"pg": "^8.13.1",
"pg-hstore": "^2.3.4",
"react": "latest",
"react-dom": "latest",
"sequelize": "^6.37.5",
"utf-8-validate": "^6.0.5"
"react-dom": "latest"
},
"devDependencies": {
"@types/node": "latest",
Expand Down
17 changes: 0 additions & 17 deletions src/app/api/blog/posts.json

This file was deleted.

1 change: 0 additions & 1 deletion src/app/api/posts.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { deletePost, getById, updatePost } from "@/app/lib/data";
import { deletePost, getPostById, updatePost } from "@/app/lib/posts_controller";
import { NextResponse } from "next/server";

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

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

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

const post = getById(id);
const post = getPostById(id);

if (!post) {
return NextResponse.json({ message: "Error" }, { status: 404 });
Expand Down
6 changes: 3 additions & 3 deletions src/app/api/blog/route.ts → src/app/api/posts/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPosts, addPost, deletePost } from "@/app/lib/data"
import { getPosts, addPost, deletePost } from "@/app/lib/posts_controller"
import { NextResponse } from "next/server"

export const GET = async (req: Request) => {
Expand All @@ -18,10 +18,10 @@ export const GET = async (req: Request) => {
}

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

try {
const post = { title };
const post = { title, slug, author_id };
await addPost(post); // Garantir que a função addPost aguarde a inserção do post
return NextResponse.json({ message: "Ok", post }, { status: 201 });
} catch (err) {
Expand Down
51 changes: 51 additions & 0 deletions src/app/api/users/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { deleteUser, getUserById, updateUser } from "@/app/lib/users_controller";
import { NextResponse } from "next/server";

export const GET = async (req: Request) => {
try {
const id = req.url.split("users/")[1]
const user = await getUserById(id)


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


return NextResponse.json({ message: "OK", user}, { status: 200 });
} catch (err) {
NextResponse.json({ message: "Error", err }, { status: 500 });
}
};

export const PUT = async (req: Request) => {
try {
const { username, password, email, is_admin } = await req.json();
const id = req.url.split("users/")[1];
updateUser(id, username, password, is_admin);
return NextResponse.json({ message: "OK" }, { status: 200 });
} catch (err) {
return NextResponse.json({message: 'Error'}, {status: 500})
}
};

export const DELETE = async (req: Request) => {
try {
const id = req.url.split("users/")[1];

const user = getUserById(id);

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

return NextResponse.json({ message: "OK" }, { status: 200 });
} catch (err) {
NextResponse.json({ message: "Error", err }, { status: 500 });
}
};

export const POST = async (req: Request) => {
console.log("GET");
};
35 changes: 35 additions & 0 deletions src/app/api/users/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getUsers, addUser, deleteUser } from "@/app/lib/users_controller"
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 });
} catch (err) {
if (err instanceof Error) {
return NextResponse.json({ message: 'Error', err: err.message }, { status: 500 });
} else {
return NextResponse.json({ message: 'Unknown error' }, { status: 500 });
}
}

}

export const POST = async (req: Request) => {
const { username, email, password, is_admin } = await req.json();

try {
const user = { username, email, password, is_admin };
await addUser(user); // Garantir que a função addPost aguarde a inserção do post
return NextResponse.json({ message: "Ok", user }, { status: 201 });
} catch (err) {
if (err instanceof Error) {
return NextResponse.json({ message: 'Error', err: err.message }, { status: 500 });
} else {
return NextResponse.json({ message: 'Unknown error' }, { status: 500 });
}
}

}
17 changes: 12 additions & 5 deletions src/app/lib/data.ts → src/app/lib/posts_controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import supabase from '../../config/database'; // Importando o cliente Supabase

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
Expand All @@ -13,11 +20,11 @@ export const getPosts = async () => {
};

// Adicionar um novo post
export const addPost = async (post: { title: string; }) => {
export const addPost = async ({title, slug, author_id}:UserInput) => {
const { data, error } = await supabase
.from('posts') // Nome da tabela
.insert([
{title: post.title }
{title, slug, author_id }
]);

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

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

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

// Obter um post pelo id
export const getById = async (id: string) => {
export const getPostById = async (id: string) => {
const { data, error } = await supabase
.from('posts')
.select('*')
Expand Down
76 changes: 76 additions & 0 deletions src/app/lib/users_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import supabase from '../../config/database'; // Importando o cliente Supabase

interface UserInputProps{
id?:number
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

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
.insert([
{username, password, is_admin, email}
]);

if (error) throw new Error(error.message);
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

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


return data;
};

// Atualizar um post pelo id
export const updateUser = async ({username, password, is_admin, email}:UserInputProps) => {
const { data, error } = await supabase
.from('users')
.update({ username, email, password, is_admin })
.eq('id', id); // A condição de atualizar pelo 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)


if (error) throw new Error(error.message);
if (!data) throw new Error("NO USER FOUND");
return data;
};
3 changes: 2 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export default function Home() {
return (
<main>
<p>My Api</p>
<a href='/api/blog'>Blog</a>
<a href='/api/posts'>posts</a><br/>
<a href='/api/users'>users</a>
</main>
)
}
Loading
Loading