blob: e6c82430f6d73d20ebbb2e0c7bad637fb757e052 (
plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
package content
import (
"fmt"
"net/http"
uuid "github.com/satori/go.uuid"
)
// Sluggable makes a struct locatable by URL with it's own path
// As an Item implementing Sluggable, slugs may overlap. If this is an issue,
// make your content struct (or one which imbeds Item) implement Sluggable
// and it will override the slug created by Item's SetSlug with your struct's
type Sluggable interface {
SetSlug(string)
}
// Identifiable enables a struct to have its ID set/get. Typically this is done
// to set an ID to -1 indicating it is new for DB inserts, since by default
// a newly initialized struct would have an ID of 0, the int zero-value, and
// BoltDB's starting key per bucket is 0, thus overwriting the first record.
type Identifiable interface {
ItemID() int
SetItemID(int)
UniqueID() uuid.UUID
String() string
}
// Hookable provides our user with an easy way to intercept or add functionality
// to the different lifecycles/events a struct may encounter. Item implements
// Hookable with no-ops so our user can override only whichever ones necessary.
type Hookable interface {
BeforeSave(req *http.Request) error
AfterSave(req *http.Request) error
BeforeDelete(req *http.Request) error
AfterDelete(req *http.Request) error
BeforeApprove(req *http.Request) error
AfterApprove(req *http.Request) error
BeforeReject(req *http.Request) error
AfterReject(req *http.Request) error
}
// Item should only be embedded into content type structs.
type Item struct {
UUID uuid.UUID `json:"uuid"`
ID int `json:"id"`
Slug string `json:"slug"`
Timestamp int64 `json:"timestamp"`
Updated int64 `json:"updated"`
}
// Time partially implements the Sortable interface
func (i Item) Time() int64 {
return i.Timestamp
}
// Touch partially implements the Sortable interface
func (i Item) Touch() int64 {
return i.Updated
}
// SetSlug sets the item's slug for its URL
func (i *Item) SetSlug(slug string) {
i.Slug = slug
}
// ItemID gets the Item's ID field
// partially implements the Identifiable interface
func (i Item) ItemID() int {
return i.ID
}
// SetItemID sets the Item's ID field
// partially implements the Identifiable interface
func (i *Item) SetItemID(id int) {
i.ID = id
}
// UniqueID gets the Item's UUID field
// partially implements the Identifiable interface
func (i Item) UniqueID() uuid.UUID {
return i.UUID
}
// String formats an Item into a printable value
// partially implements the Identifiable interface
func (i Item) String() string {
return fmt.Sprintf("Item ID: %s", i.UniqueID())
}
// BeforeSave is a no-op to ensure structs which embed Item implement Hookable
func (i Item) BeforeSave(req *http.Request) error {
return nil
}
// AfterSave is a no-op to ensure structs which embed Item implement Hookable
func (i Item) AfterSave(req *http.Request) error {
return nil
}
// BeforeDelete is a no-op to ensure structs which embed Item implement Hookable
func (i Item) BeforeDelete(req *http.Request) error {
return nil
}
// AfterDelete is a no-op to ensure structs which embed Item implement Hookable
func (i Item) AfterDelete(req *http.Request) error {
return nil
}
// BeforeApprove is a no-op to ensure structs which embed Item implement Hookable
func (i Item) BeforeApprove(req *http.Request) error {
return nil
}
// AfterApprove is a no-op to ensure structs which embed Item implement Hookable
func (i Item) AfterApprove(req *http.Request) error {
return nil
}
// BeforeReject is a no-op to ensure structs which embed Item implement Hookable
func (i Item) BeforeReject(req *http.Request) error {
return nil
}
// AfterReject is a no-op to ensure structs which embed Item implement Hookable
func (i Item) AfterReject(req *http.Request) error {
return nil
}
|