Instructor Form zum Laufen gebracht

This commit is contained in:
Jason Streifling 2023-10-20 16:33:00 +02:00
parent b78e30d109
commit d054b3644b
5 changed files with 55 additions and 17 deletions

View File

@ -36,7 +36,7 @@ func main() {
mux.HandleFunc("/internal-login/", server.DisplayTable(db)) mux.HandleFunc("/internal-login/", server.DisplayTable(db))
mux.HandleFunc("/external-login/", server.DisplayParticipantForm(sessions)) mux.HandleFunc("/external-login/", server.DisplayParticipantForm(sessions))
mux.HandleFunc("/search/", server.DisplaySearchResults(db)) mux.HandleFunc("/search/", server.DisplaySearchResults(db))
mux.HandleFunc("/new-briefing/", server.DisplayInstructorForm(sessionChan)) mux.HandleFunc("/new-briefing/", server.DisplayInstructorForm(db, sessionChan))
go handleSessions(mux, db, sessions, sessionChan) go handleSessions(mux, db, sessions, sessionChan)

View File

@ -291,10 +291,13 @@ func (db *DB) GetLastID(table string) (int, error) {
return id, nil return id, nil
} }
func (db *DB) GetInstructors() (*[]*types.Instructor, error) { func (db *DB) GetInstructors() ([]*types.Instructor, error) {
rows, err := db.Query(` rows, err := db.Query(`
SELECT * SELECT *
FROM instructors FROM instructors
ORDER BY
last_name,
first_name
`) `)
if err != nil { if err != nil {
return nil, fmt.Errorf("*DB.GetInstructors: db.Query(): %v\n", err) return nil, fmt.Errorf("*DB.GetInstructors: db.Query(): %v\n", err)
@ -303,11 +306,11 @@ func (db *DB) GetInstructors() (*[]*types.Instructor, error) {
instructors := make([]*types.Instructor, 0) instructors := make([]*types.Instructor, 0)
for rows.Next() { for rows.Next() {
instructor := new(types.Instructor) instructor := new(types.Instructor)
if err = rows.Scan(instructor); err != nil { if err = rows.Scan(&instructor.ID, &instructor.FirstName, &instructor.LastName, &instructor.PersonnelID); err != nil {
return nil, fmt.Errorf("*DB.GetInstructors: rows.Scan(): %v\n", err) return nil, fmt.Errorf("*DB.GetInstructors: rows.Scan(): %v\n", err)
} }
instructors = append(instructors, instructor) instructors = append(instructors, instructor)
} }
return &instructors, nil return instructors, nil
} }

View File

@ -5,6 +5,7 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"html/template" "html/template"
"log"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
@ -34,13 +35,38 @@ func DisplaySearchResults(db *db.DB) http.HandlerFunc {
} }
} }
func DisplayInstructorForm(cs chan<- *types.Session) http.HandlerFunc { func DisplayInstructorForm(db *db.DB, cs chan<- *types.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
type option struct {
ID int64
String string
}
type htmlData struct {
SessionID uuid.UUID
Options []option
}
session := new(types.Session) session := new(types.Session)
session.ID = uuid.New() session.ID = uuid.New()
cs <- session cs <- session
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", session.ID) instructors, err := db.GetInstructors()
if err != nil {
http.Error(w, "DisplayInstructorForm: db.GetInstructors(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
data := new(htmlData)
data.SessionID = session.ID
for _, instructor := range instructors {
option := new(option)
option.ID = instructor.ID
option.String = instructor.LastName + ", " + instructor.FirstName + ": " + fmt.Sprint(instructor.PersonnelID)
data.Options = append(data.Options, *option)
}
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", data)
} }
} }

View File

@ -8,7 +8,10 @@ type Person struct {
LastName string LastName string
} }
type Instructor Person type Instructor struct {
Person
PersonnelID int
}
type Participant struct { type Participant struct {
Person Person

View File

@ -12,26 +12,32 @@
{{ define "content" }} {{ define "content" }}
<form> <form>
<div id="instructor"> <div id="instructor">
<label for="instructor-first-input">Unterweiser Vorname</label> <label for="instructor">Unterweiser</label>
<input type="text" name="instructor-first" id="instructor-first-input" /> <select id="instructor" name="instructor">
{{ range .Options }}
<label for="instructor-last-input">Unterweiser Nachname</label> <option value="{{ .ID }}">{{ .String }}</option>
<input type="text" name="instructor-last" id="instructor-last-input" /> {{ end }}
</select>
</div> </div>
<div id="location"> <div id="location">
<label for="location-input">Ort</label> <label for="location">Ort</label>
<input type="text" name="location" id="location-input" /> <input type="text" name="location" id="location" />
</div>
<div id="document-name">
<label for="document-name">Dokument</label>
<input type="text" name="document-name" id="document-name" />
</div> </div>
<div id="state"> <div id="state">
<label for="state-input">Stand vom</label> <label for="as-of">Stand vom</label>
<input type="date" name="state" id="state-input" /> <input type="date" name="as-of" id="as-of" />
</div> </div>
{{ template "add-button" . }} {{ template "add-button" . }}
<button type="submit" hx-post="/submit-form/" hx-target="#content" hx-swap="innerHTML"> <button type="submit" hx-post="/submit-form-{{ .SessionID }}/" hx-target="#content" hx-swap="innerHTML">
OK OK
</button> </button>
</form> </form>