Skip to content

Commit 682d888

Browse files
authored
Merge pull request #6 from jesieldotdev/new-db
fix: patch
2 parents 790aa69 + 3002b45 commit 682d888

File tree

6 files changed

+71
-20
lines changed

6 files changed

+71
-20
lines changed

next.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const nextConfig = {
44
return [
55
{
66
// matching all API routes
7-
source: '/api/blog/:path*',
7+
source: '/api/:path*',
88
headers: [
99
{ key: 'Access-Control-Allow-Credentials', value: 'true' },
1010
{ key: 'Access-Control-Allow-Origin', value: '*' },

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
},
1111
"dependencies": {
1212
"@supabase/supabase-js": "^2.46.1",
13+
"cors": "^2.8.5",
1314
"next": "latest",
15+
"nextjs-cors": "^2.2.0",
1416
"pg": "^8.13.1",
1517
"pg-hstore": "^2.3.4",
1618
"react": "latest",

src/app/api/posts/[id]/route.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const GET = async (req: Request) => {
55
try {
66
const id = req.url.split("posts/")[1];
77
const post = await getPostById(id);
8-
console.log(post);
98
if (!post) {
109
return NextResponse.json({ message: "Error" }, { status: 404 });
1110
}

src/app/api/tasks/[id]/route.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { deleteTask, getTaskById, updateTask } from "@/app/lib/tasks_controller";
2+
import { TaskInput } from "@/models/models";
23
import { NextResponse } from "next/server";
34

45
export const GET = async (req: Request) => {
@@ -18,17 +19,29 @@ export const GET = async (req: Request) => {
1819
}
1920
};
2021

21-
export const PUT = async (req: Request) => {
22+
export const PATCH = async (req: Request) => {
2223
try {
23-
const { title, description, author_id } = await req.json();
24+
const { title, description, author_id, status } = await req.json();
2425
const id = req.url.split("tasks/")[1];
25-
updateTask(id, {title, description, author_id});
26+
27+
// Atualiza apenas os campos que foram passados na requisição
28+
const updatedData: Partial<TaskInput> = {};
29+
30+
if (title) updatedData.title = title;
31+
if (description) updatedData.description = description;
32+
if (author_id) updatedData.author_id = author_id;
33+
if (status) updatedData.status = status; // Se o status for passado, ele também será atualizado.
34+
35+
// Chama a função para atualizar a task no banco de dados
36+
await updateTask(id, updatedData);
37+
2638
return NextResponse.json({ message: "OK" }, { status: 200 });
2739
} catch (err) {
28-
return NextResponse.json({message: 'Error'}, {status: 500})
40+
return NextResponse.json({ message: 'Error', error: err.message }, { status: 500 });
2941
}
3042
};
3143

44+
3245
export const DELETE = async (req: Request) => {
3346
try {
3447
const id = req.url.split("tasks/")[1];

src/app/lib/tasks_controller.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const getTasks = async () => {
77
.from('tasks')
88
.select('*');
99

10-
console.log(data)
10+
1111

1212
if (error) throw new Error(error.message);
1313
return data;
@@ -41,21 +41,38 @@ export const deleteTask = async (id: string) => {
4141
};
4242

4343

44-
export const updateTask = async (id:string | number, {title, description, author_id}:TaskInput) => {
45-
const { data, error } = await supabase
46-
.from('tasks')
47-
.update({ title, description, author_id })
48-
.eq('id', id);
49-
50-
if (error) throw new Error(error.message);
51-
if (!data) throw new Error("NO TASK FOUND");
52-
return data;
53-
};
44+
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
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+
5471

5572

5673
export const getTaskById = async (id: string) => {
57-
58-
console.log(id);
74+
75+
5976
const { data, error } = await supabase
6077
.from('tasks')
6178
.select('*')

yarn.lock

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,14 @@ [email protected]:
539539
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
540540
integrity sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==
541541

542+
cors@^2.8.5:
543+
version "2.8.5"
544+
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
545+
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
546+
dependencies:
547+
object-assign "^4"
548+
vary "^1"
549+
542550
cross-spawn@^7.0.2:
543551
version "7.0.3"
544552
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
@@ -1578,7 +1586,14 @@ next@latest:
15781586
"@next/swc-win32-ia32-msvc" "13.5.3"
15791587
"@next/swc-win32-x64-msvc" "13.5.3"
15801588

1581-
object-assign@^4.1.1:
1589+
nextjs-cors@^2.2.0:
1590+
version "2.2.0"
1591+
resolved "https://registry.yarnpkg.com/nextjs-cors/-/nextjs-cors-2.2.0.tgz#d429e4cd366cc4a26179c5c1f772613ce2e385a5"
1592+
integrity sha512-FZu/A+L59J4POJNqwXYyCPDvsLDeu5HjSBvytzS6lsrJeDz5cmnH45zV+VoNic0hjaeER9xGaiIjZIWzEHnxQg==
1593+
dependencies:
1594+
cors "^2.8.5"
1595+
1596+
object-assign@^4, object-assign@^4.1.1:
15821597
version "4.1.1"
15831598
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
15841599
integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==
@@ -2225,6 +2240,11 @@ uri-js@^4.2.2:
22252240
dependencies:
22262241
punycode "^2.1.0"
22272242

2243+
vary@^1:
2244+
version "1.1.2"
2245+
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
2246+
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==
2247+
22282248
22292249
version "2.4.0"
22302250
resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-2.4.0.tgz#fa33032374962c78113f93c7f2fb4c54c9862a5d"

0 commit comments

Comments
 (0)