diff options
author | Matthew Lemon <y@yulqen.org> | 2024-04-18 11:06:32 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-04-18 11:06:32 +0100 |
commit | 20560419614e22fbb58567cdb0b88b54caf679f4 (patch) | |
tree | 163df44f07907d865b363db3bc198287ebd3d25f /internal/models/engagement.go | |
parent | 435742cede199e3c85b5e2eb5a42ccbee4906a05 (diff) |
Adds code from ded-go-core - no database
D2S test app code removed (nginx error).
Adds all go code from `ded-go-core` using the basic Gov.UK UI for a test
page for DED.
Diffstat (limited to '')
-rw-r--r-- | internal/models/engagement.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/internal/models/engagement.go b/internal/models/engagement.go new file mode 100644 index 0000000..87e0786 --- /dev/null +++ b/internal/models/engagement.go @@ -0,0 +1,54 @@ +package models + +import ( + "database/sql" + "fmt" + "strconv" + "time" +) + +type EngagementStrategy struct { + ID int + ValidFrom time.Time + ValidTo time.Time + Operation Operation +} + +func (es *EngagementStrategy) FormatForTable() string { + startYear := strconv.Itoa(es.ValidFrom.Year()) + endYear := strconv.Itoa(es.ValidTo.Year()) + return fmt.Sprintf("%s-%s", startYear, endYear) +} + +type EngagementStrategyModel struct { + DB *sql.DB +} + +func (m *EngagementStrategyModel) GetForOperation(id int) ([]EngagementStrategy, error) { + stmt := `SELECT id, valid_from, valid_to FROM engagement_strategies +WHERE operation_id = ?` + rows, err := m.DB.Query(stmt, id) + if err != nil { + return nil, err + } + + defer rows.Close() + + 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 esses, nil +} |