/* * 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 server import ( "crypto/rand" "encoding/hex" "fmt" "html/template" "log" "net/http" "time" "github.com/google/uuid" "streifling.com/jason/sicherheitsunterweisung/packages/data" ) type briefing struct { *data.Briefing participants []*participant questions []data.Question uuid uuid.UUID } func generateLogin() (string, error) { bs := make([]byte, 4) if _, err := rand.Read(bs); err != nil { return "", fmt.Errorf("error: generateLogin: rand.Read(bs): %v", err) } return hex.EncodeToString(bs), nil } 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), numberIncorrect: -1, allowRetry: false, } participant.login, err = generateLogin() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err) } b.participants = append(b.participants, &participant) cp <- &participant data := participantHTMLData{ 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 { return func(w http.ResponseWriter, r *http.Request) { b.DateTime = time.Now().Format("2006-01-02 15:04:05") b.Location = r.PostFormValue("location") b.Document = r.PostFormValue("document") b.AsOf = r.PostFormValue("as-of") err := db.WriteBriefing(b.Briefing) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err) } data := summaryHTMLData{ 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 } template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "content", data) } }