@@ -6,6 +6,7 @@ import type { CardResponse } from '../services/cardService';
66import type { Card } from '@devcard/shared' ;
77import type { FastifyInstance , FastifyRequest , FastifyReply } from 'fastify' ;
88import { CardVisibility } from '@prisma/client' ;
9+ import { request } from 'http' ;
910
1011export interface CreateCardBody {
1112 title : string ;
@@ -129,7 +130,6 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
129130 } ) ;
130131
131132 // ─── Set Default Card ───
132-
133133 app . put ( '/:id/default' , async ( request : FastifyRequest < { Params : CardParams } > , reply : FastifyReply ) : Promise < object | void > => {
134134 const userId = ( request . user as { id : string } ) . id ;
135135 const { id } = request . params ;
@@ -182,4 +182,48 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
182182 handleDbError ( error , request , reply )
183183 }
184184 } )
185+
186+ //Share card
187+ app . post ( '/:id/share' , async ( request : FastifyRequest < { Params : { id : string } } > , reply :FastifyReply ) => {
188+ const cardId = request . params . id ;
189+ const userId = request . user . id ;
190+
191+ try {
192+ const link = await cardService . shareCard ( app , userId , cardId ) ;
193+ return reply . status ( 200 ) . send ( link )
194+ } catch ( error :any ) {
195+ if ( error ?. code === 'CARD_NOT_FOUND' ) {
196+ return reply . status ( 404 ) . send ( {
197+ error : error . message ,
198+ } ) ;
199+ }
200+ if ( error ?. code === 'CARD_PRIVATE' ) {
201+ return reply . status ( 403 ) . send ( {
202+ error : error . message ,
203+ } ) ;
204+ }
205+
206+ app . log . error ( error )
207+ handleDbError ( error , request , reply )
208+ }
209+ } )
210+
211+ //Get shared card
212+ app . get ( '/share/:slug' , async ( request : FastifyRequest < { Params : { slug : string } } > , reply : FastifyReply ) => {
213+ const paramsSlug = request . params . slug ;
214+
215+ try {
216+ const card = await cardService . getSharedCard ( app , paramsSlug )
217+ return reply . status ( 200 ) . send ( card )
218+ } catch ( error :any ) {
219+ if ( error ?. code === 'CARD_NOT_FOUND' ) {
220+ return reply . status ( 404 ) . send ( {
221+ error : error . message ,
222+ } ) ;
223+ }
224+
225+ app . log . error ( error )
226+ handleDbError ( error , request , reply )
227+ }
228+ } )
185229}
0 commit comments