db.ReadAllBriefings zu db.GetAllOverviewTableData umbenannt und die Funktion an das neue DB-Layout angepasst

This commit is contained in:
Jason Streifling 2023-10-17 17:30:39 +02:00
parent e4d2f9ae3e
commit c38d3131c6
3 changed files with 59 additions and 18 deletions

View File

@ -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) {

View File

@ -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)

View File

@ -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
}