Viel Code erstmal auskommentiert, um der Logik besser folgen zu können + ein paar Änderungen

This commit is contained in:
Jason Streifling 2023-10-18 17:08:27 +02:00
parent 82ced65513
commit 39d8108521
2 changed files with 91 additions and 98 deletions

49
main.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
@ -11,22 +10,24 @@ import (
"streifling.com/jason/sicherheitsunterweisung/packages/types" "streifling.com/jason/sicherheitsunterweisung/packages/types"
) )
func waitForParticipants(sb []*types.Briefing, sp []*types.Participant, cp <-chan *types.Participant, m *http.ServeMux) { // func waitForParticipants(sb []*types.Briefing, sp []*types.Participant, cp <-chan *types.Participant, m *http.ServeMux) {
for p := range cp { // for p := range cp {
p.Questions = data.InitQuestions() // sg := make([]*types.GivenAnswer, 4)
sp = append(sp, p) //
// p.Questions = data.InitQuestions()
var i int // sp = append(sp, p)
for i = range p.Questions { //
m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i)+"/", server.DisplayQuestion(i, p)) // var i int
} // for i = range p.Questions {
m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i+1)+"/", server.DisplayTestResults(sb, p)) // m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i)+"/", server.DisplayQuestion(i, p))
} // }
} // m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i+1)+"/", server.DisplayTestResults(sb, p))
// }
// }
func main() { func main() {
logins := make([]string, 0) logins := make([]string, 0)
participants := make([]*types.Participant, 0) // participants := make([]*types.Participant, 0)
briefings := make([]*types.Briefing, 0) briefings := make([]*types.Briefing, 0)
mux := http.NewServeMux() mux := http.NewServeMux()
@ -35,26 +36,20 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
var i, j int64 // participantChan := make(chan *types.Participant)
if err := db.GetLastID(&i); err != nil { // defer close(participantChan)
log.Fatalln(err) // go waitForParticipants(briefings, participants, participantChan, mux)
}
j = i
participantChan := make(chan *types.Participant)
defer close(participantChan)
go waitForParticipants(briefings, participants, participantChan, mux)
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil) template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil)
}) })
mux.HandleFunc("/search/", server.DisplaySearchResults(db)) mux.HandleFunc("/search/", server.DisplaySearchResults(db))
mux.HandleFunc("/new-briefing/", server.DisplayForm(&i)) mux.HandleFunc("/new-briefing/", server.DisplayInstructorForm())
mux.HandleFunc("/add-participant/", server.AddParticipant(&i, &logins)) mux.HandleFunc("/add-participant/", server.AddParticipant(&logins))
mux.HandleFunc("/submit-form/", server.SubmitForm(db, &i, &j)) mux.HandleFunc("/submit-form/", server.SubmitBriefingForm(&briefings))
mux.HandleFunc("/internal-login/", server.DisplayTable(db)) mux.HandleFunc("/internal-login/", server.DisplayTable(db))
mux.HandleFunc("/external-login/", server.DisplayParticipantForm(&logins, participantChan)) mux.HandleFunc("/external-login/", server.DisplayParticipantForm(&logins))
log.Fatalln(http.ListenAndServe(":8080", mux)) log.Fatalln(http.ListenAndServe(":8080", mux))
} }

View File

@ -41,9 +41,9 @@ func DisplaySearchResults(db *data.DB) http.HandlerFunc {
} }
} }
func DisplayForm(i *int64) http.HandlerFunc { func DisplayInstructorForm() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", i) template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", nil)
} }
} }
@ -57,15 +57,13 @@ func generateUUID() (string, error) {
return hex.EncodeToString(bs), nil return hex.EncodeToString(bs), nil
} }
func AddParticipant(i *int64, sl *[]string) http.HandlerFunc { func AddParticipant(sl *[]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
uuid, err := generateUUID() login, err := generateUUID()
if err != nil { if err != nil {
http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
} }
*i++
login := fmt.Sprintf("%d", *i) + "-" + uuid
(*sl) = append(*sl, login) (*sl) = append(*sl, login)
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login) template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login)
} }
@ -73,7 +71,7 @@ func AddParticipant(i *int64, sl *[]string) http.HandlerFunc {
// TODO: Hier weiter machen, irgendwie die b.ID herausgeben, // TODO: Hier weiter machen, irgendwie die b.ID herausgeben,
// am besten hier auch die p.IDs rausgeben, damit diese später verknüpft werden können // am besten hier auch die p.IDs rausgeben, damit diese später verknüpft werden können
func SubmitBriefingForm(sb *[]types.Briefing) http.HandlerFunc { func SubmitBriefingForm(sb *[]*types.Briefing) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
now := time.Now() now := time.Now()
briefing := new(types.Briefing) briefing := new(types.Briefing)
@ -88,7 +86,7 @@ func SubmitBriefingForm(sb *[]types.Briefing) http.HandlerFunc {
briefing.DocumentName = r.PostFormValue("document") // TODO: in HTML einfügen briefing.DocumentName = r.PostFormValue("document") // TODO: in HTML einfügen
briefing.AsOf = r.PostFormValue("state") // TODO: Umbenennen briefing.AsOf = r.PostFormValue("state") // TODO: Umbenennen
// briefing.InstructorID = r.PostFormValue("instructor-id") // TODO: aus Dropdown holen // briefing.InstructorID = r.PostFormValue("instructor-id") // TODO: aus Dropdown holen
(*sb) = append(*sb, *briefing) (*sb) = append(*sb, briefing)
} }
} }
@ -130,66 +128,66 @@ func DisplayParticipantForm(sl *[]string) http.HandlerFunc {
} }
} }
func readAnswer(r *http.Request, p *types.Participant, i int) error { // func readAnswer(r *http.Request, p *types.Participant, i int) error {
v, err := strconv.Atoi(r.PostFormValue("answer")) // v, err := strconv.Atoi(r.PostFormValue("answer"))
if err != nil { // if err != nil {
return fmt.Errorf("readAnswer: strconv.Atoi(): %v\n", err) // return fmt.Errorf("readAnswer: strconv.Atoi(): %v\n", err)
} // }
//
p.Questions[i].Chosen = v // p.Questions[i].Chosen = v
//
return nil // return nil
} // }
//
func DisplayQuestion(i int, p *types.Participant) http.HandlerFunc { // func DisplayQuestion(i int, p *types.Participant) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { // return func(w http.ResponseWriter, r *http.Request) {
if i == 0 { // if i == 0 {
p.FirstName = r.PostFormValue("participant-first-" + fmt.Sprintf("%d", p.ID)) // p.FirstName = r.PostFormValue("participant-first-" + fmt.Sprintf("%d", p.ID))
p.LastName = r.PostFormValue("participant-last-" + fmt.Sprintf("%d", p.ID)) // p.LastName = r.PostFormValue("participant-last-" + fmt.Sprintf("%d", p.ID))
p.Company = r.PostFormValue("participant-company-" + fmt.Sprintf("%d", p.ID)) // p.Company = r.PostFormValue("participant-company-" + fmt.Sprintf("%d", p.ID))
} else { // } else {
if err := readAnswer(r, p, i-1); err != nil { // if err := readAnswer(r, p, i-1); err != nil {
http.Error(w, "DisplayQuestion: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError) // http.Error(w, "DisplayQuestion: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
} // }
} // }
//
data := new(questionData) // data := new(questionData)
data.ID = p.ID // data.ID = p.ID
data.Q = p.Questions[i] // data.Q = p.Questions[i]
data.I = i // data.I = i
data.J = i + 1 // data.J = i + 1
//
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) // template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
} // }
} // }
//
func DisplayTestResults(b *types.Briefing, p *types.Participant) http.HandlerFunc { // func DisplayTestResults(b *types.Briefing, p *types.Participant) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { // return func(w http.ResponseWriter, r *http.Request) {
numQuestions := len(p.Questions) // numQuestions := len(p.Questions)
wrongAnswers := make([]int, 0) // wrongAnswers := make([]int, 0)
fmt.Println(wrongAnswers) // fmt.Println(wrongAnswers)
//
if err := readAnswer(r, p, numQuestions-1); err != nil { // if err := readAnswer(r, p, numQuestions-1); err != nil {
http.Error(w, "DisplayTestResults: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError) // http.Error(w, "DisplayTestResults: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
} // }
//
for i, q := range p.Questions { // for i, q := range p.Questions {
if q.Chosen != q.Correct { // if q.Chosen != q.Correct {
wrongAnswers = append(wrongAnswers, i) // wrongAnswers = append(wrongAnswers, i)
} // }
} // }
//
if wrongAnswers == nil { // if wrongAnswers == nil {
b.Participants = append(b.Participants, p) // b.Participants = append(b.Participants, p)
} else { // } else {
data := new(questionData) // data := new(questionData)
data.ID = p.ID // data.ID = p.ID
data.Q = p.Questions[0] // data.Q = p.Questions[0]
data.I = 0 // data.I = 0
data.J = data.I + 1 // data.J = data.I + 1
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) // template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
} // }
//
template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", nil) // template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", nil)
} // }
} // }