From c38d3131c68b05154eae1be23d1ebec3cf2cb911 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Tue, 17 Oct 2023 17:30:39 +0200 Subject: [PATCH] db.ReadAllBriefings zu db.GetAllOverviewTableData umbenannt und die Funktion an das neue DB-Layout angepasst --- packages/data/db.go | 56 +++++++++++++++++++++++++++++---------- packages/server/server.go | 8 +++--- packages/types/types.go | 13 +++++++++ 3 files changed, 59 insertions(+), 18 deletions(-) diff --git a/packages/data/db.go b/packages/data/db.go index 1772ea9..9f1c072 100644 --- a/packages/data/db.go +++ b/packages/data/db.go @@ -115,30 +115,58 @@ func (db *DB) WriteBriefing(b *types.Briefing) error { return nil } -func (db *DB) ReadAllBriefings() ([]*types.Briefing, error) { - bs := make([]*types.Briefing, 0) - - rows, err := db.Query("SELECT * FROM " + db.Name) +func (db *DB) GetAllOverviewTableData() (*[]*types.OverviewTableData, error) { + rows, err := db.Query(` + SELECT + i.first_name, + i.last_name, + b.date, + b.time, + b.location, + b.document_name, + b.as_of, + p.first_name, + p.last_name, + p.company + FROM given_answers AS g + INNER JOIN briefings AS b + ON b.id = g.briefing_id + INNER JOIN participants AS p + ON p.id = g.participant_id + INNER JOIN instructors AS i + ON i.id = b.instructor_id + ORDER BY + b.id DESC, + p.id`) if err != nil { - return nil, fmt.Errorf("*DB.ReadAll: db.Query(\"SELECT * FROM \"+db.Name): %v\n", err) + return nil, fmt.Errorf("*DB.ReadAllBriefings: db.Query(): %v\n", err) } defer rows.Close() + data := make([]*types.OverviewTableData, 0) for rows.Next() { - b := new(types.Briefing) - p := new(types.Participant) + otd := new(types.OverviewTableData) - 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) + err := rows.Scan( + &otd.InstructorFirstName, + &otd.InstructorLastName, + &otd.BriefingDate, + &otd.BriefingTime, + &otd.BriefingLocation, + &otd.BriefingDocumentName, + &otd.BriefingAsOf, + &otd.ParticipantFirstName, + &otd.ParticipantLastName, + &otd.ParticipantCompany, + ) + if err != nil { + return nil, fmt.Errorf("*DB.ReadAllBriefings: rows.Scan(): %v\n", err) } - b.Participants = append(b.Participants, p) - bs = append(bs, b) + data = append(data, otd) } - reverseOrder(bs) - return bs, nil + return &data, nil } func (db *DB) ReadByName(name string) ([]*types.Briefing, error) { diff --git a/packages/server/server.go b/packages/server/server.go index ae31c47..e8f73ae 100644 --- a/packages/server/server.go +++ b/packages/server/server.go @@ -23,9 +23,9 @@ type questionData struct { func DisplayTable(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - bs, err := db.ReadAllBriefings() + bs, err := db.GetAllOverviewTableData() if err != nil { - _ = fmt.Errorf("DisplayTable: %v\n", err) + _ = fmt.Errorf("DisplayTable: *DB.GetAllOverviewTableData(): %v\n", err) } template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs) } @@ -91,9 +91,9 @@ func SubmitForm(db *data.DB, i, j *int64) http.HandlerFunc { log.Println(b) db.WriteBriefing(b) - bs, err := db.ReadAllBriefings() + bs, err := db.GetAllOverviewTableData() if err != nil { - _ = fmt.Errorf("SubmitForm: db.ReadAll(): %v\n", err) + _ = fmt.Errorf("SubmitForm: db.GetAllOverviewTableData(): %v\n", err) } template.Must(template.ParseFiles("templates/table.html")).Execute(w, bs) diff --git a/packages/types/types.go b/packages/types/types.go index f555218..6615f09 100644 --- a/packages/types/types.go +++ b/packages/types/types.go @@ -41,3 +41,16 @@ type GivenAnswer struct { QuestionID int GivenAnswer int } + +type OverviewTableData struct { + InstructorFirstName string + InstructorLastName string + BriefingDate string + BriefingTime string + BriefingLocation string + BriefingDocumentName string + BriefingAsOf string + ParticipantFirstName string + ParticipantLastName string + ParticipantCompany string +}