/* * 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 UUID uuid.UUID Participants []*Participant Questions []data.Question } 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), NoIncorrect: -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{Login: participant.Login}, } 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) } }