/* * Sicherheitsunterweisung * Copyright (C) 2023 Jason Streifling * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program. If not, see . */ package session import ( "fmt" "log" "net/http" "streifling.com/jason/sicherheitsunterweisung/packages/data" ) func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *BriefingParticipant, s *Session) { for p := range cp { p.GivenAnswers = make([]int, len(s.Questions)) mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleParticipant(db, p)) var i int for i = range s.Questions { mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, p, int64(i+1))) } mux.HandleFunc("/allow-retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleAllowRetry(p)) mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRetry(p, &i)) mux.HandleFunc("/refresh-summary/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRefresh(p)) } } func NewMux() *Mux { return &Mux{ServeMux: http.NewServeMux()} } func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) { for s := range cs { (*ss) = append((*ss), s) participantChan := make(chan *BriefingParticipant) questionIDs := make([]string, 4) for i := 0; i < len(questionIDs); i++ { questionIDs[i] = fmt.Sprint(i + 1) } var err error s.Questions, err = db.GetQuestions(questionIDs) if err != nil { log.Fatalln(err) } mux.HandleFunc("/search/"+fmt.Sprint(s.ID)+"/", s.HandleSearch(db)) mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.ID)+"/", s.HandleNewBriefing()) mux.HandleFunc("/new-participant/"+fmt.Sprint(s.ID)+"/", s.HandleNewParticipant(participantChan)) mux.HandleFunc("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db)) mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingDone()) go mux.handleParticipants(db, participantChan, s) } }