diff --git a/src/app/api/posts/[id]/route.ts b/src/app/api/posts/[id]/route.ts index fbbce69..7edf66b 100644 --- a/src/app/api/posts/[id]/route.ts +++ b/src/app/api/posts/[id]/route.ts @@ -20,7 +20,7 @@ export const PUT = async (req: Request) => { try { const { title, slug, author_id} = await req.json(); const id = req.url.split("posts/")[1]; - updatePost(id, title, slug, author_id); + updatePost({id, title, slug, author_id}); return NextResponse.json({ message: "OK" }, { status: 200 }); } catch (err) { return NextResponse.json({message: 'Error'}, {status: 500}) diff --git a/src/app/api/users/[id]/route.ts b/src/app/api/users/[id]/route.ts index ff64e12..3a56dbd 100644 --- a/src/app/api/users/[id]/route.ts +++ b/src/app/api/users/[id]/route.ts @@ -22,7 +22,7 @@ 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); + updateUser({id, username,email, password, is_admin}); return NextResponse.json({ message: "OK" }, { status: 200 }); } catch (err) { return NextResponse.json({message: 'Error'}, {status: 500}) @@ -35,7 +35,7 @@ export const DELETE = async (req: Request) => { const user = getUserById(id); - if (!post) { + if (!user) { return NextResponse.json({ message: "Error" }, { status: 404 }); } deleteUser(id); diff --git a/src/app/lib/users_controller.ts b/src/app/lib/users_controller.ts index 7a7304d..a1858f1 100644 --- a/src/app/lib/users_controller.ts +++ b/src/app/lib/users_controller.ts @@ -1,7 +1,7 @@ import supabase from '../../config/database'; // Importando o cliente Supabase interface UserInputProps{ - id?:number + id?:number | string username: string email: string password: string @@ -48,7 +48,7 @@ export const deleteUser = async (id: string) => { }; // Atualizar um post pelo id -export const updateUser = async ({username, password, is_admin, email}:UserInputProps) => { +export const updateUser = async ({username, password, is_admin, email, id}:UserInputProps) => { const { data, error } = await supabase .from('users') .update({ username, email, password, is_admin }) diff --git a/src/models/User.ts b/src/models/User.ts index bd43d2e..d2cf613 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -4,18 +4,18 @@ interface User { id: string; username: string; password: string; - createsAt: Date - is_admin: boolean; // O Supabase geralmente lida com datas como strings em formato ISO + createsAt: Date; + is_admin: boolean; } -interface UserInputProps{ - username: string - password: string - is_admin: boolean +interface UserInputProps { + username: string; + password: string; + is_admin: boolean; } class UserModel { - // Função para obter todos os posts + // Função para obter todos os usuários static async getAllUsers(): Promise { const { data, error } = await supabase .from("users") @@ -28,8 +28,8 @@ class UserModel { return data as User[]; } - // Função para criar um novo post - static async createUser(UserInputProps): Promise { + // Função para criar um novo usuário + static async createUser({ username, password, is_admin }: UserInputProps): Promise { const { data, error } = await supabase .from("users") .insert([{ username, password, is_admin }]) @@ -42,7 +42,7 @@ class UserModel { return data as User; } - // Função para obter um post por ID + // Função para obter um usuário por ID static async getUserById(userId: string): Promise { const { data, error } = await supabase .from("users") @@ -57,11 +57,11 @@ class UserModel { return data as User | null; } - // Função para atualizar um post - static async updateUser(UserInputProps): Promise { + // Função para atualizar um usuário + static async updateUser(userId: string, { username, password, is_admin }: UserInputProps): Promise { const { data, error } = await supabase .from("users") - .update({ username, password, is_admin}) + .update({ username, password, is_admin }) .eq("id", userId) .single(); @@ -72,7 +72,7 @@ class UserModel { return data as User; } - // Função para deletar um post + // Função para deletar um usuário static async deleteUser(userId: string): Promise { const { error } = await supabase .from("users")