-
Notifications
You must be signed in to change notification settings - Fork 0
/
planet.go
152 lines (118 loc) · 3.07 KB
/
planet.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package planet
import (
"github.com/gin-gonic/gin"
"github.com/kamva/mgm/v3"
"time"
)
type HttpHandler interface {
Handle(c *gin.Context)
}
type KafkaWriter interface {
Write(topic string, eventType EventType, msg []byte) error
}
type KafkaRead interface {
Read()
}
type EventProcessor interface {
Process(data interface{}) error
}
type Writer interface {
Write(createPlanet CreatePlanetCommand) (PlanetDocument, error)
}
type Deleter interface {
Delete(id ID) error
}
type Reader interface {
ReadByPlanetId(id ID) (PlanetDocument, error)
ReadAll(filter PageFilterRequest) ([]PlanetDocument, error)
Count() (int, error)
}
type Updater interface {
Update(planet PlanetDocument) error
}
type ReadUpdater interface {
Reader
Updater
}
type Status int
const (
Processing Status = iota
Processed
Failed
)
func (p Status) String() string {
return [...]string{"PROCESSING", "PROCESSED", "FAIL"}[p]
}
type EventType int
const CreatedEvent EventType = iota
func (e EventType) String() string {
return [...]string{"CREATED"}[e]
}
type EventsProcessor map[string]EventProcessor
type CreatePlanetEvent struct {
PlanetID ID `json:"planet_id"`
RetryCount int `json:"retry_count"`
}
type CreatePlanetCommand struct {
Name string `json:"name"`
Climate string `json:"climate"`
Terrain string `json:"terrain"`
}
func (c *CreatePlanetCommand) Validate() map[string]string {
validation := make(map[string]string)
if c.Name == "" {
validation["name"] = "This field cannot be empty."
}
if c.Climate == "" {
validation["climate"] = "This field cannot be empty."
}
if c.Terrain == "" {
validation["terrain"] = "This field cannot be empty."
}
return validation
}
type StateResponse struct {
PlanetID ID `json:"planet_id"`
Status string `json:"status"`
}
type PageFilterRequest struct {
Name string `form:"name"`
Page int `form:"page"`
PerPage int `form:"per_page"`
}
func (p *PageFilterRequest) Validate() map[string]string {
validation := make(map[string]string)
if p.Page <= 0 {
validation["page"] = "the page query param cannot be zero or negative"
}
if p.PerPage <= 0 {
validation["per_page"] = "the per_page query param cannot be zero or negative"
}
return validation
}
type Metadata struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
PageCount int `json:"page_count"`
TotalCount int `json:"total_count"`
}
type Pagination struct {
Meta Metadata `json:"_metadata"`
Results interface{} `json:"results"`
}
type PlanetDocument struct {
PlanetID ID `bson:"planetID" json:"planet_id"`
Name string `bson:"name" json:"name"`
Climate []string `bson:"climate" json:"climate"`
Terrain []string `bson:"terrain" json:"terrain"`
Films Films `bson:"films" json:"films"`
Status string `bson:"status" json:"status"`
mgm.IDField `bson:" ,inline" json:"-"`
mgm.DateFields `bson:" ,inline"`
}
type ID string
type Films []Film
type Film struct {
Title string `bson:"title" json:"title"`
ReleaseDate time.Time `bson:"release_date" json:"release_date"`
}