From c69bfdfab272036dad3e7e1b555a5f7d571cfd4d Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Tue, 17 Oct 2023 17:45:26 +0200 Subject: [PATCH] db.ReadByName zu db.GetOverviewTableDataByName umbenannt und an die neue DB-Struktur angepasst --- packages/data/db.go | 79 +++++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/packages/data/db.go b/packages/data/db.go index 9f1c072..c3d938e 100644 --- a/packages/data/db.go +++ b/packages/data/db.go @@ -137,7 +137,8 @@ func (db *DB) GetAllOverviewTableData() (*[]*types.OverviewTableData, error) { ON i.id = b.instructor_id ORDER BY b.id DESC, - p.id`) + p.id + `) if err != nil { return nil, fmt.Errorf("*DB.ReadAllBriefings: db.Query(): %v\n", err) } @@ -169,42 +170,64 @@ func (db *DB) GetAllOverviewTableData() (*[]*types.OverviewTableData, error) { return &data, nil } -func (db *DB) ReadByName(name string) ([]*types.Briefing, error) { - bs := make([]*types.Briefing, 0) - - rows, err := db.Query("SELECT *"+ - " FROM "+db.Name+ - " WHERE instructor_first LIKE ?"+ - " OR instructor_last LIKE ?"+ - " OR participant_first LIKE ?"+ - " OR participant_last LIKE ?", - "%"+name+"%", "%"+name+"%", "%"+name+"%", "%"+name+"%") +func (db *DB) GetOverviewTableDataByName(n string) (*[]*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 + WHERE + i.first_name LIKE ? OR + i.last_name LIKE ? OR + p.first_name LIKE ? OR + p.last_name LIKE ? + ORDER BY + b.id DESC, + p.id + `, "%"+n+"%", "%"+n+"%", "%"+n+"%", "%"+n+"%") if err != nil { - return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT *"+ - " FROM \"+db.Name+"+ - " WHERE instructor_first LIKE ?"+ - " OR instructor_last LIKE ?"+ - " OR participant_first LIKE ?"+ - " OR participant_last LIKE ?\"): %v\n", err) + return nil, fmt.Errorf("*DB.GetOverviewTableDataByName: 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.ReadByName: rows.Scan(&p.ID, &b.FirstName,"+ - " &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &p.FirstName,"+ - " &p.LastName, &p.Company): %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) GetLastID(i *int64) error {