Compare commits
21 Commits
datenbank_
...
726c8b6dcb
Author | SHA1 | Date | |
---|---|---|---|
726c8b6dcb | |||
608879d008 | |||
616df72041 | |||
1597d38d34 | |||
8dbb5f946d | |||
7c7cb5959d | |||
2beb90a345 | |||
f80dca4b10 | |||
bf05bc0be7 | |||
f6a073fc39 | |||
82870e100f | |||
2b119f6752 | |||
c04932383e | |||
8ea0c2964a | |||
519dc82023 | |||
324a1c54d6 | |||
15675d5e6c | |||
8ae3019b9c | |||
b13eba8008 | |||
da77201a93 | |||
71bf8978ac |
35
main.go
35
main.go
@ -1,38 +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"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/types"
|
||||
)
|
||||
|
||||
func writeBriefing(ch chan *types.Briefing, db *db.DB) {
|
||||
for b := range ch {
|
||||
db.WriteBriefing(b)
|
||||
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()
|
||||
cb := make(chan *types.Briefing)
|
||||
|
||||
db, err := db.Open("sicherheitsunterweisung")
|
||||
db, err := data.OpenDB("sicherheitsunterweisung")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
||||
mux.HandleFunc("/", server.DisplayForm(&i))
|
||||
mux.HandleFunc("/add-participant/", server.AddParticipant(&i))
|
||||
mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j))
|
||||
uuidChan := make(chan string)
|
||||
defer close(uuidChan)
|
||||
go addUUIDs(&uuidChan, &uuids)
|
||||
|
||||
go writeBriefing(cb, db)
|
||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
||||
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))
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
@ -63,18 +69,18 @@ func Open(dbName string) (*DB, error) {
|
||||
func (db *DB) WriteBriefing(b *types.Briefing) error {
|
||||
for i := 0; i < len(b.Participants); i++ {
|
||||
result, err := db.Exec("INSERT INTO "+db.Name+" (instructor_first,"+
|
||||
"instructor_last, date, time, state, location, participant_first,"+
|
||||
"participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
" instructor_last, date, time, state, location, participant_first,"+
|
||||
" participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location,
|
||||
b.Participants[i].FirstName, b.Participants[i].LastName,
|
||||
b.Participants[i].Company)
|
||||
if err != nil {
|
||||
return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO"+
|
||||
"\"+db.Name+\" (instructor_first, instructor_last, date, time, state,"+
|
||||
"location, participant_first, participant_last, company) VALUES (?, ?,"+
|
||||
"?, ?, ?, ?, ?, ?, ?)\", b.FirstName, b.LastName, b.Date, b.Time,"+
|
||||
"b.State, b.Location, b.Participants[i].FirstName,"+
|
||||
"b.Participants[i].LastName, b.Participants[i].Company): %v\n", err)
|
||||
" \"+db.Name+\" (instructor_first, instructor_last, date, time, state,"+
|
||||
" location, participant_first, participant_last, company) VALUES (?, ?,"+
|
||||
" ?, ?, ?, ?, ?, ?, ?)\", b.FirstName, b.LastName, b.Date, b.Time,"+
|
||||
" b.State, b.Location, b.Participants[i].FirstName,"+
|
||||
" b.Participants[i].LastName, b.Participants[i].Company): %v\n", err)
|
||||
}
|
||||
|
||||
_, err = result.LastInsertId()
|
||||
@ -86,6 +92,32 @@ func (db *DB) WriteBriefing(b *types.Briefing) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) ReadAll() (*[]types.Briefing, error) {
|
||||
bs := make([]types.Briefing, 0)
|
||||
|
||||
rows, err := db.Query("SELECT *" + " FROM " + db.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("*DB.ReadAll: db.Query(\"SELECT * FROM \"+db.Name): %v\n", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
b := new(types.Briefing)
|
||||
p := new(types.Participant)
|
||||
|
||||
if err := rows.Scan(&p.ID, &b.FirstName, &b.LastName, &b.Date, &b.Time,
|
||||
&b.State, &b.Location, &p.FirstName, &p.LastName, &p.Company); err != nil {
|
||||
return nil, fmt.Errorf("*DB.ReadAll: db.Query(): %v\n", err)
|
||||
}
|
||||
|
||||
b.Participants = append(b.Participants, *p)
|
||||
bs = append(bs, *b)
|
||||
}
|
||||
|
||||
reverseOrder(&bs)
|
||||
return &bs, nil
|
||||
}
|
||||
|
||||
func (db *DB) ReadByName(name string) (*[]types.Briefing, error) {
|
||||
bs := make([]types.Briefing, 0)
|
||||
|
||||
@ -120,5 +152,6 @@ func (db *DB) ReadByName(name string) (*[]types.Briefing, error) {
|
||||
bs = append(bs, *b)
|
||||
}
|
||||
|
||||
reverseOrder(&bs)
|
||||
return &bs, nil
|
||||
}
|
1
packages/data/questionaires.go
Normal file
1
packages/data/questionaires.go
Normal file
@ -0,0 +1 @@
|
||||
package data
|
@ -1,27 +1,50 @@
|
||||
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/index.html")).Execute(w, i)
|
||||
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/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i)
|
||||
template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "participant", i)
|
||||
}
|
||||
}
|
||||
|
||||
func SubmitForm(ch chan<- *types.Briefing, 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)
|
||||
|
||||
@ -44,6 +67,48 @@ func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc {
|
||||
}
|
||||
|
||||
log.Println(b)
|
||||
ch <- 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
|
||||
}
|
||||
|
@ -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
|
||||
Company string
|
||||
QuestionaireUUID string
|
||||
}
|
||||
|
||||
type Briefing struct {
|
||||
|
64
templates/form.html
Normal file
64
templates/form.html
Normal file
@ -0,0 +1,64 @@
|
||||
{{ define "uuid" }}
|
||||
<span>{{ . }}</span>
|
||||
{{ end }}
|
||||
|
||||
{{ define "participant" }}
|
||||
<div id="participant-{{ . }}">
|
||||
<label for="participant-first-input-{{ . }}">Vorname</label>
|
||||
<input type="text" name="participant-first-{{ . }}" id="participant-first-input-{{ . }}" />
|
||||
|
||||
<label for="participant-last-input-{{ . }}">Nachname</label>
|
||||
<input type="text" name="participant-last-{{ . }}" id="participant-last-input-{{ . }}" />
|
||||
|
||||
<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 }}
|
||||
|
||||
{{ define "content" }}
|
||||
<form>
|
||||
<div id="instructor">
|
||||
<label for="instructor-first-input">Unterweiser Vorname</label>
|
||||
<input type="text" name="instructor-first" id="instructor-first-input" />
|
||||
|
||||
<label for="instructor-last-input">Unterweiser Nachname</label>
|
||||
<input type="text" name="instructor-last" id="instructor-last-input" />
|
||||
</div>
|
||||
|
||||
<div id="date">
|
||||
<label for="date-input">Datum</label>
|
||||
<input type="date" name="date" id="date-input" />
|
||||
</div>
|
||||
|
||||
<div id="time">
|
||||
<label for="time-input">Uhrzeit</label>
|
||||
<input type="time" name="time" id="time-input" />
|
||||
</div>
|
||||
|
||||
<div id="state">
|
||||
<label for="state-input">Stand vom</label>
|
||||
<input type="date" name="state" id="state-input" />
|
||||
</div>
|
||||
|
||||
<div id="location">
|
||||
<label for="location-input">Ort</label>
|
||||
<input type="text" name="location" id="location-input" />
|
||||
</div>
|
||||
|
||||
<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-form/" hx-target="#content" hx-swap="innerHTML">
|
||||
Senden
|
||||
</button>
|
||||
</form>
|
||||
{{ end }}
|
@ -11,57 +11,9 @@
|
||||
<body>
|
||||
<h1>Sicherheitsunterweisung</h1>
|
||||
|
||||
<form>
|
||||
<div id="instructor">
|
||||
<label for="instructor-first-input">Unterweiser Vorname</label>
|
||||
<input type="text" name="instructor-first" id="instructor-first-input" />
|
||||
|
||||
<label for="instructor-last-input">Unterweiser Nachname</label>
|
||||
<input type="text" name="instructor-last" id="instructor-last-input" />
|
||||
</div>
|
||||
|
||||
<div id="date">
|
||||
<label for="date-input">Datum</label>
|
||||
<input type="date" name="date" id="date-input" />
|
||||
</div>
|
||||
|
||||
<div id="time">
|
||||
<label for="time-input">Uhrzeit</label>
|
||||
<input type="time" name="time" id="time-input" />
|
||||
</div>
|
||||
|
||||
<div id="state">
|
||||
<label for="state-input">Stand vom</label>
|
||||
<input type="date" name="state" id="state-input" />
|
||||
</div>
|
||||
|
||||
<div id="location">
|
||||
<label for="location-input">Ort</label>
|
||||
<input type="text" name="location" id="location-input" />
|
||||
</div>
|
||||
|
||||
<div id="participants">
|
||||
<button type="button" hx-post="/add-participant/" hx-target="#participants" hx-swap="beforeend"
|
||||
hx-trigger="click">
|
||||
+
|
||||
</button>
|
||||
|
||||
<div id="participant-{{ . }}">
|
||||
<label for="participant-first-input-{{ . }}">Vorname</label>
|
||||
<input type="text" name="participant-first-{{ . }}" id="participant-first-input-{{ . }}" />
|
||||
|
||||
<label for="participant-last-input-{{ . }}">Nachname</label>
|
||||
<input type="text" name="participant-last-{{ . }}" id="participant-last-input-{{ . }}" />
|
||||
|
||||
<label for="participant-company-input-{{ . }}">Firma</label>
|
||||
<input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button type="submit" hx-post="/submit/">
|
||||
Senden
|
||||
</button>
|
||||
</form>
|
||||
<div id="content">
|
||||
{{ template "content" . }}
|
||||
</div>
|
||||
|
||||
<script src="/static/js/htmx.min.js" type="text/javascript"></script>
|
||||
</body>
|
||||
|
16
templates/login.html
Normal file
16
templates/login.html
Normal 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 }}
|
@ -1,12 +0,0 @@
|
||||
{{ define "participant" }}
|
||||
<div id="participant-{{ . }}">
|
||||
<label for="participant-first-input-{{ . }}">Vorname</label>
|
||||
<input type="text" name="participant-first-{{ . }}" id="participant-first-input-{{ . }}" />
|
||||
|
||||
<label for="participant-last-input-{{ . }}">Nachname</label>
|
||||
<input type="text" name="participant-last-{{ . }}" id="participant-last-input-{{ . }}" />
|
||||
|
||||
<label for="participant-company-input-{{ . }}">Firma</label>
|
||||
<input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
|
||||
</div>
|
||||
{{ end }}
|
15
templates/question.html
Normal file
15
templates/question.html
Normal 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 }}
|
49
templates/table.html
Normal file
49
templates/table.html
Normal file
@ -0,0 +1,49 @@
|
||||
{{ define "rows" }}
|
||||
{{ range . }}
|
||||
<tr>
|
||||
<td>{{ .FirstName }}</td>
|
||||
<td>{{ .LastName }}</td>
|
||||
<td>{{ .Date }}</td>
|
||||
<td>{{ .Time }}</td>
|
||||
<td>{{ .State }}</td>
|
||||
<td>{{ .Location }}</td>
|
||||
{{ range .Participants }}
|
||||
<td>{{ .FirstName }}</td>
|
||||
<td>{{ .LastName }}</td>
|
||||
<td>{{ .Company }}</td>
|
||||
{{ end }}
|
||||
</tr>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ define "content" }}
|
||||
<form>
|
||||
<label for="search-input">Suche</label>
|
||||
<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>
|
||||
<button type="submit" hx-post="/new-briefing/" hx-target="#content" hx-swap="innerHTML">
|
||||
Neue Unterweisung
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th colspan="2">Unterweiser</th>
|
||||
<th>Datum</th>
|
||||
<th>Uhrzeit</th>
|
||||
<th>Stand</th>
|
||||
<th>Ort</th>
|
||||
<th colspan="2">Teilnehmer</th>
|
||||
<th>Firma</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody id="results">
|
||||
{{ template "rows" . }}
|
||||
</tbody>
|
||||
</table>
|
||||
{{ end }}
|
Reference in New Issue
Block a user