diff --git a/main.go b/main.go index b0baab8..b4ea527 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,7 @@ func main() { mux.HandleFunc("/internal-login/", server.DisplayTable(db)) mux.HandleFunc("/external-login/", server.DisplayParticipantForm(sessions)) 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) diff --git a/packages/db/db.go b/packages/db/db.go index 720c3aa..6124258 100644 --- a/packages/db/db.go +++ b/packages/db/db.go @@ -291,10 +291,13 @@ func (db *DB) GetLastID(table string) (int, error) { return id, nil } -func (db *DB) GetInstructors() (*[]*types.Instructor, error) { +func (db *DB) GetInstructors() ([]*types.Instructor, error) { rows, err := db.Query(` SELECT * FROM instructors + ORDER BY + last_name, + first_name `) if err != nil { 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) for rows.Next() { 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) } instructors = append(instructors, instructor) } - return &instructors, nil + return instructors, nil } diff --git a/packages/server/server.go b/packages/server/server.go index c6e84b6..5ffb46b 100644 --- a/packages/server/server.go +++ b/packages/server/server.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "fmt" "html/template" + "log" "net/http" "strconv" "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) { + type option struct { + ID int64 + String string + } + + type htmlData struct { + SessionID uuid.UUID + Options []option + } + session := new(types.Session) session.ID = uuid.New() 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) } } diff --git a/packages/types/types.go b/packages/types/types.go index 46f9d48..5c9c84d 100644 --- a/packages/types/types.go +++ b/packages/types/types.go @@ -8,7 +8,10 @@ type Person struct { LastName string } -type Instructor Person +type Instructor struct { + Person + PersonnelID int +} type Participant struct { Person diff --git a/templates/briefing.html b/templates/briefing.html index ec3e343..3c15be8 100644 --- a/templates/briefing.html +++ b/templates/briefing.html @@ -12,26 +12,32 @@ {{ define "content" }}
- - - - - + +
- - + + +
+ +
+ +
- - + +
{{ template "add-button" . }} -