1+ import { NextRequest } from 'next/server'
2+ import { eq , sql } from 'drizzle-orm'
3+ import { createLogger } from '@/lib/logs/console-logger'
4+ import { createErrorResponse , createSuccessResponse } from '@/app/api/workflows/utils'
5+ import { db } from '@/db'
6+ import * as schema from '@/db/schema'
7+
8+ const logger = createLogger ( 'MarketplaceViewAPI' )
9+
10+ /**
11+ * POST handler for incrementing the view count when a workflow card is clicked
12+ * This endpoint is called from the WorkflowCard component's onClick handler
13+ *
14+ * The ID parameter is the marketplace entry ID, not the workflow ID
15+ */
16+ export async function POST ( request : NextRequest , { params } : { params : Promise < { id : string } > } ) {
17+ const requestId = crypto . randomUUID ( ) . slice ( 0 , 8 )
18+
19+ try {
20+ const { id } = await params
21+
22+ // Find the marketplace entry for this marketplace ID
23+ const marketplaceEntry = await db
24+ . select ( {
25+ id : schema . marketplace . id ,
26+ } )
27+ . from ( schema . marketplace )
28+ . where ( eq ( schema . marketplace . id , id ) )
29+ . limit ( 1 )
30+ . then ( ( rows ) => rows [ 0 ] )
31+
32+ if ( ! marketplaceEntry ) {
33+ logger . warn ( `[${ requestId } ] No marketplace entry found with ID: ${ id } ` )
34+ return createErrorResponse ( 'Marketplace entry not found' , 404 )
35+ }
36+
37+ // Increment the view count for this workflow
38+ await db
39+ . update ( schema . marketplace )
40+ . set ( {
41+ views : sql `${ schema . marketplace . views } + 1`
42+ } )
43+ . where ( eq ( schema . marketplace . id , id ) )
44+
45+ logger . info ( `[${ requestId } ] Incremented view count for marketplace entry: ${ id } ` )
46+
47+ return createSuccessResponse ( {
48+ success : true ,
49+ } )
50+ } catch ( error ) {
51+ logger . error (
52+ `[${ requestId } ] Error incrementing view count for marketplace entry: ${ ( await params ) . id } ` ,
53+ error
54+ )
55+ return createErrorResponse ( 'Failed to track view' , 500 )
56+ }
57+ }
0 commit comments