Skip to content

Commit bc6b292

Browse files
committed
Routes for user participation
1 parent 630d795 commit bc6b292

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

backend/main.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,95 @@ func main() {
349349
rw.Write(data)
350350
})
351351

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+
352441
srv := &http.Server{
353442
Addr: ":8080",
354443
Handler: router,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE user_participation
2+
ADD COLUMN id UUID;
3+
4+
ALTER TABLE user_participation
5+
DROP CONSTRAINT IF EXISTS user_participation_pk;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE user_participation
2+
DROP COLUMN IF EXISTS id;
3+
4+
ALTER TABLE user_participation
5+
ADD CONSTRAINT user_participation_pk PRIMARY KEY (cluesheet_id, ipa_uid);

backend/model.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ type Clue struct {
4545
Children []*Clue `json:",omitzero"`
4646
}
4747

48+
type UserParticipation struct {
49+
Cluesheet_id uuid.UUID
50+
Ipa_uid string
51+
Hidden bool
52+
}
53+
4854
type ClueRelation struct {
4955
Id uuid.UUID
5056
Parent_id uuid.UUID

0 commit comments

Comments
 (0)