diff options
-rw-r--r-- | cmd/web/handlers.go | 13 | ||||
-rw-r--r-- | internal/models/engagement.go | 32 | ||||
-rw-r--r-- | internal/models/operation.go | 11 | ||||
-rw-r--r-- | ui/html/pages/operations/list.tmpl.html | 12 |
4 files changed, 46 insertions, 22 deletions
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 0a7be55..6901661 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -18,15 +18,20 @@ func (app *application) listOperations(w http.ResponseWriter, r *http.Request) { return } - var esses []models.EngagementStrategy + var newOps []models.Operation for _, op := range ops { - es, err := app.engagement_strategies.GetForOperation(op.ID) + esses, err := app.engagement_strategies.GetForOperation(op.ID) // TODO: Check what kind of error this is, don't just continue if err != nil { continue } - esses = append(esses, es) + if len(esses) > 0 { + op.EngagementStrategies = esses + newOps = append(newOps, op) + } else { + newOps = append(newOps, op) + } } files := []string{ @@ -42,7 +47,7 @@ func (app *application) listOperations(w http.ResponseWriter, r *http.Request) { } data := operationsTemplateData{ - Operations: ops, + Operations: newOps, } err = ts.ExecuteTemplate(w, "base", data) diff --git a/internal/models/engagement.go b/internal/models/engagement.go index db1c2f5..26b468a 100644 --- a/internal/models/engagement.go +++ b/internal/models/engagement.go @@ -2,7 +2,6 @@ package models import ( "database/sql" - "errors" "time" ) @@ -17,20 +16,31 @@ type EngagementStrategyModel struct { DB *sql.DB } -func (m *EngagementStrategyModel) GetForOperation(id int) (EngagementStrategy, error) { +func (m *EngagementStrategyModel) GetForOperation(id int) ([]EngagementStrategy, error) { stmt := `SELECT id, valid_from, valid_to FROM engagement_strategies WHERE operation_id = ?` - row := m.DB.QueryRow(stmt, id) + rows, err := m.DB.Query(stmt, id) + if err != nil { + return nil, err + } - var es EngagementStrategy + defer rows.Close() - err := row.Scan(&es.ID, &es.ValidFrom, &es.ValidTo) - if err != nil { - if errors.Is(err, sql.ErrNoRows) { - return EngagementStrategy{}, ErrNoRecord - } else { - return EngagementStrategy{}, err + var esses []EngagementStrategy + + for rows.Next() { + var es EngagementStrategy + err = rows.Scan(&es.ID, &es.ValidFrom, &es.ValidTo) + if err != nil { + return nil, err } + + esses = append(esses, es) + } + + if err = rows.Err(); err != nil { + return nil, err } - return es, nil + + return esses, nil } diff --git a/internal/models/operation.go b/internal/models/operation.go index 4c5d67f..ecfd31b 100644 --- a/internal/models/operation.go +++ b/internal/models/operation.go @@ -6,11 +6,12 @@ import ( ) type Operation struct { - ID int - Name string - Description string - Created time.Time - OrganisationName string + ID int + Name string + Description string + Created time.Time + OrganisationName string + EngagementStrategies []EngagementStrategy } type OperationModel struct { diff --git a/ui/html/pages/operations/list.tmpl.html b/ui/html/pages/operations/list.tmpl.html index 7207ab6..2e5a07b 100644 --- a/ui/html/pages/operations/list.tmpl.html +++ b/ui/html/pages/operations/list.tmpl.html @@ -17,7 +17,7 @@ Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod <th>Description</th> <th>Organisation</th> <th>Engagement Plan</th> - <th>Engagement Strategy</th> + <th>Engagement Strategies</th> <th>SharePoint</th> </tr> </thead> @@ -30,7 +30,15 @@ Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod <td>{{.Description}}</td> <td>{{.OrganisationName}}</td> <td><a href="#">EP 2024</a></td> - <td><a href="#">ES 2023-2025</a></td> + <td> + {{if .EngagementStrategies}} + {{range .EngagementStrategies}} + {{.}} + {{end}} + {{else}} + <p>NA</p> + {{end}} + </td> <td><a href="#">Link</a></td> </tr> {{end}} |