From aded71394dbe35c96848c57896bc0032d3ec6d5f Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 26 Oct 2023 10:05:21 +0200 Subject: [PATCH] =?UTF-8?q?Kleine=20=C3=84nderungen=20f=C3=BCr=20Sessions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/server/server.go | 61 ++++++++++++++++++++++++++------------- templates/briefing.html | 24 ++++++++------- 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/packages/server/server.go b/packages/server/server.go index 5ffb46b..0deff44 100644 --- a/packages/server/server.go +++ b/packages/server/server.go @@ -15,13 +15,17 @@ import ( "streifling.com/jason/sicherheitsunterweisung/packages/types" ) +func displayTable(w http.ResponseWriter, db *db.DB) { + bs, err := db.GetAllOverviewTableData() + if err != nil { + http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError) + } + template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs) +} + func DisplayTable(db *db.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - bs, err := db.GetAllOverviewTableData() - if err != nil { - http.Error(w, "DisplayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError) - } - template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs) + displayTable(w, db) } } @@ -82,36 +86,53 @@ func generateUUID() (string, error) { func AddParticipant(s *types.Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - login, err := generateUUID() + type httpData struct { + SessionID uuid.UUID + Login string + } + + data := new(httpData) + var err error + + data.SessionID = s.ID + data.Login, err = generateUUID() if err != nil { http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError) } - s.Logins = append(s.Logins, login) - template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login) + s.Logins = append(s.Logins, data.Login) + template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data) } } -// TODO: Hier weiter machen, irgendwie die b.ID herausgeben, -// am besten hier auch die p.IDs rausgeben, damit diese später verknüpft werden können func SubmitBriefingForm(s *types.Session, db *db.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { now := time.Now() briefing := new(types.Briefing) - - // TODO: Dropdownmenü - // instructorFirstName := r.PostFormValue("instructor-first") - // instructorLastName := r.PostFormValue("instructor-last") + var err error briefing.Date = now.Format("2006-01-02") briefing.Time = now.Format("15:04:05") briefing.Location = r.PostFormValue("location") - briefing.DocumentName = r.PostFormValue("document") // TODO: in HTML einfügen - briefing.AsOf = r.PostFormValue("state") // TODO: Umbenennen - // briefing.InstructorID = r.PostFormValue("instructor-id") // TODO: aus Dropdown holen + briefing.DocumentName = r.PostFormValue("document-name") + briefing.AsOf = r.PostFormValue("as-of") + + briefing.InstructorID, err = strconv.ParseInt(r.PostFormValue("instructor"), 10, 64) + if err != nil { + http.Error(w, "SubmitBriefingForm: strconv.ParseInt(): "+fmt.Sprint(err), http.StatusInternalServerError) + log.Panicln(err) + } + + err = db.WriteBriefing(briefing) + if err != nil { + http.Error(w, "SubmitBriefingForm: db.WriteBriefing(): "+fmt.Sprint(err), http.StatusInternalServerError) + log.Panicln(err) + } - db.WriteBriefing(briefing) s.BriefingID = briefing.ID + s.InstructorID = briefing.InstructorID + + displayTable(w, db) } } @@ -142,13 +163,13 @@ func newParticipant(l string) (*types.Participant, error) { func DisplayParticipantForm(ss []*types.Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - type data struct { + type httpData struct { SessionID uuid.UUID UUID string } if loginIsCorrect(r.PostFormValue("login"), ss) { - data := new(data) + data := new(httpData) var err error data.UUID, err = generateUUID() diff --git a/templates/briefing.html b/templates/briefing.html index 3c15be8..6ee3cfc 100644 --- a/templates/briefing.html +++ b/templates/briefing.html @@ -1,12 +1,18 @@ -{{ define "add-button" }} - +{{ define "add-buttons" }} +
+ + + +
{{ end }} {{ define "new" }} -{{ . }} -{{ template "add-button" . }} +{{ template "add-buttons" . }} +

{{ .Login }}

{{ end }} {{ define "content" }} @@ -35,10 +41,6 @@ - {{ template "add-button" . }} - - + {{ template "add-buttons" . }} {{ end }}