summaryrefslogtreecommitdiffstats
path: root/internal/models/engagement.go
blob: 87e07868b1584e1808eed1e088805a57d1beebff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
}