Skip to content

Commit a9020d4

Browse files
committed
fix
1 parent 3002b45 commit a9020d4

File tree

2 files changed

+83
-21
lines changed

2 files changed

+83
-21
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { updateTaskStatus } from "@/app/lib/tasks_controller";
2+
import { NextResponse } from "next/server";
3+
4+
export const PATCH = async (req: Request, { params }: { params: { id: string } }) => {
5+
try {
6+
const { id } = params; // Acessando o ID via params para maior clareza
7+
const { status } = await req.json();
8+
9+
// Verificar se o status é válido
10+
if (!['incomplete', 'completed'].includes(status)) {
11+
return NextResponse.json({ message: 'Invalid status value' }, { status: 400 });
12+
}
13+
14+
// Chamar a função para atualizar o status da tarefa
15+
const updatedTask = await updateTaskStatus(Number(id), status);
16+
17+
return NextResponse.json({ message: "Task status updated successfully", task: updatedTask }, { status: 200 });
18+
} catch (err) {
19+
if (err instanceof Error) {
20+
console.error('Erro ao atualizar status da tarefa:', err); // Log de erro
21+
return NextResponse.json({ message: 'Internal server error', error: err.message }, { status: 500 });
22+
} else {
23+
console.error('Erro desconhecido:', err); // Log de erro
24+
return NextResponse.json({ message: 'Unknown error occurred' }, { status: 500 });
25+
}
26+
}
27+
};

src/app/lib/tasks_controller.ts

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,65 @@ export const deleteTask = async (id: string) => {
4242

4343

4444
export const updateTask = async (id: string | number, updatedData: Partial<TaskInput>) => {
45-
console.log('Updating task with ID: ', id);
46-
47-
// Confirme se a tarefa existe antes de tentar atualizar
48-
const { data: task, error: taskError } = await supabase
49-
.from('tasks')
50-
.select('*')
51-
.eq('id', id)
52-
.single();
53-
54-
if (taskError) throw new Error(`Error fetching task: ${taskError.message}`);
55-
if (!task) throw new Error(`No task found with the given id: ${id}`);
56-
57-
// Atualiza apenas os campos passados na requisição
45+
console.log('Updating task with ID: ', id);
46+
47+
// Confirme se a tarefa existe antes de tentar atualizar
48+
const { data: task, error: taskError } = await supabase
49+
.from('tasks')
50+
.select('*')
51+
.eq('id', id)
52+
.single();
53+
54+
if (taskError) throw new Error(`Error fetching task: ${taskError.message}`);
55+
if (!task) throw new Error(`No task found with the given id: ${id}`);
56+
57+
// Atualiza apenas os campos passados na requisição
58+
const { data, error } = await supabase
59+
.from('tasks')
60+
.update(updatedData)
61+
.eq('id', Number(id)); // Garantir que o id seja numérico
62+
63+
if (error) throw new Error(`Error updating task: ${error.message}`);
64+
65+
console.log('Updated task: ', data); // Verifique a resposta
66+
67+
return data;
68+
};
69+
70+
export const updateTaskStatus = async (id: string | number, status: 'incomplete' | 'completed') => {
71+
console.log('Updating status for task with ID: ', id);
72+
73+
// Verifica se o status é válido antes de enviar a requisição
74+
if (!['incomplete', 'completed'].includes(status)) {
75+
throw new Error('Invalid status value');
76+
}
77+
78+
// Verifica se o ID é um número válido
79+
if (typeof id !== 'number' || isNaN(Number(id))) {
80+
throw new Error('Invalid ID format');
81+
}
82+
83+
try {
84+
// Atualiza a coluna status da tarefa com o ID fornecido
5885
const { data, error } = await supabase
5986
.from('tasks')
60-
.update(updatedData)
61-
.eq('id', Number(id)); // Garantir que o id seja numérico
62-
63-
if (error) throw new Error(`Error updating task: ${error.message}`);
64-
65-
console.log('Updated task: ', data); // Verifique a resposta
66-
87+
.update({ status })
88+
.eq('id', id); // Garante que o id seja numérico
89+
90+
if (error) {
91+
console.error('Error updating task status: ', error.message);
92+
throw new Error(`Error updating task status: ${error.message}`);
93+
}
94+
95+
console.log('Updated task status: ', data); // Verifica a resposta
6796
return data;
68-
};
97+
} catch (err) {
98+
console.error('Unexpected error: ', err);
99+
throw new Error('An unexpected error occurred while updating task status');
100+
}
101+
};
102+
103+
69104

70105

71106

0 commit comments

Comments
 (0)