-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
58 lines (49 loc) · 1.5 KB
/
list.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
54
55
56
57
58
package deployments
import (
"net/http"
"time"
"github.com/frohwerk/deputy-backend/internal/request"
"github.com/frohwerk/deputy-backend/pkg/httputil"
)
type deployment struct {
Name string `json:"name,omitempty"`
Image string `json:"image,omitempty"`
Updated *time.Time `json:"updated,omitempty"`
}
func (h *handler) List(rw http.ResponseWriter, r *http.Request) {
appId, _ := request.StringParam(r.URL.Query(), "app")
envId, _ := request.StringParam(r.URL.Query(), "env")
result, err := h.list(appId, envId)
if err != nil {
httputil.WriteErrorResponse(rw, err)
} else {
httputil.WriteJsonResponse(rw, result)
}
}
func (h *handler) list(appId, envId string) ([]deployment, error) {
rows, err := h.db.Query(`
SELECT d.component_id IS NOT NULL AS DEPLOYED,
c.name,
COALESCE(d.image_ref, ''), COALESCE(d.updated, TIMESTAMP '0001-01-01 00:00:00+00' AT TIME ZONE 'UTC')
FROM apps_components ac
CROSS JOIN platforms p
INNER JOIN components c ON c.id = ac.component_id
LEFT JOIN deployments d ON d.component_id = c.id AND d.platform_id = p.id
WHERE ac.app_id = $1 AND p.env_id = $2
`, appId, envId)
if err != nil {
return nil, err
}
var deployed = false
result := make([]deployment, 0)
for i := 0; rows.Next(); i++ {
result = append(result, deployment{})
if err := rows.Scan(&deployed, &result[i].Name, &result[i].Image, &result[i].Updated); err != nil {
return nil, err
}
if !deployed {
result[i].Updated = nil
}
}
return result, nil
}