File tree Expand file tree Collapse file tree 4 files changed +60
-2
lines changed
migrations/20250402170145_add_cascade_delete Expand file tree Collapse file tree 4 files changed +60
-2
lines changed Original file line number Diff line number Diff line change 1+ -- DropForeignKey
2+ ALTER TABLE " UserAction" DROP CONSTRAINT " UserAction_pollId_fkey" ;
3+
4+ -- DropForeignKey
5+ ALTER TABLE " Vote" DROP CONSTRAINT " Vote_pollId_fkey" ;
6+
7+ -- AddForeignKey
8+ ALTER TABLE " UserAction" ADD CONSTRAINT " UserAction_pollId_fkey" FOREIGN KEY (" pollId" ) REFERENCES " Poll" (" pollId" ) ON DELETE CASCADE ON UPDATE CASCADE;
9+
10+ -- AddForeignKey
11+ ALTER TABLE " Vote" ADD CONSTRAINT " Vote_pollId_fkey" FOREIGN KEY (" pollId" ) REFERENCES " Poll" (" pollId" ) ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ model UserAction {
3232 pollId Int
3333 type ActionType
3434 user User @relation (fields : [userId ] , references : [id ] )
35- poll Poll @relation (fields : [pollId ] , references : [pollId ] )
35+ poll Poll @relation (fields : [pollId ] , references : [pollId ] , onDelete : Cascade )
3636
3737}
3838
@@ -62,7 +62,7 @@ model Vote {
6262 weightDistribution Json
6363 proof String
6464 user User @relation (fields : [userId ] , references : [id ] )
65- poll Poll @relation (fields : [pollId ] , references : [pollId ] )
65+ poll Poll @relation (fields : [pollId ] , references : [pollId ] , onDelete : Cascade )
6666}
6767
6868enum ActionType {
Original file line number Diff line number Diff line change @@ -46,4 +46,18 @@ export class PollController {
4646 . json ( { message : 'Internal server error' , error : error . message } ) ;
4747 }
4848 }
49+
50+ @Delete ( ':id' )
51+ async deletePoll ( @Param ( 'id' ) id : number , @Res ( ) res : Response ) {
52+ const userId = 1 ; // need to implement Auth
53+ try {
54+ const poll = await this . pollService . deletePoll ( userId , Number ( id ) ) ;
55+
56+ return res . status ( 200 ) . json ( { message : 'Poll deleted' , poll : poll } ) ;
57+ } catch ( error ) {
58+ return res
59+ . status ( 500 )
60+ . json ( { message : 'Internal server error' , error : error . message } ) ;
61+ }
62+ }
4963}
Original file line number Diff line number Diff line change @@ -134,4 +134,37 @@ export class PollService {
134134 throw new Error ( 'Database query failed' ) ;
135135 }
136136 }
137+
138+ async deletePoll ( userId : number , pollId : number ) {
139+ const poll = await this . databaseService . poll . findUnique ( {
140+ where : { pollId } ,
141+ } ) ;
142+
143+ if ( ! poll ) {
144+ throw new Error ( 'Poll not found' ) ;
145+ }
146+ if ( poll . authorUserId !== userId ) {
147+ throw new Error ( 'User Not Authorized' ) ;
148+ }
149+
150+ return this . databaseService . $transaction ( async ( tx ) => {
151+ const deleted = await tx . poll . delete ( {
152+ where : {
153+ pollId,
154+ } ,
155+ } ) ;
156+
157+ // Update user's pollsCreatedCount
158+ await tx . user . update ( {
159+ where : { id : deleted . authorUserId } ,
160+ data : {
161+ pollsCreatedCount : {
162+ decrement : 1 ,
163+ } ,
164+ } ,
165+ } ) ;
166+
167+ return deleted ;
168+ } ) ;
169+ }
137170}
You can’t perform that action at this time.
0 commit comments