From 8ae3019b9cdfa48fbe78f9e2188c27a8f8a918ca Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sat, 7 Oct 2023 17:22:55 +0200 Subject: [PATCH] =?UTF-8?q?*DB.ReadAll()=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- packages/db/db.go | 25 +++++++++++++++++++++++++ packages/server/server.go | 4 ++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 946afbd..a36e40b 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,8 @@ func writeBriefing(ch chan *types.Briefing, db *db.DB) { func main() { var i, j int64 - i, j = 1, 1 + mux := http.NewServeMux() cb := make(chan *types.Briefing) diff --git a/packages/db/db.go b/packages/db/db.go index 4a82091..05389bb 100644 --- a/packages/db/db.go +++ b/packages/db/db.go @@ -86,6 +86,31 @@ func (db *DB) WriteBriefing(b *types.Briefing) error { return nil } +func (db *DB) ReadAll() (*[]types.Briefing, error) { + bs := make([]types.Briefing, 0) + + rows, err := db.Query("SELECT *" + " FROM " + db.Name) + if err != nil { + return nil, fmt.Errorf("*DB.ReadAll: db.Query(\"SELECT * FROM \"+db.Name): %v\n", err) + } + defer rows.Close() + + for rows.Next() { + b := new(types.Briefing) + p := new(types.Participant) + + if err := rows.Scan(&p.ID, &b.FirstName, &b.LastName, &b.Date, &b.Time, + &b.State, &b.Location, &p.FirstName, &p.LastName, &p.Company); err != nil { + return nil, fmt.Errorf("*DB.ReadAll: db.Query(): %v\n", err) + } + + b.Participants = append(b.Participants, *p) + bs = append(bs, *b) + } + + return &bs, nil +} + func (db *DB) ReadByName(name string) (*[]types.Briefing, error) { bs := make([]types.Briefing, 0) diff --git a/packages/server/server.go b/packages/server/server.go index dbab18e..28ae6b1 100644 --- a/packages/server/server.go +++ b/packages/server/server.go @@ -10,14 +10,14 @@ import ( func DisplayForm(i *int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("templates/index.html")).Execute(w, i) + template.Must(template.ParseFiles("templates/form.html")).Execute(w, i) } } func AddParticipant(i *int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { *i++ - template.Must(template.ParseFiles("templates/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i) + template.Must(template.ParseFiles("templates/form.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i) } }