@@ -349,6 +349,95 @@ func main() {
349
349
rw .Write (data )
350
350
})
351
351
352
+ v1 .Path ("/cluesheet/{cluesheet_id}/participation/{ipa_uid}" ).Methods ("GET" ).HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
353
+ vars := mux .Vars (r )
354
+ cluesheet_id , err := uuid .Parse (vars ["cluesheet_id" ])
355
+ if err != nil {
356
+ http .Error (rw , fmt .Sprintf ("failed to parse uuid '%s': '%s'" , vars ["id" ], err ), 400 )
357
+ return
358
+ }
359
+
360
+ ipa_uid := vars ["ipa_uid" ]
361
+ // todo validation
362
+
363
+ rows , err := conn .Query (r .Context (), `select * from user_participation where cluesheet_id = $1 and ipa_uid = $2` , cluesheet_id , ipa_uid )
364
+ if err != nil {
365
+ http .Error (rw , fmt .Sprintf ("failed to query for user participation on '%s' for user '%s': %s" , cluesheet_id , ipa_uid , err ), 500 )
366
+ return
367
+ }
368
+
369
+ participation , err := pgx .CollectOneRow [UserParticipation ](rows , pgx .RowToStructByNameLax [UserParticipation ])
370
+ if err == pgx .ErrNoRows {
371
+ // if no stored result, there's no hiding
372
+ participation = UserParticipation {
373
+ Cluesheet_id : cluesheet_id ,
374
+ Ipa_uid : ipa_uid ,
375
+ Hidden : false ,
376
+ }
377
+ } else if err != nil {
378
+ http .Error (rw , fmt .Sprintf ("failed to query for user participation on '%s' for user '%s': %s" , cluesheet_id , ipa_uid , err ), 500 )
379
+ return
380
+ }
381
+
382
+ data , err := json .Marshal (participation )
383
+ if err != nil {
384
+ http .Error (rw , "Failed to marshal data" , 500 )
385
+ fmt .Println (err .Error ())
386
+ return
387
+ }
388
+
389
+ rw .Header ().Set ("Content-Type" , "application/json" )
390
+ rw .Write (data )
391
+ })
392
+
393
+ v1 .Path ("/cluesheet/{cluesheet_id}/participation/{ipa_uid}" ).Methods ("POST" ).HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
394
+ vars := mux .Vars (r )
395
+ cluesheet_id , err := uuid .Parse (vars ["cluesheet_id" ])
396
+ if err != nil {
397
+ http .Error (rw , fmt .Sprintf ("failed to parse uuid '%s': '%s'" , vars ["id" ], err ), 400 )
398
+ return
399
+ }
400
+
401
+ ipa_uid := vars ["ipa_uid" ]
402
+ // todo validation
403
+
404
+ body , err := io .ReadAll (r .Body )
405
+ if err != nil {
406
+ http .Error (rw , fmt .Sprintf ("failed to read request: %s" , err .Error ()), 400 )
407
+ return
408
+ }
409
+
410
+ var params struct {
411
+ Hidden bool
412
+ }
413
+
414
+ err = json .Unmarshal (body , & params )
415
+ if err != nil {
416
+ http .Error (rw , fmt .Sprintf ("failed to parse request: %s" , err .Error ()), 400 )
417
+ return
418
+ }
419
+
420
+ participation := UserParticipation {Cluesheet_id : cluesheet_id , Ipa_uid : ipa_uid , Hidden : params .Hidden }
421
+
422
+ log .FromContext (r .Context ()).Info ("participation" , zap .Any ("participation" , participation ))
423
+ _ , err = conn .Exec (r .Context (), `insert into user_participation(cluesheet_id, ipa_uid, hidden) values ($1, $2, $3) on conflict (cluesheet_id, ipa_uid) do update set hidden = $3` , participation .Cluesheet_id , participation .Ipa_uid , participation .Hidden )
424
+ if err != nil {
425
+ http .Error (rw , "failed to store clue parent" , 500 )
426
+ fmt .Println (err .Error ())
427
+ return
428
+ }
429
+
430
+ data , err := json .Marshal (participation )
431
+ if err != nil {
432
+ http .Error (rw , "Failed to marshal data" , 500 )
433
+ fmt .Println (err .Error ())
434
+ return
435
+ }
436
+
437
+ rw .Header ().Set ("Content-Type" , "application/json" )
438
+ rw .Write (data )
439
+ })
440
+
352
441
srv := & http.Server {
353
442
Addr : ":8080" ,
354
443
Handler : router ,
0 commit comments