15 Commits

Author SHA1 Message Date
726c8b6dcb Login für eingeladene Prüflinge implementiert 2023-10-10 20:57:53 +02:00
608879d008 Generierte UUID in einen asynchronen Speicher schreiben 2023-10-10 19:50:19 +02:00
616df72041 package db zu data umbenannt, außerdem db.Open() zu data.OpenDB() 2023-10-10 19:19:17 +02:00
1597d38d34 UUID-Button repariert 2023-10-10 19:18:34 +02:00
8dbb5f946d UUID-Element zum Ersetzen des "Einladen"-Buttons erstellt 2023-10-10 19:08:59 +02:00
7c7cb5959d GenerateUUID zum erstellen einer pseudozufälligen, 8 Stellen langen Hexadezimalzahl 2023-10-10 19:06:02 +02:00
2beb90a345 Button zum Generieren einer UUID hinzugefügt 2023-10-10 18:55:54 +02:00
f80dca4b10 Bei 82870e10 war question.html nicht gespeichert 2023-10-10 18:55:34 +02:00
bf05bc0be7 Der Participant muss nicht das ganze Questionaire kennen, nur die UUID 2023-10-10 18:52:29 +02:00
f6a073fc39 Alle nötigen types für Questionaire eingefügt 2023-10-10 18:50:54 +02:00
82870e100f question.html erstellt 2023-10-10 18:43:48 +02:00
2b119f6752 Leerzeichen in SQL-Abfragen ergänzt 2023-10-08 15:16:47 +02:00
c04932383e Reihenfolge der ausgegebenen Ergebnisse umgedreht 2023-10-08 15:13:22 +02:00
8ea0c2964a Spalten in Ausgabetabelle umbenannt 2023-10-08 15:12:52 +02:00
519dc82023 Automatisches Suchen ohne Submit-Button 2023-10-08 11:01:57 +02:00
9 changed files with 147 additions and 29 deletions

26
main.go
View File

@ -1,29 +1,45 @@
package main
import (
"html/template"
"log"
"net/http"
"streifling.com/jason/sicherheitsunterweisung/packages/db"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
"streifling.com/jason/sicherheitsunterweisung/packages/server"
)
func addUUIDs(uc *chan string, uuids *[]string) {
for uuid := range *uc {
(*uuids) = append(*uuids, uuid)
}
}
func main() {
var i, j int64
uuids := make([]string, 0)
i, j = 1, 1
mux := http.NewServeMux()
db, err := db.Open("sicherheitsunterweisung")
db, err := data.OpenDB("sicherheitsunterweisung")
if err != nil {
log.Fatalln(err)
}
uuidChan := make(chan string)
defer close(uuidChan)
go addUUIDs(&uuidChan, &uuids)
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
mux.HandleFunc("/", server.DisplayTable(db))
mux.HandleFunc("/submit/", server.SubmitForm(db, &i, &j))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil)
})
mux.HandleFunc("/search/", server.DisplayResults(db))
mux.HandleFunc("/new-briefing/", server.DisplayForm(&i))
mux.HandleFunc("/add-participant/", server.AddParticipant(&i))
mux.HandleFunc("/submit-form/", server.SubmitForm(db, &i, &j))
mux.HandleFunc("/generate-uuid/", server.GenerateUUID(uuidChan))
mux.HandleFunc("/internal-login/", server.DisplayTable(db))
mux.HandleFunc("/external-login/", server.DisplayQuestionsIfOK(&uuids))
log.Fatalln(http.ListenAndServe(":8080", mux))
}

View File

@ -1,4 +1,4 @@
package db
package data
import (
"bufio"
@ -37,7 +37,13 @@ func getCredentials() (string, string, error) {
return strings.TrimSpace(user), strings.TrimSpace(pass), nil
}
func Open(dbName string) (*DB, error) {
func reverseOrder(bs *[]types.Briefing) {
for i, j := 0, len(*bs)-1; i < j; i, j = i+1, j-1 {
(*bs)[i], (*bs)[j] = (*bs)[j], (*bs)[i]
}
}
func OpenDB(dbName string) (*DB, error) {
var err error
db := new(DB)
@ -108,6 +114,7 @@ func (db *DB) ReadAll() (*[]types.Briefing, error) {
bs = append(bs, *b)
}
reverseOrder(&bs)
return &bs, nil
}
@ -145,5 +152,6 @@ func (db *DB) ReadByName(name string) (*[]types.Briefing, error) {
bs = append(bs, *b)
}
reverseOrder(&bs)
return &bs, nil
}

View File

@ -0,0 +1 @@
package data

View File

@ -1,16 +1,17 @@
package server
import (
"crypto/rand"
"encoding/hex"
"fmt"
"html/template"
"log"
"net/http"
"streifling.com/jason/sicherheitsunterweisung/packages/db"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
"streifling.com/jason/sicherheitsunterweisung/packages/types"
)
func DisplayTable(db *db.DB) http.HandlerFunc {
func DisplayTable(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.ReadAll()
if err != nil {
@ -20,7 +21,7 @@ func DisplayTable(db *db.DB) http.HandlerFunc {
}
}
func DisplayResults(db *db.DB) http.HandlerFunc {
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 {
@ -43,7 +44,7 @@ func AddParticipant(i *int64) http.HandlerFunc {
}
}
func SubmitForm(db *db.DB, i, j *int64) http.HandlerFunc {
func SubmitForm(db *data.DB, i, j *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
b := new(types.Briefing)
@ -71,6 +72,43 @@ func SubmitForm(db *db.DB, i, j *int64) http.HandlerFunc {
if err != nil {
_ = fmt.Errorf("SubmitForm: db.ReadAll(): %v\n", err)
}
template.Must(template.ParseFiles("templates/index.html", "templates/table.html")).Execute(w, bs)
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
}

View File

@ -7,10 +7,28 @@ type Person struct {
type Instructor Person
type QAElement struct {
ID int
Text string
}
type Answer QAElement
type Question struct {
QAElement
Answers []Answer
}
type Questionaire struct {
UUID string
Questions []Question
}
type Participant struct {
ID int64
Person
Company string
QuestionaireUUID string
}
type Briefing struct {

View File

@ -1,3 +1,7 @@
{{ define "uuid" }}
<span>{{ . }}</span>
{{ end }}
{{ define "participant" }}
<div id="participant-{{ . }}">
<label for="participant-first-input-{{ . }}">Vorname</label>
@ -8,6 +12,10 @@
<label for="participant-company-input-{{ . }}">Firma</label>
<input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
<button type="button" hx-post="/generate-uuid/" hx-target="this" hx-swap="outerHTML">
Einladen
</button>
</div>
{{ end }}
@ -43,13 +51,13 @@
<div id="participants">
<button type="button" hx-post="/add-participant/" hx-target="#participants" hx-swap="beforeend" hx-trigger="click">
+
Neuer Teilnehmer
</button>
{{ template "participant" . }}
</div>
<button type="submit" hx-post="/submit/" hx-target="#content" hx-swap="innerHTML">
<button type="submit" hx-post="/submit-form/" hx-target="#content" hx-swap="innerHTML">
Senden
</button>
</form>

16
templates/login.html Normal file
View File

@ -0,0 +1,16 @@
{{ define "content" }}
<form>
<h2>Login</h2>
<label for="login-input">Code</label>
<input type="text" name="login" id="login-input" />
<button type="submit" hx-post="/external-login/" hx-target="#content">
Anmelden
</button>
<button type="submit" hx-post="/internal-login/" hx-target="#content">
Intern
</button>
</form>
{{ end }}

15
templates/question.html Normal file
View File

@ -0,0 +1,15 @@
{{ define "answers" }}
{{ range .Answers }}
<label for="answer-{{ .ID }}">{{ .Text }}</label>
<input type="radio" name="answer-{{ .ID }}" id="answer-{{ .ID }}" />
{{ end }}
{{ end }}
{{ define "content" }}
<h2>Frage {{ .Question.ID }}</h2>
<p>{{ .Question.Text }}</p>
{{ template "answers" . }}
<button type="submit" hx-post="/submit-{{ .UUID }}-{{ .Question.ID }}/" hx-target="#content" hx-swap="innerHTML">
{{ end }}

View File

@ -19,10 +19,8 @@
{{ define "content" }}
<form>
<label for="search-input">Suche</label>
<input type="text" name="search" id="search-input" />
<button type="submit" hx-post="/search/" hx-target="#results" hx-swap="innerHTML">
Suchen
</button>
<input type="text" name="search" id="search-input" hx-post="/search/" hx-target="#results" hx-swap="innerHTML"
hx-trigger="keyup changed delay:200ms" />
</form>
<form>
@ -34,12 +32,12 @@
<table>
<thead>
<tr>
<th colspan="2">Name Unterweiser</th>
<th colspan="2">Unterweiser</th>
<th>Datum</th>
<th>Uhrzeit</th>
<th>Stand</th>
<th>Ort</th>
<th colspan="2">Name Teilnehmer</th>
<th colspan="2">Teilnehmer</th>
<th>Firma</th>
</tr>
</thead>