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
2 changes: 1 addition & 1 deletion src/app/api/posts/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/users/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/app/lib/users_controller.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 })
Expand Down
28 changes: 14 additions & 14 deletions src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<User[]> {
const { data, error } = await supabase
.from("users")
Expand All @@ -28,8 +28,8 @@ class UserModel {
return data as User[];
}

// Função para criar um novo post
static async createUser(UserInputProps): Promise<User> {
// Função para criar um novo usuário
static async createUser({ username, password, is_admin }: UserInputProps): Promise<User> {
const { data, error } = await supabase
.from("users")
.insert([{ username, password, is_admin }])
Expand All @@ -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<User | null> {
const { data, error } = await supabase
.from("users")
Expand All @@ -57,11 +57,11 @@ class UserModel {
return data as User | null;
}

// Função para atualizar um post
static async updateUser(UserInputProps): Promise<User> {
// Função para atualizar um usuário
static async updateUser(userId: string, { username, password, is_admin }: UserInputProps): Promise<User> {
const { data, error } = await supabase
.from("users")
.update({ username, password, is_admin})
.update({ username, password, is_admin })
.eq("id", userId)
.single();

Expand All @@ -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<void> {
const { error } = await supabase
.from("users")
Expand Down
Loading