diff --git a/packages/data/db.go b/packages/data/db.go index 4e309a0..d15833f 100644 --- a/packages/data/db.go +++ b/packages/data/db.go @@ -89,32 +89,85 @@ func OpenDB(dbName string) (*DB, error) { return db, nil } -func (db *DB) WriteBriefing(b *types.Briefing) error { - 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) +func (db *DB) writeBriefing(b *types.Briefing) error { + result, err := db.Exec(` + INSERT INTO briefings + (date, time, location, document_name, as_of, instructor_id) + VALUES + (?, ?, ?, ?, ?, ?) + `, b.Date, b.Time, b.Location, b.DocumentName, b.AsOf, b.InstructorID) + if err != nil { + return fmt.Errorf("*DB.writeBriefing: db.Exec(): %v\n", err) + } + + _, err = result.LastInsertId() + if err != nil { + return fmt.Errorf("*DB.writeBriefing: result.LastInsertId(): %v\n", err) + } + + return nil +} + +func (db *DB) writeParticipants(sp *[]*types.Participant) error { + for _, p := range *sp { + result, err := db.Exec(` + INSERT INTO participants + (first_name, last_name, company) + VALUES + (?, ?, ?) + `, p.FirstName, p.LastName, p.Company) if err != nil { - 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) + return fmt.Errorf("*DB.writeParticipants: db.Exec(): %v\n", err) } _, err = result.LastInsertId() if err != nil { - return fmt.Errorf("*DB.WriteBriefing: result.LastInsertId(): %v\n", err) + return fmt.Errorf("*DB.writeParticipants: result.LastInsertId(): %v\n", err) } } return nil } +func (db *DB) writeGivenAnswers(b *types.Briefing, sp *[]*types.Participant, sq *[]*types.Question, sg *[]*types.GivenAnswer) error { + for _, p := range *sp { + for i, q := range *sq { + result, err := db.Exec(` + INSERT INTO given_answers + (briefing_id, participant_id, question_id, given_answer) + VALUES + (?, ?, ?, ?) + `, b.ID, p.ID, q.ID, (*sg)[i]) + if err != nil { + return fmt.Errorf("*DB.writeGivenAnswers: db.Exec(): %v\n", err) + } + + _, err = result.LastInsertId() + if err != nil { + return fmt.Errorf("*DB.writeGivenAnswers: result.LastInsertId(): %v\n", err) + } + } + } + + return nil +} + +func (db *DB) WriteAllDataOfBriefing(b *types.Briefing, sp *[]*types.Participant, sq *[]*types.Question, sg *[]*types.GivenAnswer) error { + if err := db.writeBriefing(b); err != nil { + return fmt.Errorf("*DB.WriteAllDataOfBriefing: db.writeBriefing(): %v\n", err) + } + + if err := db.writeParticipants(sp); err != nil { + return fmt.Errorf("*DB.WriteAllDataOfBriefing: db.writeParticipants(): %v\n", err) + } + + if err := db.writeGivenAnswers(b, sp, sq, sg); err != nil { + return fmt.Errorf("*DB.WriteAllDataOfBriefing: db.writeGivenAnswers(): %v\n", err) + } + + return nil +} + func (db *DB) GetAllOverviewTableData() (*[]*types.OverviewTableData, error) { rows, err := db.Query(` SELECT