-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
40 lines (33 loc) · 918 Bytes
/
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
package main
import (
"github.com/fatih/color"
"github.com/rodaine/table"
"gorm.io/gorm"
)
func List(db *gorm.DB, priority string) {
Todos := db.Order(`
CASE Priority
WHEN 'urgent' THEN 1
WHEN 'high' THEN 2
WHEN 'medium' THEN 3
WHEN 'normal' THEN 4
WHEN 'low' THEN 5
ELSE 6
END`).Table("Todos")
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("ID", "Name", "Description", "Priority", "CreatedDate")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt).WithPadding(3)
var results []Todo
if len(priority) > 0 {
Todos.Limit(10).Where(&Todo{
Priority: priority,
}).Find(&results)
} else {
Todos.Limit(10).Find(&results)
}
for _, res := range results {
tbl.AddRow(res.ID, res.Name, res.Description, res.Priority, res.CreatedDate)
}
tbl.Print()
}