-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathroute.ts
More file actions
74 lines (64 loc) · 2.09 KB
/
route.ts
File metadata and controls
74 lines (64 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { auth } from "@/lib/auth";
import { createAsset, getAssetsByUserId, getAssetById, updateAsset, deleteAsset } from "@/lib/services/financeService";
import { NextRequest } from "next/server";
// Create a new asset
export async function POST(request: NextRequest) {
try {
const session = await auth.api.getSession({
headers: request.headers,
});
if (!session) {
return new Response(
JSON.stringify({ error: "Unauthorized" }),
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
const data = await request.json();
// Validate required fields
if (!data.name || data.value === undefined) {
return new Response(
JSON.stringify({ error: "Name and value are required" }),
{ status: 400, headers: { "Content-Type": "application/json" } }
);
}
const asset = await createAsset({
...data,
userId: session.user.id,
});
return new Response(
JSON.stringify(asset),
{ status: 201, headers: { "Content-Type": "application/json" } }
);
} catch (error) {
console.error("Error creating asset:", error);
return new Response(
JSON.stringify({ error: "Failed to create asset" }),
{ status: 500, headers: { "Content-Type": "application/json" } }
);
}
}
// Get all assets for the authenticated user
export async function GET(request: NextRequest) {
try {
const session = await auth.api.getSession({
headers: request.headers,
});
if (!session) {
return new Response(
JSON.stringify({ error: "Unauthorized" }),
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
const assets = await getAssetsByUserId(session.user.id);
return new Response(
JSON.stringify(assets),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
} catch (error) {
console.error("Error fetching assets:", error);
return new Response(
JSON.stringify({ error: "Failed to fetch assets" }),
{ status: 500, headers: { "Content-Type": "application/json" } }
);
}
}