-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
53 lines (41 loc) · 974 Bytes
/
model.go
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
package main
import (
"database/sql"
)
type Version struct {
ID int `json:"id"`
Number string `json:"number"`
PublishedAt string `json:"publishedAt"`
}
type Project struct {
ID int `json:"id"`
Name string `json:"name"`
Versions []Version
}
func (p *Project) getProject(db *sql.DB) error {
query := `SELECT
projects.id as p_id,
projects.identifier as name,
versions.id as v_id,
versions.name as name,
versions.published_at as published_at
FROM projects
JOIN versions ON versions.project_id = projects.id
WHERE projects.identifier = ?
ORDER BY versions.published_at`
rows, err := db.Query(query, p.Name)
if err != nil {
return err
}
defer rows.Close()
versions := []Version{}
for rows.Next() {
var v Version
if err := rows.Scan(&p.ID, &p.Name, &v.ID, &v.Number, &v.PublishedAt); err != nil {
return err
}
versions = append(versions, v)
}
p.Versions = versions
return nil
}