-
Notifications
You must be signed in to change notification settings - Fork 28
/
list.go
51 lines (41 loc) · 1.13 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
// SPDX-License-Identifier: Apache-2.0
package schedule
import (
"context"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
)
// ListSchedules gets a list of all schedules from the database.
func (e *engine) ListSchedules(ctx context.Context) ([]*library.Schedule, error) {
e.logger.Trace("listing all schedules from the database")
// variables to store query results and return value
count := int64(0)
s := new([]database.Schedule)
schedules := []*library.Schedule{}
// count the results
count, err := e.CountSchedules(ctx)
if err != nil {
return nil, err
}
// short-circuit if there are no results
if count == 0 {
return schedules, nil
}
// send query to the database and store result in variable
err = e.client.
Table(constants.TableSchedule).
Find(&s).
Error
if err != nil {
return nil, err
}
// iterate through all query results
for _, schedule := range *s {
// https://golang.org/doc/faq#closures_and_goroutines
tmp := schedule
// convert query result to API type
schedules = append(schedules, tmp.ToLibrary())
}
return schedules, nil
}