115 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			115 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package server
 | |
| 
 | |
| import (
 | |
| 	"crypto/rand"
 | |
| 	"encoding/hex"
 | |
| 	"fmt"
 | |
| 	"html/template"
 | |
| 	"log"
 | |
| 	"net/http"
 | |
| 	"streifling.com/jason/sicherheitsunterweisung/packages/data"
 | |
| 	"streifling.com/jason/sicherheitsunterweisung/packages/types"
 | |
| )
 | |
| 
 | |
| func DisplayTable(db *data.DB) http.HandlerFunc {
 | |
| 	return func(w http.ResponseWriter, r *http.Request) {
 | |
| 		bs, err := db.ReadAll()
 | |
| 		if err != nil {
 | |
| 			_ = fmt.Errorf("DisplayTable: %v\n", err)
 | |
| 		}
 | |
| 		template.Must(template.ParseFiles("templates/index.html", "templates/table.html")).Execute(w, bs)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func DisplayResults(db *data.DB) http.HandlerFunc {
 | |
| 	return func(w http.ResponseWriter, r *http.Request) {
 | |
| 		bs, err := db.ReadByName(r.PostFormValue("search"))
 | |
| 		if err != nil {
 | |
| 			_ = fmt.Errorf("DisplayResults: db.ReadByName(r.PostFormValue()): %v\n", err)
 | |
| 		}
 | |
| 		template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func DisplayForm(i *int64) http.HandlerFunc {
 | |
| 	return func(w http.ResponseWriter, r *http.Request) {
 | |
| 		template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "content", i)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func AddParticipant(i *int64) http.HandlerFunc {
 | |
| 	return func(w http.ResponseWriter, r *http.Request) {
 | |
| 		*i++
 | |
| 		template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "participant", i)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func SubmitForm(db *data.DB, i, j *int64) http.HandlerFunc {
 | |
| 	return func(w http.ResponseWriter, r *http.Request) {
 | |
| 		b := new(types.Briefing)
 | |
| 
 | |
| 		b.FirstName = r.PostFormValue("instructor-first")
 | |
| 		b.LastName = r.PostFormValue("instructor-last")
 | |
| 		b.Date = r.PostFormValue("date")
 | |
| 		b.Time = r.PostFormValue("time")
 | |
| 		b.State = r.PostFormValue("state")
 | |
| 		b.Location = r.PostFormValue("location")
 | |
| 
 | |
| 		for ; *j <= *i; *j++ {
 | |
| 			b.Participants = append(b.Participants, types.Participant{
 | |
| 				ID: *j,
 | |
| 				Person: types.Person{
 | |
| 					FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(*j)),
 | |
| 					LastName:  r.PostFormValue(("participant-last-" + fmt.Sprint(*j))),
 | |
| 				},
 | |
| 				Company: r.PostFormValue(("participant-company-" + fmt.Sprint(*j))),
 | |
| 			})
 | |
| 		}
 | |
| 
 | |
| 		log.Println(b)
 | |
| 		db.WriteBriefing(b)
 | |
| 		bs, err := db.ReadAll()
 | |
| 		if err != nil {
 | |
| 			_ = fmt.Errorf("SubmitForm: db.ReadAll(): %v\n", err)
 | |
| 		}
 | |
| 
 | |
| 		template.Must(template.ParseFiles("templates/table.html")).Execute(w, bs)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func GenerateUUID(ch chan<- string) http.HandlerFunc {
 | |
| 	return func(w http.ResponseWriter, r *http.Request) {
 | |
| 		bs := make([]byte, 4)
 | |
| 
 | |
| 		_, err := rand.Read(bs)
 | |
| 		if err != nil {
 | |
| 			_ = fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
 | |
| 		}
 | |
| 
 | |
| 		uuid := hex.EncodeToString(bs)
 | |
| 		ch <- uuid
 | |
| 
 | |
| 		template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "uuid", uuid)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func DisplayQuestionsIfOK(uuids *[]string) http.HandlerFunc {
 | |
| 	return func(w http.ResponseWriter, r *http.Request) {
 | |
| 		if uuidIsOK(r.PostFormValue("login"), uuids) {
 | |
| 			template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", nil)
 | |
| 		} else {
 | |
| 			template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
 | |
| 		}
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // TODO: Delete uuid from uuids
 | |
| func uuidIsOK(uuid string, uuids *[]string) bool {
 | |
| 	for _, u := range *uuids {
 | |
| 		if uuid == u {
 | |
| 			return true
 | |
| 		}
 | |
| 	}
 | |
| 	return false
 | |
| }
 |