9 Commits

9 changed files with 114 additions and 48 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.21.1
require ( require (
github.com/go-sql-driver/mysql v1.7.1 github.com/go-sql-driver/mysql v1.7.1
github.com/google/uuid v1.3.1
golang.org/x/term v0.13.0 golang.org/x/term v0.13.0
) )

2
go.sum
View File

@ -1,5 +1,7 @@
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=

23
main.go
View File

@ -6,39 +6,24 @@ import (
"streifling.com/jason/sicherheitsunterweisung/packages/db" "streifling.com/jason/sicherheitsunterweisung/packages/db"
"streifling.com/jason/sicherheitsunterweisung/packages/server" "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 main() { func main() {
var i, j int64 var i, j int64
i, j = 1, 1 i, j = 1, 1
mux := http.NewServeMux() mux := http.NewServeMux()
cb := make(chan *types.Briefing)
db, err := db.Open("sicherheitsunterweisung") db, err := db.Open("sicherheitsunterweisung")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
bs, err := db.ReadAll()
if err != nil {
log.Fatalln(err)
}
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
// mux.HandleFunc("/", server.DisplayForm(&i)) mux.HandleFunc("/", server.DisplayTable(db))
mux.HandleFunc("/", server.DisplayTable(bs)) mux.HandleFunc("/search/", server.DisplayResults(db))
mux.HandleFunc("/new-briefing/", server.DisplayForm(&i))
mux.HandleFunc("/add-participant/", server.AddParticipant(&i)) mux.HandleFunc("/add-participant/", server.AddParticipant(&i))
mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j)) mux.HandleFunc("/submit/", server.SubmitForm(db, &i, &j))
go writeBriefing(cb, db)
log.Fatalln(http.ListenAndServe(":8080", mux)) log.Fatalln(http.ListenAndServe(":8080", mux))
} }

View File

@ -37,6 +37,12 @@ func getCredentials() (string, string, error) {
return strings.TrimSpace(user), strings.TrimSpace(pass), nil return strings.TrimSpace(user), strings.TrimSpace(pass), nil
} }
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 Open(dbName string) (*DB, error) { func Open(dbName string) (*DB, error) {
var err error var err error
db := new(DB) db := new(DB)
@ -63,18 +69,18 @@ func Open(dbName string) (*DB, error) {
func (db *DB) WriteBriefing(b *types.Briefing) error { func (db *DB) WriteBriefing(b *types.Briefing) error {
for i := 0; i < len(b.Participants); i++ { for i := 0; i < len(b.Participants); i++ {
result, err := db.Exec("INSERT INTO "+db.Name+" (instructor_first,"+ result, err := db.Exec("INSERT INTO "+db.Name+" (instructor_first,"+
"instructor_last, date, time, state, location, participant_first,"+ " instructor_last, date, time, state, location, participant_first,"+
"participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", " participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location,
b.Participants[i].FirstName, b.Participants[i].LastName, b.Participants[i].FirstName, b.Participants[i].LastName,
b.Participants[i].Company) b.Participants[i].Company)
if err != nil { if err != nil {
return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO"+ return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO"+
"\"+db.Name+\" (instructor_first, instructor_last, date, time, state,"+ " \"+db.Name+\" (instructor_first, instructor_last, date, time, state,"+
"location, participant_first, participant_last, company) VALUES (?, ?,"+ " location, participant_first, participant_last, company) VALUES (?, ?,"+
"?, ?, ?, ?, ?, ?, ?)\", b.FirstName, b.LastName, b.Date, b.Time,"+ " ?, ?, ?, ?, ?, ?, ?)\", b.FirstName, b.LastName, b.Date, b.Time,"+
"b.State, b.Location, b.Participants[i].FirstName,"+ " b.State, b.Location, b.Participants[i].FirstName,"+
"b.Participants[i].LastName, b.Participants[i].Company): %v\n", err) " b.Participants[i].LastName, b.Participants[i].Company): %v\n", err)
} }
_, err = result.LastInsertId() _, err = result.LastInsertId()
@ -108,6 +114,7 @@ func (db *DB) ReadAll() (*[]types.Briefing, error) {
bs = append(bs, *b) bs = append(bs, *b)
} }
reverseOrder(&bs)
return &bs, nil return &bs, nil
} }
@ -145,5 +152,6 @@ func (db *DB) ReadByName(name string) (*[]types.Briefing, error) {
bs = append(bs, *b) bs = append(bs, *b)
} }
reverseOrder(&bs)
return &bs, nil return &bs, nil
} }

View File

@ -2,21 +2,41 @@ package server
import ( import (
"fmt" "fmt"
"github.com/google/uuid"
"html/template" "html/template"
"io"
"log" "log"
"net/http" "net/http"
"os"
"streifling.com/jason/sicherheitsunterweisung/packages/db"
"streifling.com/jason/sicherheitsunterweisung/packages/types" "streifling.com/jason/sicherheitsunterweisung/packages/types"
) )
func DisplayTable(bs *[]types.Briefing) http.HandlerFunc { func DisplayTable(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.ReadAll()
if err != nil {
_ = fmt.Errorf("DisplayTable: %v\n", err)
return
}
template.Must(template.ParseFiles("templates/index.html", "templates/table.html")).Execute(w, bs) template.Must(template.ParseFiles("templates/index.html", "templates/table.html")).Execute(w, bs)
} }
} }
func DisplayResults(db *db.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)
return
}
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs)
}
}
func DisplayForm(i *int64) http.HandlerFunc { func DisplayForm(i *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/index.html", "templates/form.html")).Execute(w, i) template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "content", i)
} }
} }
@ -27,7 +47,7 @@ func AddParticipant(i *int64) http.HandlerFunc {
} }
} }
func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc { func SubmitForm(db *db.DB, i, j *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
b := new(types.Briefing) b := new(types.Briefing)
@ -50,6 +70,37 @@ func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc {
} }
log.Println(b) log.Println(b)
ch <- b db.WriteBriefing(b)
bs, err := db.ReadAll()
if err != nil {
_ = fmt.Errorf("SubmitForm: db.ReadAll(): %v\n", err)
return
}
template.Must(template.ParseFiles("templates/index.html", "templates/table.html")).Execute(w, bs)
}
}
func Upload(fn, ln, d, t string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(10 << 20) // 10 MiB
tmpFile, _, err := r.FormFile("questionaire")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer tmpFile.Close()
file, err := os.Create(ln + fn + d + t + uuid.New().String() + ".png")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
if _, err := io.Copy(file, tmpFile); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} }
} }

View File

@ -1,5 +1,7 @@
package types package types
import "os"
type Person struct { type Person struct {
FirstName string FirstName string
LastName string LastName string
@ -10,7 +12,8 @@ type Instructor Person
type Participant struct { type Participant struct {
ID int64 ID int64
Person Person
Company string Company string
Questionaire string
} }
type Briefing struct { type Briefing struct {

View File

@ -8,6 +8,8 @@
<label for="participant-company-input-{{ . }}">Firma</label> <label for="participant-company-input-{{ . }}">Firma</label>
<input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" /> <input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
<button type="button" hx-post="/upload/">Fragebogen hochladen</button>
</div> </div>
{{ end }} {{ end }}
@ -49,6 +51,8 @@
{{ template "participant" . }} {{ template "participant" . }}
</div> </div>
<button type="submit" hx-post="/submit/">Senden</button> <button type="submit" hx-post="/submit/" hx-target="#content" hx-swap="innerHTML">
Senden
</button>
</form> </form>
{{ end }} {{ end }}

View File

@ -11,7 +11,9 @@
<body> <body>
<h1>Sicherheitsunterweisung</h1> <h1>Sicherheitsunterweisung</h1>
{{ template "content" . }} <div id="content">
{{ template "content" . }}
</div>
<script src="/static/js/htmx.min.js" type="text/javascript"></script> <script src="/static/js/htmx.min.js" type="text/javascript"></script>
</body> </body>

View File

@ -1,4 +1,5 @@
{{ define "row" }} {{ define "rows" }}
{{ range . }}
<tr> <tr>
<td>{{ .FirstName }}</td> <td>{{ .FirstName }}</td>
<td>{{ .LastName }}</td> <td>{{ .LastName }}</td>
@ -13,27 +14,36 @@
{{ end }} {{ end }}
</tr> </tr>
{{ end }} {{ end }}
{{ end }}
{{ define "content" }} {{ define "content" }}
<form> <form>
<label for="search-input">Suche</label> <label for="search-input">Suche</label>
<input type="text" name="search" id="search-input" /> <input type="text" name="search" id="search-input" hx-post="/search/" hx-target="#results" hx-swap="innerHTML"
<button type="submit" hx-post="" hx-target="" hx-swap="">Suchen</button> 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> </form>
<table> <table>
<tr> <thead>
<th colspan="2">Name Unterweiser</th> <tr>
<th>Datum</th> <th colspan="2">Unterweiser</th>
<th>Uhrzeit</th> <th>Datum</th>
<th>Stand</th> <th>Uhrzeit</th>
<th>Ort</th> <th>Stand</th>
<th colspan="2">Name Teilnehmer</th> <th>Ort</th>
<th>Firma</th> <th colspan="2">Teilnehmer</th>
</tr> <th>Firma</th>
</tr>
</thead>
{{ range . }} <tbody id="results">
{{ template "row" . }} {{ template "rows" . }}
{{ end }} </tbody>
</table> </table>
{{ end }} {{ end }}