1+ import { NextRequest } from 'next/server' ;
2+ import { DocumentService , DevlogService } from '@codervisor/devlog-core/server' ;
3+ import { ApiErrors , createSuccessResponse , RouteParams , ServiceHelper } from '@/lib/api/api-utils' ;
4+ import { RealtimeEventType } from '@/lib/realtime' ;
5+
6+ // Mark this route as dynamic to prevent static generation
7+ export const dynamic = 'force-dynamic' ;
8+
9+ // GET /api/projects/[name]/devlogs/[devlogId]/documents/[documentId] - Get specific document
10+ export async function GET (
11+ request : NextRequest ,
12+ { params } : { params : { name : string ; devlogId : string ; documentId : string } } ,
13+ ) {
14+ try {
15+ // Parse and validate parameters
16+ const projectResult = RouteParams . parseProjectName ( params ) ;
17+ if ( ! projectResult . success ) {
18+ return projectResult . response ;
19+ }
20+
21+ const { projectName } = projectResult . data ;
22+ const { devlogId, documentId } = params ;
23+
24+ if ( ! devlogId || ! documentId ) {
25+ return ApiErrors . invalidRequest ( 'Missing devlogId or documentId' ) ;
26+ }
27+
28+ // Parse devlogId as number
29+ const parsedDevlogId = parseInt ( devlogId ) ;
30+ if ( isNaN ( parsedDevlogId ) ) {
31+ return ApiErrors . invalidRequest ( 'Invalid devlogId' ) ;
32+ }
33+
34+ // Get project using helper
35+ const projectHelperResult = await ServiceHelper . getProjectByNameOrFail ( projectName ) ;
36+ if ( ! projectHelperResult . success ) {
37+ return projectHelperResult . response ;
38+ }
39+
40+ const project = projectHelperResult . data . project ;
41+
42+ // Verify devlog exists
43+ const devlogService = DevlogService . getInstance ( project . id ) ;
44+ const devlog = await devlogService . get ( parsedDevlogId , false ) ;
45+ if ( ! devlog ) {
46+ return ApiErrors . devlogNotFound ( ) ;
47+ }
48+
49+ // Get document
50+ const documentService = DocumentService . getInstance ( project . id ) ;
51+ const document = await documentService . getDocument ( documentId ) ;
52+
53+ if ( ! document ) {
54+ return ApiErrors . notFound ( 'Document not found' ) ;
55+ }
56+
57+ // Verify document belongs to the specified devlog
58+ if ( document . devlogId !== parsedDevlogId ) {
59+ return ApiErrors . notFound ( 'Document not found' ) ;
60+ }
61+
62+ return createSuccessResponse ( document ) ;
63+ } catch ( error ) {
64+ console . error ( 'Error fetching document:' , error ) ;
65+ return ApiErrors . internalError ( 'Failed to fetch document' ) ;
66+ }
67+ }
68+
69+ // DELETE /api/projects/[name]/devlogs/[devlogId]/documents/[documentId] - Delete document
70+ export async function DELETE (
71+ request : NextRequest ,
72+ { params } : { params : { name : string ; devlogId : string ; documentId : string } } ,
73+ ) {
74+ try {
75+ // Parse and validate parameters
76+ const projectResult = RouteParams . parseProjectName ( params ) ;
77+ if ( ! projectResult . success ) {
78+ return projectResult . response ;
79+ }
80+
81+ const { projectName } = projectResult . data ;
82+ const { devlogId, documentId } = params ;
83+
84+ if ( ! devlogId || ! documentId ) {
85+ return ApiErrors . invalidRequest ( 'Missing devlogId or documentId' ) ;
86+ }
87+
88+ // Parse devlogId as number
89+ const parsedDevlogId = parseInt ( devlogId ) ;
90+ if ( isNaN ( parsedDevlogId ) ) {
91+ return ApiErrors . invalidRequest ( 'Invalid devlogId' ) ;
92+ }
93+
94+ // Get project using helper
95+ const projectHelperResult = await ServiceHelper . getProjectByNameOrFail ( projectName ) ;
96+ if ( ! projectHelperResult . success ) {
97+ return projectHelperResult . response ;
98+ }
99+
100+ const project = projectHelperResult . data . project ;
101+
102+ // Verify devlog exists
103+ const devlogService = DevlogService . getInstance ( project . id ) ;
104+ const devlog = await devlogService . get ( parsedDevlogId , false ) ;
105+ if ( ! devlog ) {
106+ return ApiErrors . devlogNotFound ( ) ;
107+ }
108+
109+ // Verify document exists and belongs to the devlog
110+ const documentService = DocumentService . getInstance ( project . id ) ;
111+ const document = await documentService . getDocument ( documentId ) ;
112+
113+ if ( ! document || document . devlogId !== parsedDevlogId ) {
114+ return ApiErrors . notFound ( 'Document not found' ) ;
115+ }
116+
117+ // Delete document
118+ const deleted = await documentService . deleteDocument ( documentId ) ;
119+
120+ if ( ! deleted ) {
121+ return ApiErrors . internalError ( 'Failed to delete document' ) ;
122+ }
123+
124+ return createSuccessResponse (
125+ { message : 'Document deleted successfully' } ,
126+ {
127+ sseEventType : RealtimeEventType . DEVLOG_UPDATED ,
128+ }
129+ ) ;
130+ } catch ( error ) {
131+ console . error ( 'Error deleting document:' , error ) ;
132+ return ApiErrors . internalError ( 'Failed to delete document' ) ;
133+ }
134+ }
0 commit comments