11import { NextRequest , NextResponse } from 'next/server' ;
22import { DevlogService , ProjectService } from '@codervisor/devlog-core' ;
3+ import { RouteParams , ApiErrors } from '@/lib/api-utils' ;
34
45// Mark this route as dynamic to prevent static generation
56export const dynamic = 'force-dynamic' ;
67
78// GET /api/projects/[id]/devlogs/[devlogId] - Get specific devlog entry
89export async function GET (
910 request : NextRequest ,
10- { params } : { params : { id : number ; devlogId : number } } ,
11+ { params } : { params : { id : string ; devlogId : string } } ,
1112) {
1213 try {
14+ // Parse and validate parameters
15+ const paramResult = RouteParams . parseProjectAndDevlogId ( params ) ;
16+ if ( ! paramResult . success ) {
17+ return paramResult . response ;
18+ }
19+
20+ const { projectId, devlogId } = paramResult . data ;
21+
1322 const projectService = ProjectService . getInstance ( ) ;
14- const project = await projectService . get ( params . id ) ;
23+ const project = await projectService . get ( projectId ) ;
1524 if ( ! project ) {
16- return NextResponse . json ( { error : 'Project not found' } , { status : 404 } ) ;
25+ return ApiErrors . projectNotFound ( ) ;
1726 }
1827
19- const devlogService = DevlogService . getInstance ( params . id ) ;
20- const entry = await devlogService . get ( params . devlogId ) ;
28+ const devlogService = DevlogService . getInstance ( projectId ) ;
29+ const entry = await devlogService . get ( devlogId ) ;
2130
2231 if ( ! entry ) {
23- return NextResponse . json ( { error : 'Devlog entry not found' } , { status : 404 } ) ;
32+ return ApiErrors . devlogNotFound ( ) ;
2433 }
2534
2635 return NextResponse . json ( entry ) ;
2736 } catch ( error ) {
2837 console . error ( 'Error fetching devlog:' , error ) ;
29- return NextResponse . json ( { error : 'Failed to fetch devlog' } , { status : 500 } ) ;
38+ return ApiErrors . internalError ( 'Failed to fetch devlog' ) ;
3039 }
3140}
3241
3342// PUT /api/projects/[id]/devlogs/[devlogId] - Update devlog entry
3443export async function PUT (
3544 request : NextRequest ,
36- { params } : { params : { id : number ; devlogId : number } } ,
45+ { params } : { params : { id : string ; devlogId : string } } ,
3746) {
3847 try {
48+ // Parse and validate parameters
49+ const paramResult = RouteParams . parseProjectAndDevlogId ( params ) ;
50+ if ( ! paramResult . success ) {
51+ return paramResult . response ;
52+ }
53+
54+ const { projectId, devlogId } = paramResult . data ;
55+
3956 const projectService = ProjectService . getInstance ( ) ;
40- const project = await projectService . get ( params . id ) ;
57+ const project = await projectService . get ( projectId ) ;
4158 if ( ! project ) {
42- return NextResponse . json ( { error : 'Project not found' } , { status : 404 } ) ;
59+ return ApiErrors . projectNotFound ( ) ;
4360 }
4461
4562 const data = await request . json ( ) ;
4663
47- const devlogService = DevlogService . getInstance ( params . id ) ;
64+ const devlogService = DevlogService . getInstance ( projectId ) ;
4865
4966 // Verify entry exists and belongs to project
50- const existingEntry = await devlogService . get ( params . devlogId ) ;
67+ const existingEntry = await devlogService . get ( devlogId ) ;
5168 if ( ! existingEntry ) {
52- return NextResponse . json ( { error : 'Devlog entry not found' } , { status : 404 } ) ;
69+ return ApiErrors . devlogNotFound ( ) ;
5370 }
5471
5572 // Update entry
5673 const updatedEntry = {
5774 ...existingEntry ,
5875 ...data ,
59- id : params . devlogId ,
60- projectId : params . id , // Ensure project context is maintained
76+ id : devlogId ,
77+ projectId : projectId , // Ensure project context is maintained
6178 updatedAt : new Date ( ) . toISOString ( ) ,
6279 } ;
6380
@@ -67,37 +84,45 @@ export async function PUT(
6784 } catch ( error ) {
6885 console . error ( 'Error updating devlog:' , error ) ;
6986 const message = error instanceof Error ? error . message : 'Failed to update devlog' ;
70- return NextResponse . json ( { error : message } , { status : 500 } ) ;
87+ return ApiErrors . internalError ( message ) ;
7188 }
7289}
7390
7491// DELETE /api/projects/[id]/devlogs/[devlogId] - Delete devlog entry
7592export async function DELETE (
7693 request : NextRequest ,
77- { params } : { params : { id : number ; devlogId : number } } ,
94+ { params } : { params : { id : string ; devlogId : string } } ,
7895) {
7996 try {
97+ // Parse and validate parameters
98+ const paramResult = RouteParams . parseProjectAndDevlogId ( params ) ;
99+ if ( ! paramResult . success ) {
100+ return paramResult . response ;
101+ }
102+
103+ const { projectId, devlogId } = paramResult . data ;
104+
80105 const projectService = ProjectService . getInstance ( ) ;
81- const project = await projectService . get ( params . id ) ;
106+ const project = await projectService . get ( projectId ) ;
82107
83108 if ( ! project ) {
84- return NextResponse . json ( { error : 'Project not found' } , { status : 404 } ) ;
109+ return ApiErrors . projectNotFound ( ) ;
85110 }
86111
87- const devlogService = DevlogService . getInstance ( params . id ) ;
112+ const devlogService = DevlogService . getInstance ( projectId ) ;
88113
89114 // Verify entry exists and belongs to project
90- const existingEntry = await devlogService . get ( params . devlogId ) ;
115+ const existingEntry = await devlogService . get ( devlogId ) ;
91116 if ( ! existingEntry ) {
92- return NextResponse . json ( { error : 'Devlog entry not found' } , { status : 404 } ) ;
117+ return ApiErrors . devlogNotFound ( ) ;
93118 }
94119
95- await devlogService . delete ( params . devlogId ) ;
120+ await devlogService . delete ( devlogId ) ;
96121
97122 return NextResponse . json ( { success : true } ) ;
98123 } catch ( error ) {
99124 console . error ( 'Error deleting devlog:' , error ) ;
100125 const message = error instanceof Error ? error . message : 'Failed to delete devlog' ;
101- return NextResponse . json ( { error : message } , { status : 500 } ) ;
126+ return ApiErrors . internalError ( message ) ;
102127 }
103128}
0 commit comments