diff options
Diffstat (limited to 'internal')
-rw-r--r-- | internal/models/engagement.go | 54 | ||||
-rw-r--r-- | internal/models/errors.go | 7 | ||||
-rw-r--r-- | internal/models/operation.go | 52 | ||||
-rw-r--r-- | internal/models/organisations.go | 85 | ||||
-rw-r--r-- | internal/models/person.go | 51 |
5 files changed, 249 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 +} diff --git a/internal/models/errors.go b/internal/models/errors.go new file mode 100644 index 0000000..a70c7dc --- /dev/null +++ b/internal/models/errors.go @@ -0,0 +1,7 @@ +package models + +import ( + "errors" +) + +var ErrNoRecord = errors.New("models: no matching record found") diff --git a/internal/models/operation.go b/internal/models/operation.go new file mode 100644 index 0000000..ecfd31b --- /dev/null +++ b/internal/models/operation.go @@ -0,0 +1,52 @@ +package models + +import ( + "database/sql" + "time" +) + +type Operation struct { + ID int + Name string + Description string + Created time.Time + OrganisationName string + EngagementStrategies []EngagementStrategy +} + +type OperationModel struct { + DB *sql.DB +} + +func (m *OperationModel) ListAll() ([]Operation, error) { + + stmt := `SELECT op.id, op.name, op.description, op.created, org.name + FROM operations op + INNER JOIN organisations org ON op.organisation_id=org.id` + + // stmt := `SELECT * FROM operations` + + rows, err := m.DB.Query(stmt) + if err != nil { + return nil, err + } + + defer rows.Close() + + var ops []Operation + + for rows.Next() { + var o Operation + err = rows.Scan(&o.ID, &o.Name, &o.Description, &o.Created, &o.OrganisationName) + if err != nil { + return nil, err + } + + ops = append(ops, o) + } + + if err = rows.Err(); err != nil { + return nil, err + } + return ops, err +} diff --git a/internal/models/organisations.go b/internal/models/organisations.go new file mode 100644 index 0000000..88f7bc9 --- /dev/null +++ b/internal/models/organisations.go @@ -0,0 +1,85 @@ +package models + +import ( + "database/sql" + "errors" + "time" +) + +type Organisation struct { + ID int + Name string + Created time.Time +} + +type OrganisationModel struct { + DB *sql.DB +} + +func (m *OrganisationModel) Insert(name string) (int, error) { + stmt := `INSERT INTO organisations (name, created) + VALUEs (?, UTC_TIMESTAMP())` + + result, err := m.DB.Exec(stmt, name) + if err != nil { + return 0, err + } + + id, err := result.LastInsertId() + if err != nil { + return 0, err + } + return int(id), nil +} + +func (m *OrganisationModel) Get(id int) (Organisation, error) { + stmt := `SELECT id, name, created FROM organisations + WHERE id = ?` + + row := m.DB.QueryRow(stmt, id) + + var o Organisation + + err := row.Scan(&o.ID, &o.Name, &o.Created) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return Organisation{}, ErrNoRecord + } else { + return Organisation{}, err + } + } + return o, nil +} + +// Ten most recent... +func (m *OrganisationModel) Latest() ([]Organisation, error) { + // Pick out the last 10 + stmt := `SELECT id, name, created FROM organisations + ORDER BY id DESC LIMIT 10` + + rows, err := m.DB.Query(stmt) + if err != nil { + return nil, err + } + + defer rows.Close() + + var organisations []Organisation + + for rows.Next() { + var o Organisation + + err = rows.Scan(&o.ID, &o.Name, &o.Created) + if err != nil { + return nil, err + } + + organisations = append(organisations, o) + } + + if err = rows.Err(); err != nil { + return nil, err + } + + return organisations, err +} diff --git a/internal/models/person.go b/internal/models/person.go new file mode 100644 index 0000000..d2b8585 --- /dev/null +++ b/internal/models/person.go @@ -0,0 +1,51 @@ +package models + +import ( + "database/sql" + "time" +) + +type Person struct { + ID string + FirstName string + LastName string + OrganisationName string + OrganisationID int + RoleName string + Created time.Time +} + +type PersonModel struct { + DB *sql.DB +} + +func (m *PersonModel) ListAll() ([]Person, error) { + stmt := `SELECT p.id, p.first_name, p.last_name, p.role_name, org.name, org.id + FROM persons p + INNER JOIN organisations org ON p.organisation_id=org.id` + + rows, err := m.DB.Query(stmt) + if err != nil { + return nil, err + } + + defer rows.Close() + + var ps []Person + + for rows.Next() { + var p Person + err = rows.Scan(&p.ID, &p.FirstName, &p.LastName, &p.RoleName, &p.OrganisationName, &p.OrganisationID) + if err != nil { + return nil, err + } + + ps = append(ps, p) + } + + if err = rows.Err(); err != nil { + return nil, err + } + + return ps, err +} |