-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathroute.ts
More file actions
113 lines (97 loc) · 3.35 KB
/
route.ts
File metadata and controls
113 lines (97 loc) · 3.35 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { NextRequest } from 'next/server';
import { db, finance } from '@/db';
import { getAuthenticatedUser, createErrorResponse, createSuccessResponse } from '@/lib/auth-utils';
import { updateAssetSchema } from '@/lib/validations/finance';
import { eq, and } from 'drizzle-orm';
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const user = await getAuthenticatedUser();
if (!user) {
return createErrorResponse('Unauthorized', 401);
}
const asset = await db
.select()
.from(finance.assets)
.where(and(
eq(finance.assets.id, id),
eq(finance.assets.userId, user.id)
))
.limit(1);
if (asset.length === 0) {
return createErrorResponse('Asset not found', 404);
}
return createSuccessResponse(asset[0]);
} catch (error) {
console.error('Error fetching asset:', error);
return createErrorResponse('Internal server error', 500);
}
}
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const user = await getAuthenticatedUser();
if (!user) {
return createErrorResponse('Unauthorized', 401);
}
const body = await request.json();
const validationResult = updateAssetSchema.safeParse(body);
if (!validationResult.success) {
return createErrorResponse(
validationResult.error.issues.map(e => e.message).join(', '),
400
);
}
const updateData = { ...validationResult.data, updatedAt: new Date() };
if (updateData.purchaseDate) {
updateData.purchaseDate = new Date(updateData.purchaseDate);
}
const updatedAsset = await db
.update(finance.assets)
.set(updateData)
.where(and(
eq(finance.assets.id, id),
eq(finance.assets.userId, user.id)
))
.returning();
if (updatedAsset.length === 0) {
return createErrorResponse('Asset not found', 404);
}
return createSuccessResponse(updatedAsset[0]);
} catch (error) {
console.error('Error updating asset:', error);
return createErrorResponse('Internal server error', 500);
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const user = await getAuthenticatedUser();
if (!user) {
return createErrorResponse('Unauthorized', 401);
}
const deletedAsset = await db
.delete(finance.assets)
.where(and(
eq(finance.assets.id, id),
eq(finance.assets.userId, user.id)
))
.returning();
if (deletedAsset.length === 0) {
return createErrorResponse('Asset not found', 404);
}
return createSuccessResponse({ message: 'Asset deleted successfully' });
} catch (error) {
console.error('Error deleting asset:', error);
return createErrorResponse('Internal server error', 500);
}
}