diff --git a/packages/server/briefing.go b/packages/server/briefing.go index 9254bbb..cc1e679 100644 --- a/packages/server/briefing.go +++ b/packages/server/briefing.go @@ -24,11 +24,11 @@ import ( "streifling.com/jason/sicherheitsunterweisung/packages/data" ) -type Briefing struct { +type briefing struct { *data.Briefing - UUID uuid.UUID - Participants []*Participant - Questions []data.Question + uuid uuid.UUID + participants []*participant + questions []data.Question } func generateLogin() (string, error) { @@ -41,33 +41,33 @@ func generateLogin() (string, error) { return hex.EncodeToString(bs), nil } -func (b *Briefing) HandleNewParticipant(cp chan<- *Participant) http.HandlerFunc { +func (b *briefing) handleNewParticipant(cp chan<- *participant) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var err error - participant := Participant{ - Participant: new(data.Participant), - NoIncorrect: -1, - AllowRetry: false, + participant := participant{ + Participant: new(data.Participant), + numberIncorrect: -1, + allowRetry: false, } - participant.Login, err = generateLogin() + participant.login, err = generateLogin() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err) } - b.Participants = append(b.Participants, &participant) + b.participants = append(b.participants, &participant) cp <- &participant data := participantHTMLData{ - BriefingID: b.UUID, - Participant: Participant{Login: participant.Login}, + briefingID: b.uuid, + participant: participant, } template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data) } } -func (b *Briefing) HandleBriefingForm(db *data.DB, s *Session) http.HandlerFunc { +func (b *briefing) handleBriefingForm(db *data.DB, s *Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { b.DateTime = time.Now().Format("2006-01-02 15:04:05") b.Location = r.PostFormValue("location") @@ -81,13 +81,13 @@ func (b *Briefing) HandleBriefingForm(db *data.DB, s *Session) http.HandlerFunc } data := summaryHTMLData{ - SessionID: s.UUID, - BriefingID: b.UUID, - ParticipantsData: make([]participantHTMLData, len(b.Participants)), + sessionID: s.uuid, + briefingID: b.uuid, + participantsData: make([]participantHTMLData, len(b.participants)), } - for i, p := range b.Participants { - data.ParticipantsData[i].BriefingID = b.UUID - data.ParticipantsData[i].Participant = *p + for i, p := range b.participants { + data.participantsData[i].briefingID = b.uuid + data.participantsData[i].participant = *p } template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "content", data) diff --git a/packages/server/htmlStructs.go b/packages/server/htmlStructs.go index c38c31d..8cff19b 100644 --- a/packages/server/htmlStructs.go +++ b/packages/server/htmlStructs.go @@ -17,41 +17,41 @@ import ( ) type tableHTMLData struct { - SessionID uuid.UUID - OTD []data.OverviewTableData + sessionID uuid.UUID + otd []data.OverviewTableData } type participantHTMLData struct { - BriefingID uuid.UUID - Participant + briefingID uuid.UUID + participant } type questionHTMLData struct { participantHTMLData - QuestionID int64 - Question data.Question + questionID int64 + question data.Question } type resultAnswer struct { - ID int64 - Text string - Correct bool - Chosen bool - IsImage bool // TODO: relocate to sessionStructs if possible + id int64 + text string + correct bool + chosen bool + isImage bool // TODO: relocate to sessionStructs if possible } type resultQuestion struct { - Text string - Answers []resultAnswer + text string + answers []resultAnswer } type resultHTMLData struct { participantHTMLData - Questions []resultQuestion + questions []resultQuestion } type summaryHTMLData struct { - SessionID uuid.UUID - BriefingID uuid.UUID - ParticipantsData []participantHTMLData + sessionID uuid.UUID + briefingID uuid.UUID + participantsData []participantHTMLData } diff --git a/packages/server/login.go b/packages/server/login.go index 7ea55fc..38edb7d 100644 --- a/packages/server/login.go +++ b/packages/server/login.go @@ -21,6 +21,19 @@ import ( "streifling.com/jason/sicherheitsunterweisung/packages/data" ) +func findCorrectLogin(l string, ss *[]*Session) (*briefing, *participant, bool) { + for _, s := range *ss { + for _, b := range s.briefings { + for _, p := range b.participants { + if l == p.login { + return b, p, true + } + } + } + } + return nil, nil, false +} + func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { instructors, err := db.GetInstructors() @@ -32,15 +45,15 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H for _, i := range instructors { if r.PostFormValue("login") == fmt.Sprint(i.ID) { session := Session{ - UUID: uuid.New(), - Instructor: *i, - Briefings: make([]*Briefing, 0), + uuid: uuid.New(), + instructor: *i, + briefings: make([]*briefing, 0), } (*ss) = append((*ss), &session) cs <- &session - data := tableHTMLData{SessionID: session.UUID} - data.OTD, err = db.GetAllOverviewTableData() + data := tableHTMLData{sessionID: session.uuid} + data.otd, err = db.GetAllOverviewTableData() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err) @@ -54,26 +67,13 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H } } -func findCorrectLogin(l string, ss *[]*Session) (*Briefing, *Participant, bool) { - for _, s := range *ss { - for _, b := range s.Briefings { - for _, p := range b.Participants { - if l == p.Login { - return b, p, true - } - } - } - } - return nil, nil, false -} - func HandleExternalLogin(ss *[]*Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { briefing, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss) if loginCorrect { data := participantHTMLData{ - BriefingID: briefing.UUID, - Participant: Participant{Login: participant.Login}, + briefingID: briefing.uuid, + participant: *participant, } template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data) diff --git a/packages/server/mux.go b/packages/server/mux.go index 95dcf7f..19addf5 100644 --- a/packages/server/mux.go +++ b/packages/server/mux.go @@ -28,23 +28,7 @@ func NewMux() *Mux { return &Mux{ServeMux: http.NewServeMux()} } -func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *Participant, b *Briefing) { - for p := range cp { - p.GivenAnswers = make([]int, len(b.Questions)) - - mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleParticipant(db, b)) - mux.HandleFunc("/end-video/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleEndOfVideo(b)) - var i int - for i = range b.Questions { - mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", p.HandleAnswer(db, b, int64(i+1))) - } - mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleAllowRetry()) - mux.HandleFunc("/retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRetry(b, &i)) - mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRefresh(b)) - } -} - -func getQuestions(db *data.DB, b *Briefing) { +func getQuestions(db *data.DB, b *briefing) { questionIDs := make([]string, 4) for i := 0; i < len(questionIDs); i++ { @@ -52,43 +36,59 @@ func getQuestions(db *data.DB, b *Briefing) { } var err error - b.Questions, err = db.GetQuestions(questionIDs) + b.questions, err = db.GetQuestions(questionIDs) if err != nil { log.Fatalln(err) } - for i := range b.Questions { - for j := range b.Questions[i].Answers { - if parts := strings.Split(b.Questions[i].Answers[j].Text, ":/"); parts[0] == "file" { - b.Questions[i].Answers[j].Text = parts[1] - b.Questions[i].Answers[j].IsImage = true + for i := range b.questions { + for j := range b.questions[i].Answers { + if parts := strings.Split(b.questions[i].Answers[j].Text, ":/"); parts[0] == "file" { + b.questions[i].Answers[j].Text = parts[1] + b.questions[i].Answers[j].IsImage = true } else { - b.Questions[i].Answers[j].IsImage = false + b.questions[i].Answers[j].IsImage = false } } } } -func (mux *Mux) handleBriefings(db *data.DB, cb <-chan *Briefing, s *Session) { - participantChan := make(chan *Participant) +func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *participant, b *briefing) { + for p := range cp { + p.givenAnswers = make([]int, len(b.questions)) + + mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/", p.handleParticipant(db, b)) + mux.HandleFunc("/end-video/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/", p.handleEndOfVideo(b)) + var i int + for i = range b.questions { + mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/"+fmt.Sprint(i+1)+"/", p.handleAnswer(db, b, int64(i+1))) + } + mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/", p.handleAllowRetry()) + mux.HandleFunc("/retry/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/", p.handleRetry(b, &i)) + mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/", p.handleRefresh(b)) + } +} + +func (mux *Mux) handleBriefings(db *data.DB, cb <-chan *briefing, s *Session) { + participantChan := make(chan *participant) for b := range cb { getQuestions(db, b) - mux.HandleFunc("/new-participant/"+fmt.Sprint(b.UUID)+"/", b.HandleNewParticipant(participantChan)) - mux.HandleFunc("/submit-form/"+fmt.Sprint(b.UUID)+"/", b.HandleBriefingForm(db, s)) + mux.HandleFunc("/new-participant/"+fmt.Sprint(b.uuid)+"/", b.handleNewParticipant(participantChan)) + mux.HandleFunc("/submit-form/"+fmt.Sprint(b.uuid)+"/", b.handleBriefingForm(db, s)) go mux.handleParticipants(db, participantChan, b) } } func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) { - briefingChan := make(chan *Briefing) + briefingChan := make(chan *briefing) for s := range cs { (*ss) = append((*ss), s) - mux.HandleFunc("/search/"+fmt.Sprint(s.UUID)+"/", s.HandleSearch(db)) - mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.UUID)+"/", s.HandleNewBriefing(briefingChan)) - mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.UUID)+"/", s.HandleBriefingDone(db)) + mux.HandleFunc("/search/"+fmt.Sprint(s.uuid)+"/", s.handleSearch(db)) + mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.uuid)+"/", s.handleNewBriefing(briefingChan)) + mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.uuid)+"/", s.handleBriefingDone(db)) go mux.handleBriefings(db, briefingChan, s) } diff --git a/packages/server/participant.go b/packages/server/participant.go index 06637c6..2715659 100644 --- a/packages/server/participant.go +++ b/packages/server/participant.go @@ -21,45 +21,45 @@ import ( "streifling.com/jason/sicherheitsunterweisung/packages/data" ) -type Participant struct { +type participant struct { *data.Participant - Login string - GivenAnswers []int - NoIncorrect int - AllowRetry bool + login string + givenAnswers []int + numberIncorrect int + allowRetry bool } -func handleGivenAnswer(p *Participant, i int64, r *http.Request) error { +func handleGivenAnswer(p *participant, i int64, r *http.Request) error { answer, err := strconv.Atoi(r.PostFormValue("answer")) if err != nil { return fmt.Errorf("error: handleGivenAnswer: strconv.Atoi(): %v", err) } - p.GivenAnswers[i] = answer + p.givenAnswers[i] = answer return nil } func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestion { questions := make([]resultQuestion, len(sq)) for i, q := range sq { - questions[i].Text = q.Text - questions[i].Answers = make([]resultAnswer, len(q.Answers)) + questions[i].text = q.Text + questions[i].answers = make([]resultAnswer, len(q.Answers)) for j := range q.Answers { - questions[i].Answers[j].ID = q.Answers[j].ID - questions[i].Answers[j].Text = q.Answers[j].Text - questions[i].Answers[j].IsImage = q.Answers[j].IsImage + questions[i].answers[j].id = q.Answers[j].ID + questions[i].answers[j].text = q.Answers[j].Text + questions[i].answers[j].isImage = q.Answers[j].IsImage if j+1 == q.Correct { - questions[i].Answers[j].Correct = true + questions[i].answers[j].correct = true } else { - questions[i].Answers[j].Correct = false + questions[i].answers[j].correct = false } if j+1 == givenAnswers[i] { - questions[i].Answers[j].Chosen = true + questions[i].answers[j].chosen = true } else { - questions[i].Answers[j].Chosen = false + questions[i].answers[j].chosen = false } } } @@ -67,11 +67,11 @@ func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestio return questions } -func (p *Participant) HandleParticipant(db *data.DB, b *Briefing) http.HandlerFunc { +func (p *participant) handleParticipant(db *data.DB, b *briefing) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.Login)) - p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.Login)) - p.Company = r.PostFormValue("company-" + fmt.Sprint(p.Login)) + p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.login)) + p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.login)) + p.Company = r.PostFormValue("company-" + fmt.Sprint(p.login)) err := db.WriteParticipant(p.Participant) if err != nil { @@ -80,17 +80,17 @@ func (p *Participant) HandleParticipant(db *data.DB, b *Briefing) http.HandlerFu } data := participantHTMLData{ - BriefingID: b.UUID, - Participant: *p, + briefingID: b.uuid, + participant: *p, } template.Must(template.ParseFiles("templates/video.html")).ExecuteTemplate(w, "content", data) } } -func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.HandlerFunc { +func (p *participant) handleAnswer(db *data.DB, b *briefing, i int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if i < int64(len(b.Questions)) { + if i < int64(len(b.questions)) { if err := handleGivenAnswer(p, i-1, r); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err) @@ -98,11 +98,11 @@ func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.Handl data := questionHTMLData{ participantHTMLData: participantHTMLData{ - BriefingID: b.UUID, - Participant: Participant{Login: p.Login}, + briefingID: b.uuid, + participant: participant{login: p.login}, }, - QuestionID: i + 1, - Question: b.Questions[i], + questionID: i + 1, + question: b.questions[i], } template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) @@ -112,23 +112,23 @@ func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.Handl log.Panicln(err) } - p.NoIncorrect = 0 - for i, q := range b.Questions { - if p.GivenAnswers[i] != q.Correct { - p.NoIncorrect++ + p.numberIncorrect = 0 + for i, q := range b.questions { + if p.givenAnswers[i] != q.Correct { + p.numberIncorrect++ } } data := resultHTMLData{ participantHTMLData: participantHTMLData{ - BriefingID: b.UUID, - Participant: *p, + briefingID: b.uuid, + participant: *p, }, - Questions: makeResultQuestions(b.Questions, p.GivenAnswers), + questions: makeResultQuestions(b.questions, p.givenAnswers), } - if data.NoIncorrect == 0 { - if err := db.WriteGivenAnswers(*b.Briefing, *p.Participant, b.Questions, p.GivenAnswers); err != nil { + if data.numberIncorrect == 0 { + if err := db.WriteGivenAnswers(*b.Briefing, *p.Participant, b.questions, p.givenAnswers); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err) } @@ -139,36 +139,36 @@ func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.Handl } } -func (p *Participant) HandleAllowRetry() http.HandlerFunc { +func (p *participant) handleAllowRetry() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - p.NoIncorrect = -1 - p.AllowRetry = true + p.numberIncorrect = -1 + p.allowRetry = true } } -func (p *Participant) HandleRetry(b *Briefing, i *int) http.HandlerFunc { +func (p *participant) handleRetry(b *briefing, i *int) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if p.AllowRetry { - p.AllowRetry = false + if p.allowRetry { + p.allowRetry = false (*i) = 0 data := questionHTMLData{ participantHTMLData: participantHTMLData{ - BriefingID: b.UUID, - Participant: Participant{Login: p.Login}, + briefingID: b.uuid, + participant: participant{login: p.login}, }, - QuestionID: int64(*i + 1), - Question: b.Questions[*i], + questionID: int64(*i + 1), + question: b.questions[*i], } template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) } else { data := resultHTMLData{ participantHTMLData: participantHTMLData{ - BriefingID: b.UUID, - Participant: *p, + briefingID: b.uuid, + participant: *p, }, - Questions: makeResultQuestions(b.Questions, p.GivenAnswers), + questions: makeResultQuestions(b.questions, p.givenAnswers), } template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data) @@ -176,26 +176,26 @@ func (p *Participant) HandleRetry(b *Briefing, i *int) http.HandlerFunc { } } -func (p *Participant) HandleRefresh(b *Briefing) http.HandlerFunc { +func (p *participant) handleRefresh(b *briefing) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { data := participantHTMLData{ - BriefingID: b.UUID, - Participant: *p, + briefingID: b.uuid, + participant: *p, } template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data) } } -func (p *Participant) HandleEndOfVideo(b *Briefing) http.HandlerFunc { +func (p *participant) handleEndOfVideo(b *briefing) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { data := questionHTMLData{ participantHTMLData: participantHTMLData{ - BriefingID: b.UUID, - Participant: Participant{Login: p.Login}, + briefingID: b.uuid, + participant: participant{login: p.login}, }, - QuestionID: 1, - Question: b.Questions[0], + questionID: 1, + question: b.questions[0], } template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) diff --git a/packages/server/session.go b/packages/server/session.go index 21691de..46e79e6 100644 --- a/packages/server/session.go +++ b/packages/server/session.go @@ -21,18 +21,18 @@ import ( ) type Session struct { - UUID uuid.UUID - Instructor data.Instructor - Briefings []*Briefing + uuid uuid.UUID + instructor data.Instructor + briefings []*briefing } -func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc { +func (s *Session) handleSearch(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var err error data := tableHTMLData{} - data.SessionID = s.UUID - data.OTD, err = db.GetOverviewTableDataByName(r.PostFormValue("search")) + data.sessionID = s.uuid + data.otd, err = db.GetOverviewTableDataByName(r.PostFormValue("search")) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err) @@ -41,22 +41,22 @@ func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc { } } -func (s *Session) HandleNewBriefing(cb chan<- *Briefing) http.HandlerFunc { +func (s *Session) handleNewBriefing(cb chan<- *briefing) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - briefing := Briefing{Briefing: &data.Briefing{InstructorID: s.Instructor.ID}, UUID: uuid.New()} - s.Briefings = append(s.Briefings, &briefing) + briefing := briefing{Briefing: &data.Briefing{InstructorID: s.instructor.ID}, uuid: uuid.New()} + s.briefings = append(s.briefings, &briefing) cb <- &briefing - template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", participantHTMLData{BriefingID: briefing.UUID}) + template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", participantHTMLData{briefingID: briefing.uuid}) } } -func (s *Session) HandleBriefingDone(db *data.DB) http.HandlerFunc { +func (s *Session) handleBriefingDone(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - data := tableHTMLData{SessionID: s.UUID} + data := tableHTMLData{sessionID: s.uuid} var err error - data.OTD, err = db.GetAllOverviewTableData() + data.otd, err = db.GetAllOverviewTableData() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err)