-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguide.go
169 lines (146 loc) · 3.92 KB
/
guide.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package guide
import (
_ "embed"
"errors"
"strconv"
)
func WithValidStringCoordinates(latitude, longitude string) guideOption {
return func(g *guide) error {
coordinate, err := parseCoordinates(latitude, longitude)
if err != nil {
return err
}
g.Coordinate = coordinate
return nil
}
}
func WithDescription(description string) guideOption {
return func(g *guide) error {
g.Description = description
return nil
}
}
func PoiWithValidStringCoordinates(latitude, longitude string) poiOption {
return func(poi *pointOfInterest) error {
coordinate, err := parseCoordinates(latitude, longitude)
if err != nil {
return err
}
poi.Coordinate = coordinate
return nil
}
}
func PoiWithDescription(description string) poiOption {
return func(poi *pointOfInterest) error {
poi.Description = description
return nil
}
}
type guide struct {
Id int64
Name string
Description string
Coordinate coordinate
Pois []pointOfInterest
// guide.mapArea/coordinates}
}
type coordinate struct {
Latitude, Longitude float64
}
func newCoordinate(latitude, longitude float64) (coordinate, error) {
if latitude < -90 || latitude > 90 {
return coordinate{}, errors.New("latitude has to be in the -90°, 90° range")
}
if longitude < -180 || longitude > 180 {
return coordinate{}, errors.New("longitude has to be in the -90°, 90° range")
}
return coordinate{Latitude: latitude, Longitude: longitude}, nil
}
func parseCoordinates(latitude, longitude string) (coordinate, error) {
if latitude == "" {
return coordinate{}, errors.New("latitude cannot be empty")
}
if longitude == "" {
return coordinate{}, errors.New("longitude cannot be empty")
}
lat, err := strconv.ParseFloat(latitude, 64)
if err != nil {
return coordinate{}, errors.New("latitude has to be a number")
}
lon, err := strconv.ParseFloat(longitude, 64)
if err != nil {
return coordinate{}, errors.New("longitude has to be a number")
}
coord, err := newCoordinate(lat, lon)
if err != nil {
return coordinate{}, err
}
return coord, nil
}
type guideOption func(*guide) error
func NewGuide(name string, opts ...guideOption) (guide, error) {
if name == "" {
return guide{}, errors.New("guide name cannot be empty")
}
g := guide{
Name: name,
Pois: []pointOfInterest{},
}
for _, opt := range opts {
err := opt(&g)
if err != nil {
return guide{}, err
}
}
return g, nil
}
// pointOfInterest represents a geo location in a map. Hence,
// the relationship is one-to-many, guide to points of Interests
// there is no guarantee that a poi is bounded within a maps coordinates. See IsBounded()
type pointOfInterest struct {
Id int64
GuideID int64
Coordinate coordinate
Name string
Description string
}
// IsBounded determines if a pointOfInterest is bounded within guide.mapArea/coordinates
func (p pointOfInterest) IsBounded() bool {
return false
}
type poiOption func(*pointOfInterest) error
func NewPointOfInterest(name string, guideID int64, opts ...poiOption) (pointOfInterest, error) {
if name == "" {
return pointOfInterest{}, errors.New("poi name cannot be empty")
}
if guideID <= 0 {
return pointOfInterest{}, errors.New("guide ID cannot be empty")
}
poi := pointOfInterest{
Name: name,
GuideID: guideID,
}
for _, opt := range opts {
err := opt(&poi)
if err != nil {
return pointOfInterest{}, err
}
}
return poi, nil
}
type guideForm struct {
GuideId int64
Name, Description, Latitude, Longitude string
Errors []string
}
type poiForm struct {
PoiID int64
GuideID int64
GuideName string
Name, Description, Latitude, Longitude string
Errors []string
}
type userForm struct {
Username, Password, ConfirmPassword, Email string
Errors []string
}