-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
212 lines (178 loc) · 4.35 KB
/
main.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/labstack/echo/v4"
)
type (
Customer struct {
Name string `json:"cName"`
Tel uint64 `json:"cTel"`
Address string `json:"cAddress"`
ID uint64 `json:"cID"`
RegisterDate time.Time `json:"cRegisterDate"`
}
CustomerResponse struct {
Name string `json:"cName"`
Tel uint64 `json:"cTel"`
Address string `json:"cAddress"`
ID uint64 `json:"cID"`
RegisterDate string `json:"cRegisterDate"`
Message string `json:"msg"`
}
AllCustomers struct {
Size uint64 `json:"size"`
Customers []*CustomerResponse `json:"customers"`
Message string `json:"msg"`
}
JustMSG struct {
Message string `json:"msg"`
}
ReportResponse struct {
TotalCustomers uint64 `json:"totalCustomers"`
Period int `json:"period"`
Message string `json:"msg"`
}
)
var (
db = map[uint64]*Customer{}
last uint64 = 0
)
func getCustomersSlice(db map[uint64]*Customer) []*CustomerResponse {
ans := make([]*CustomerResponse, 0, len(db))
for _, c := range db {
ans = append(ans, newCustomerResponse(c))
}
return ans
}
func newCustomerList(db map[uint64]*Customer) AllCustomers {
return AllCustomers{
Size: uint64(len(db)),
Customers: getCustomersSlice(db),
Message: "success",
}
}
func newReportResponse(n uint64) ReportResponse {
return ReportResponse{
TotalCustomers: n,
Period: 1,
Message: "success",
}
}
func createError() JustMSG {
return JustMSG{Message: "error"}
}
func timeToString(t time.Time) string {
return fmt.Sprintf("%d-%d-%d", t.Year(), t.Month(), t.Day())
}
func newCustomerResponse(c *Customer) *CustomerResponse {
return &CustomerResponse{
Name: c.Name,
Tel: c.Tel,
Address: c.Address,
ID: c.ID,
RegisterDate: timeToString(c.RegisterDate),
Message: "success",
}
}
func CreateNewCustomer(c echo.Context) error {
newUser := &Customer{
ID: last + 1,
RegisterDate: time.Now(),
}
if err := c.Bind(newUser); err != nil {
return err
}
db[newUser.ID] = newUser
last++
response := newCustomerResponse(newUser)
return c.JSON(http.StatusCreated, response)
}
func EditCustomer(c echo.Context) error {
cidI, err := strconv.Atoi(c.Param("cid"))
if err != nil {
return err
}
cid := uint64(cidI)
oldUser, ok := db[cid]
if !ok {
return c.JSON(http.StatusNotFound, createError())
}
if err := c.Bind(oldUser); err != nil {
return err
}
return c.JSON(http.StatusCreated, newCustomerResponse(oldUser))
}
func getAll(c echo.Context) error {
ans := newCustomerList(db)
if ans.Size == 0 {
return c.JSON(http.StatusNotFound, createError())
}
return c.JSON(http.StatusOK, ans)
}
func DeleteCustomer(c echo.Context) error {
cidI, err := strconv.Atoi(c.Param("cid"))
if err != nil {
return err
}
cid := uint64(cidI)
_, ok := db[cid]
if !ok {
return c.JSON(http.StatusNotFound, createError())
}
delete(db, cid)
return c.JSON(http.StatusOK, JustMSG{Message: "success"})
}
func sameMonth(m int, c *Customer) bool {
return int(c.RegisterDate.Month()) == m
}
func ReportByMonth(c echo.Context) error {
monthI, err := strconv.Atoi(c.Param("month"))
if err != nil {
return c.JSON(http.StatusBadRequest, createError())
}
var ans uint64 = 0
for _, customer := range db {
if sameMonth(monthI, customer) {
ans++
}
}
if ans == 0 {
return c.JSON(http.StatusNotFound, createError())
} else {
return c.JSON(http.StatusOK, newReportResponse(ans))
}
}
func searchCustomer(c echo.Context) error {
prefix := c.QueryParam("cName")
for _, customer := range db {
if strings.HasPrefix(customer.Name, prefix) {
return c.JSON(http.StatusFound, newCustomerResponse(customer))
}
}
return c.JSON(http.StatusNotFound, createError())
}
func GetCustomers(c echo.Context) error {
qp := c.QueryParam("cName")
ok := qp != ""
if ok {
return searchCustomer(c)
} else {
return getAll(c)
}
}
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.POST("/customers", CreateNewCustomer)
e.PUT("/customers/:cid", EditCustomer)
e.GET("/customers", GetCustomers)
e.DELETE("/customers/:cid", DeleteCustomer)
e.GET("/report/:month", ReportByMonth)
e.Logger.Fatal(e.Start(":3000"))
}