/
types.go
45 lines (35 loc) · 1 KB
/
types.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
package db
import (
"fmt"
"sort"
"time"
"github.com/uptrace/bun"
)
// Fruit model to hold the Fruit data
type Fruit struct {
bun.BaseModel `bun:"table:fruits,alias:f"`
ID int `bun:",pk,autoincrement,nullzero" json:"id"`
Name string `bun:",notnull" json:"name" `
Season string `bun:",notnull" json:"season"`
Emoji string `bun:"," json:"emoji,omitempty"`
CreatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"-"`
ModifiedAt time.Time `json:"-"`
}
// Fruits represents a collection of Fruits
type Fruits []*Fruit
var _ sort.Interface = (Fruits)(nil)
// Len implements sort.Interface
func (f Fruits) Len() int {
return len(f)
}
// Less implements sort.Interface
func (f Fruits) Less(i int, j int) bool {
return f[i].Name < f[j].Name
}
// Swap implements sort.Interface
func (f Fruits) Swap(i int, j int) {
f[i], f[j] = f[j], f[i]
}
func (f *Fruit) String() string {
return fmt.Sprintf("ID: %d, Name: %s, Season: %s", f.ID, f.Name, f.Season)
}