Grundlegenden Funktionsumfang geschaffen, dafür einiges umstrukturiert
This commit is contained in:
parent
15675d5e6c
commit
324a1c54d6
23
main.go
23
main.go
@ -6,39 +6,24 @@ import (
|
||||
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/db"
|
||||
"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() {
|
||||
var i, j int64
|
||||
i, j = 1, 1
|
||||
|
||||
mux := http.NewServeMux()
|
||||
cb := make(chan *types.Briefing)
|
||||
|
||||
db, err := db.Open("sicherheitsunterweisung")
|
||||
if err != nil {
|
||||
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.HandleFunc("/", server.DisplayForm(&i))
|
||||
mux.HandleFunc("/", server.DisplayTable(bs))
|
||||
mux.HandleFunc("/", server.DisplayTable(db))
|
||||
mux.HandleFunc("/submit/", server.SubmitForm(db, &i, &j))
|
||||
mux.HandleFunc("/search/", server.DisplayResults(db))
|
||||
mux.HandleFunc("/new-briefing/", server.DisplayForm(&i))
|
||||
mux.HandleFunc("/add-participant/", server.AddParticipant(&i))
|
||||
mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j))
|
||||
|
||||
go writeBriefing(cb, db)
|
||||
|
||||
log.Fatalln(http.ListenAndServe(":8080", mux))
|
||||
}
|
||||
|
@ -5,18 +5,34 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/db"
|
||||
"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) {
|
||||
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 *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)
|
||||
}
|
||||
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", "templates/form.html")).Execute(w, i)
|
||||
template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "content", i)
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,7 +43,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) {
|
||||
b := new(types.Briefing)
|
||||
|
||||
@ -50,6 +66,11 @@ 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/index.html", "templates/table.html")).Execute(w, bs)
|
||||
}
|
||||
}
|
||||
|
@ -49,6 +49,8 @@
|
||||
{{ template "participant" . }}
|
||||
</div>
|
||||
|
||||
<button type="submit" hx-post="/submit/">Senden</button>
|
||||
<button type="submit" hx-post="/submit/" hx-target="#content" hx-swap="innerHTML">
|
||||
Senden
|
||||
</button>
|
||||
</form>
|
||||
{{ end }}
|
||||
|
@ -11,7 +11,9 @@
|
||||
<body>
|
||||
<h1>Sicherheitsunterweisung</h1>
|
||||
|
||||
{{ template "content" . }}
|
||||
<div id="content">
|
||||
{{ template "content" . }}
|
||||
</div>
|
||||
|
||||
<script src="/static/js/htmx.min.js" type="text/javascript"></script>
|
||||
</body>
|
||||
|
@ -1,4 +1,5 @@
|
||||
{{ define "row" }}
|
||||
{{ define "rows" }}
|
||||
{{ range . }}
|
||||
<tr>
|
||||
<td>{{ .FirstName }}</td>
|
||||
<td>{{ .LastName }}</td>
|
||||
@ -13,27 +14,38 @@
|
||||
{{ end }}
|
||||
</tr>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ define "content" }}
|
||||
<form>
|
||||
<label for="search-input">Suche</label>
|
||||
<input type="text" name="search" id="search-input" />
|
||||
<button type="submit" hx-post="" hx-target="" hx-swap="">Suchen</button>
|
||||
<button type="submit" hx-post="/search/" hx-target="#results" hx-swap="innerHTML">
|
||||
Suchen
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<form>
|
||||
<button type="submit" hx-post="/new-briefing/" hx-target="#content" hx-swap="innerHTML">
|
||||
Neue Unterweisung
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th colspan="2">Name Unterweiser</th>
|
||||
<th>Datum</th>
|
||||
<th>Uhrzeit</th>
|
||||
<th>Stand</th>
|
||||
<th>Ort</th>
|
||||
<th colspan="2">Name Teilnehmer</th>
|
||||
<th>Firma</th>
|
||||
</tr>
|
||||
<thead>
|
||||
<tr>
|
||||
<th colspan="2">Name Unterweiser</th>
|
||||
<th>Datum</th>
|
||||
<th>Uhrzeit</th>
|
||||
<th>Stand</th>
|
||||
<th>Ort</th>
|
||||
<th colspan="2">Name Teilnehmer</th>
|
||||
<th>Firma</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
{{ range . }}
|
||||
{{ template "row" . }}
|
||||
{{ end }}
|
||||
<tbody id="results">
|
||||
{{ template "rows" . }}
|
||||
</tbody>
|
||||
</table>
|
||||
{{ end }}
|
||||
|
Loading…
x
Reference in New Issue
Block a user