From b42f739581645d8c7a6d1bb8fb7ff7c11ab5e083 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Wed, 18 Oct 2023 16:21:44 +0200 Subject: [PATCH] =?UTF-8?q?DB.GetLastID()=20funktioniert=20wieder=20und=20?= =?UTF-8?q?DB.GetInstructors()=20erstellt=20f=C3=BCr=20Darstellung=20in=20?= =?UTF-8?q?Drop-Down-Men=C3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/data/db.go | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/data/db.go b/packages/data/db.go index d15833f..1c4528d 100644 --- a/packages/data/db.go +++ b/packages/data/db.go @@ -283,15 +283,40 @@ func (db *DB) GetOverviewTableDataByName(n string) (*[]*types.OverviewTableData, return &data, nil } -// TODO: So nicht mehr nützlich, zeitnah streichen -func (db *DB) GetLastID(i *int64) error { - row := db.QueryRow("SELECT id" + - " FROM " + db.Name + - " ORDER BY id DESC LIMIT 0, 1") +func (db *DB) GetLastID(table string) (int, error) { + var id int - if err := row.Scan(i); err != nil { - return fmt.Errorf("*DB.GetLastID: row.Scan(&i): %v\n", err) + row := db.QueryRow(` + SELECT id + FROM ? + ORDER BY id DESC + LIMIT 0, 1 + `, table) + + if err := row.Scan(&id); err != nil { + return -1, fmt.Errorf("*DB.GetLastID: row.Scan(): %v\n", err) } - return nil + return id, nil +} + +func (db *DB) GetInstructors() (*[]*types.Instructor, error) { + rows, err := db.Query(` + SELECT * + FROM instructors + `) + if err != nil { + return nil, fmt.Errorf("*DB.GetInstructors: db.Query(): %v\n", err) + } + + instructors := make([]*types.Instructor, 0) + for rows.Next() { + instructor := new(types.Instructor) + if err = rows.Scan(instructor); err != nil { + return nil, fmt.Errorf("*DB.GetInstructors: rows.Scan(): %v\n", err) + } + instructors = append(instructors, instructor) + } + + return &instructors, nil }