54 lines
1.5 KiB
Go
Raw Normal View History

package session
import (
"fmt"
"log"
"net/http"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
type Mux struct {
*http.ServeMux
}
func NewMux() *Mux {
mux := new(Mux)
mux.ServeMux = http.NewServeMux()
return mux
}
func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *data.Participant, s *Session) {
for participant := range cp {
mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleParticipant(db, participant, &s.Questions))
for i := range s.Questions {
mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, participant, &s.Questions, int64(i+1)))
}
mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleRetry(participant, &s.Questions))
}
}
func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {
for s := range cs {
(*ss) = append(*ss, s)
participantChan := make(chan *data.Participant)
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("/new-briefing/", s.HandleNewBriefing())
mux.HandleFunc("/new-participant/"+fmt.Sprint(s.ID)+"/", s.HandleNewParticipant(participantChan))
mux.HandleFunc("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db))
go mux.handleParticipants(db, participantChan, s)
}
}