diff options
author | Yulqen <246857+yulqen@users.noreply.github.com> | 2024-04-18 13:58:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-18 13:58:11 +0100 |
commit | 69017cc225e2754466b8444ca42cb1122208425d (patch) | |
tree | 6a7ac8d150b95c1fad9229d124a23737abc17964 /internal/models/engagement.go | |
parent | 530f08071fc1295fabdaedf9724b8cda48780927 (diff) | |
parent | f08ac7b887e0bae0cb67362eec90a575faec07f1 (diff) |
Merge pull request #4 from defencedigital/changes
Changes
Diffstat (limited to 'internal/models/engagement.go')
-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 +} |