diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8a398a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +create_tables.sql diff --git a/main.go b/main.go index f16135c..3b0bd1f 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,12 @@ import ( "streifling.com/jason/sicherheitsunterweisung/packages/types" ) +func writeBriefing(ch chan *types.Briefing, db *db.DB) { + for b := range ch { + db.WriteBriefing(b) + } +} + func main() { var i, j int64 @@ -26,11 +32,7 @@ func main() { mux.HandleFunc("/add-participant/", server.AddParticipant(&i)) mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j)) - go func() { - for b := range cb { - db.WriteBriefing(b) - } - }() + go writeBriefing(cb, db) log.Fatalln(http.ListenAndServe(":8080", mux)) } diff --git a/packages/db/db.go b/packages/db/db.go index 2b8c8d0..94c1ef7 100644 --- a/packages/db/db.go +++ b/packages/db/db.go @@ -38,7 +38,7 @@ func getCredentials() (string, string, error) { } func OpenDB(dbName string) (*DB, error) { - var db *DB + var db DB var err error cfg := mysql.NewConfig() @@ -57,16 +57,54 @@ func OpenDB(dbName string) (*DB, error) { return nil, fmt.Errorf("newDB: db.Ping(): %v\n", err) } - return db, nil + return &db, nil } func (db *DB) WriteBriefing(b *types.Briefing) error { - for i := 1; i < len(b.Participants); i++ { - _, err := db.Exec("INSERT INTO ? (instructor_first, instructor_last, date, time, state, location, participant_first, participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", db.Name, b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, b.Participants[i].FirstName, b.Participants[i].LastName, b.Participants[i].Company) + for i := 0; i < len(b.Participants); i++ { + result, err := db.Exec("INSERT INTO "+db.Name+" (instructor_first,"+ + "instructor_last, date, time, state, location, participant_first,"+ + "participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", + b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, + b.Participants[i].FirstName, b.Participants[i].LastName, + b.Participants[i].Company) if err != nil { - return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO ? (instructor_first, instructor_last, date, time, state, location, participant_first, participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)\", db.Name, b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, b.Participants[i].FirstName, b.Participants[i].LastName, b.Participants[i].Company): %v\n", err) + return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO"+ + "\"+db.Name+\" (instructor_first, instructor_last, date, time, state,"+ + "location, participant_first, participant_last, company) VALUES (?, ?,"+ + "?, ?, ?, ?, ?, ?, ?)\", b.FirstName, b.LastName, b.Date, b.Time,"+ + "b.State, b.Location, b.Participants[i].FirstName,"+ + "b.Participants[i].LastName, b.Participants[i].Company): %v\n", err) + } + + _, err = result.LastInsertId() + if err != nil { + return fmt.Errorf("*DB.WriteBriefing: result.LastInsertId(): %v\n", err) } } return nil } + +func (db *DB) ReadByName(name string) (*[]types.Briefing, error) { + var b types.Briefing + var bs []types.Briefing + + rows, err := db.Query("SELECT * FROM " + db.Name + " WHERE" + + " instructor_first LIKE " + name + " OR instructor_last LIKE " + name + + " participant_first LIKE " + name + " participant_last LIKE " + name) + if err != nil { + return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT * FROM \"+db.Name+\" WHERE" + + " instructor_first LIKE \"+name+\" OR instructor_last LIKE \"+name+" + + " participant_first LIKE \"+name+\" participant_last LIKE \"+name)") + } + + for rows.Next() { + if err := rows.Scan(&b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &b.Participants[0].FirstName, &b.Participants[0].LastName, &b.Participants[0].Company); err != nil { + return nil, fmt.Errorf("*DB.ReadByName: rows.Scan(&b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &b.Participants[0].FirstName, &b.Participants[0].LastName, &b.Participants[0].Company)") + } + bs = append(bs, b) + } + + return &bs, nil +}