Skip to content

Commit 47b7ced

Browse files
committed
feat: tasks
1 parent e3838d3 commit 47b7ced

File tree

5 files changed

+180
-2
lines changed

5 files changed

+180
-2
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { deleteTask, getTaskById, updateTask } from "@/app/lib/tasks_controller";
2+
import { NextResponse } from "next/server";
3+
4+
export const GET = async (req: Request) => {
5+
try {
6+
const id = req.url.split("tasks/")[1]
7+
const task = await getTaskById(id)
8+
9+
10+
if (!task) {
11+
return NextResponse.json({ message: "Error" }, { status: 404 });
12+
}
13+
14+
15+
return NextResponse.json({ message: "OK", task}, { status: 200 });
16+
} catch (err) {
17+
NextResponse.json({ message: "Error", err }, { status: 500 });
18+
}
19+
};
20+
21+
export const PUT = async (req: Request) => {
22+
try {
23+
const { title, description, author } = await req.json();
24+
const id = req.url.split("tasks/")[1];
25+
updateTask(id, {title, description, author});
26+
return NextResponse.json({ message: "OK" }, { status: 200 });
27+
} catch (err) {
28+
return NextResponse.json({message: 'Error'}, {status: 500})
29+
}
30+
};
31+
32+
export const DELETE = async (req: Request) => {
33+
try {
34+
const id = req.url.split("tasks/")[1];
35+
36+
const task = getTaskById(id);
37+
38+
if (!task) {
39+
return NextResponse.json({ message: "Error" }, { status: 404 });
40+
}
41+
deleteTask(id);
42+
43+
return NextResponse.json({ message: "OK" }, { status: 200 });
44+
} catch (err) {
45+
NextResponse.json({ message: "Error", err }, { status: 500 });
46+
}
47+
};
48+
49+
export const POST = async (req: Request) => {
50+
console.log("GET");
51+
};

src/app/api/tasks/route.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { getTasks, addTask, deleteTask } from "@/app/lib/tasks_controller"
2+
import { NextResponse } from "next/server"
3+
4+
export const GET = async (req: Request) => {
5+
try {
6+
7+
const tasks = await getTasks();
8+
9+
return NextResponse.json({ message: 'OK', tasks }, { status: 200 });
10+
} catch (err) {
11+
if (err instanceof Error) {
12+
return NextResponse.json({ message: 'Error', err: err.message }, { status: 500 });
13+
} else {
14+
return NextResponse.json({ message: 'Unknown error' }, { status: 500 });
15+
}
16+
}
17+
18+
}
19+
20+
export const POST = async (req: Request) => {
21+
const { title, description, author_id } = await req.json();
22+
23+
try {
24+
const task = { title, description, author_id };
25+
await addTask(task);
26+
return NextResponse.json({ message: "Ok", task }, { status: 201 });
27+
} catch (err) {
28+
if (err instanceof Error) {
29+
return NextResponse.json({ message: 'Error', err: err.message }, { status: 500 });
30+
} else {
31+
return NextResponse.json({ message: 'Unknown error' }, { status: 500 });
32+
}
33+
}
34+
35+
}

src/app/lib/tasks_controller.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { TaskInput, UserInputProps } from '@/models/models';
2+
import supabase from '../../config/database';
3+
4+
5+
export const getTasks = async () => {
6+
const { data, error } = await supabase
7+
.from('tasks')
8+
.select('*');
9+
10+
console.log(data)
11+
12+
if (error) throw new Error(error.message);
13+
return data;
14+
};
15+
16+
17+
export const addTask = async ({title, description, author_id}:TaskInput) => {
18+
const { data, error } = await supabase
19+
.from('tasks')
20+
.insert([
21+
{title, description, author_id, }
22+
]);
23+
24+
if (error) throw new Error(error.message);
25+
return data;
26+
};
27+
28+
29+
export const deleteTask = async (id: string) => {
30+
const { data, error } = await supabase
31+
.from('tasks')
32+
.delete()
33+
.eq('id', id);
34+
if (error) throw new Error(error.message);
35+
if (!data) {
36+
throw new Error("NO TASK FOUND");
37+
}
38+
39+
40+
return data;
41+
};
42+
43+
44+
export const updateTask = async (id:string | number, {title, description, author}:TaskInput) => {
45+
const { data, error } = await supabase
46+
.from('tasks')
47+
.update({ title, description, author })
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+
};
54+
55+
56+
export const getTaskById = async (id: string) => {
57+
58+
console.log(id);
59+
const { data, error } = await supabase
60+
.from('tasks')
61+
.select('*')
62+
.eq('id', id)
63+
.single();
64+
65+
66+
if (error) throw new Error(error.message);
67+
if (!data) throw new Error("NO TASK FOUND");
68+
return data;
69+
};

src/app/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export default function Home() {
55
<main>
66
<p>My Api</p>
77
<a href='/api/posts'>posts</a><br/>
8-
<a href='/api/users'>users</a>
8+
<a href='/api/users'>users</a><br/>
9+
<a href='/api/tasks'>tasks</a>
910
</main>
1011
)
1112
}

src/models/models.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,26 @@ export interface UserInputProps{
1212
email: string
1313
password: string
1414
is_admin: boolean
15-
}
15+
}
16+
17+
export interface TaskProps{
18+
title: string;
19+
description: string;
20+
start_date: string;
21+
status?: "incomplete" | "completed";
22+
id: number | string;
23+
tags: string[];
24+
end_date: string;
25+
author_id: string;
26+
}
27+
28+
export interface TaskInput{
29+
title: string;
30+
description: string;
31+
start_date: string;
32+
status?: "incomplete" | "completed";
33+
tags: string[];
34+
end_date: string;
35+
author_id: string;
36+
}
37+

0 commit comments

Comments
 (0)