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
|
package models
import (
"database/sql"
"time"
)
type Operation struct {
ID int
Name string
Description string
Created time.Time
OrganisationName string
}
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
}
|