From 4de7d36385f6faf8fe47a2cc8f84e8817cc0ab5b Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 19 Oct 2023 20:05:26 +0200 Subject: [PATCH] server weitgehend auf Sessions umgestellt --- packages/server/server.go | 57 ++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/packages/server/server.go b/packages/server/server.go index f8735ab..c6e84b6 100644 --- a/packages/server/server.go +++ b/packages/server/server.go @@ -9,18 +9,12 @@ import ( "strconv" "time" - "streifling.com/jason/sicherheitsunterweisung/packages/data" + "github.com/google/uuid" + "streifling.com/jason/sicherheitsunterweisung/packages/db" "streifling.com/jason/sicherheitsunterweisung/packages/types" ) -// type questionData struct { -// ID int64 -// Q types.Question -// I int -// J int -// } - -func DisplayTable(db *data.DB) http.HandlerFunc { +func DisplayTable(db *db.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { bs, err := db.GetAllOverviewTableData() if err != nil { @@ -30,7 +24,7 @@ func DisplayTable(db *data.DB) http.HandlerFunc { } } -func DisplaySearchResults(db *data.DB) http.HandlerFunc { +func DisplaySearchResults(db *db.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search")) if err != nil { @@ -40,9 +34,13 @@ func DisplaySearchResults(db *data.DB) http.HandlerFunc { } } -func DisplayInstructorForm() http.HandlerFunc { +func DisplayInstructorForm(cs chan<- *types.Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", nil) + session := new(types.Session) + session.ID = uuid.New() + cs <- session + + template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", session.ID) } } @@ -56,21 +54,21 @@ func generateUUID() (string, error) { return hex.EncodeToString(bs), nil } -func AddParticipant(sl *[]string) http.HandlerFunc { +func AddParticipant(s *types.Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { login, err := generateUUID() if err != nil { http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError) } - (*sl) = append(*sl, login) + s.Logins = append(s.Logins, login) template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login) } } // 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(db *data.DB, sl *[]string) http.HandlerFunc { +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) @@ -87,15 +85,18 @@ func SubmitBriefingForm(db *data.DB, sl *[]string) http.HandlerFunc { // briefing.InstructorID = r.PostFormValue("instructor-id") // TODO: aus Dropdown holen db.WriteBriefing(briefing) + s.BriefingID = briefing.ID } } // TODO: Make it only serve one purpose -func loginIsCorrect(l string, logins *[]string) bool { - for i, v := range *logins { - if l == v { - (*logins) = append((*logins)[:i], (*logins)[i+1:]...) - return true +func loginIsCorrect(l string, ss []*types.Session) bool { + for _, session := range ss { + for i, v := range session.Logins { + if l == v { + session.Logins = append(session.Logins[:i], session.Logins[i+1:]...) + return true + } } } return false @@ -113,15 +114,23 @@ func newParticipant(l string) (*types.Participant, error) { return p, nil } -func DisplayParticipantForm(sl *[]string) http.HandlerFunc { +func DisplayParticipantForm(ss []*types.Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if loginIsCorrect(r.PostFormValue("login"), sl) { - uuid, err := generateUUID() + type data struct { + SessionID uuid.UUID + UUID string + } + + if loginIsCorrect(r.PostFormValue("login"), ss) { + data := new(data) + var err error + + data.UUID, err = generateUUID() if err != nil { http.Error(w, "DisplayParticipantForm: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError) } - template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", uuid) + template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data) } else { template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil) }