generated from traPtitech/naro-template-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
241 lines (200 loc) · 6.55 KB
/
handler.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"database/sql"
"errors"
"fmt"
"log"
"net/http"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"golang.org/x/crypto/bcrypt"
)
type City struct {
ID int `json:"id,omitempty" db:"ID"`
Name sql.NullString `json:"name,omitempty" db:"Name"`
CountryCode sql.NullString `json:"countryCode,omitempty" db:"CountryCode"`
District sql.NullString `json:"district,omitempty" db:"District"`
Population sql.NullInt64 `json:"population,omitempty" db:"Population"`
}
func getCityInfoHandler(c echo.Context) error {
cityName := c.Param("cityName")
fmt.Println(cityName)
var city City
db.Get(&city, "SELECT * FROM city WHERE Name=?", cityName)
if !city.Name.Valid {
return c.NoContent(http.StatusNotFound)
}
return c.JSON(http.StatusOK, city)
}
type countryName struct {
Name string `json:"name" db:"Name"`
}
type cityName struct {
Name string `json:"name" db:"Name"`
}
func getCountryCityListHandler(c echo.Context) error {
countryName := c.Param("countryName")
var countryCode string
db.Get(&countryCode, "SELECT Code FROM country WHERE Name=?", countryName)
if countryCode == "" {
return c.NoContent(http.StatusNotFound)
}
var cities []cityName
var cityNumber int
db.Get(&cityNumber, "SELECT COUNT(*) FROM city WHERE countryCode=?", countryCode)
for i := 0; i < cityNumber; i++ {
var oneCity cityName
db.Get(&oneCity, "SELECT Name FROM city WHERE countryCode=? LIMIT 1 OFFSET ?", countryCode, i)
if oneCity.Name == "" {
return c.NoContent(http.StatusNotFound)
}
cities = append(cities, oneCity)
}
return c.JSON(http.StatusOK, cities)
}
func getCountryInfoAllHandler(c echo.Context) error {
var countryNumber int
db.Get(&countryNumber, "SELECT COUNT(*) FROM country")
fmt.Println(countryNumber)
if countryNumber == 0 {
return c.NoContent(http.StatusNotFound)
}
var countries []countryName
for i := 0; i < countryNumber; i++ {
var oneCountry countryName
db.Get(&oneCountry, "SELECT Name FROM country LIMIT 1 OFFSET ?", i)
if oneCountry.Name == "" {
return c.NoContent(http.StatusNotFound)
}
countries = append(countries, oneCountry)
}
return c.JSON(http.StatusOK, countries)
}
func testHandler(c echo.Context) error {
var oneCity City
db.Get(&oneCity, "SELECT * FROM city LIMIT 1")
return c.JSON(http.StatusOK, oneCity)
}
func postCityHandler(c echo.Context) error {
var city City
err := c.Bind(&city)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "bad request body")
}
result, err := db.Exec("INSERT INTO city (Name, CountryCode, District, Population) VALUES (?, ?, ?, ?)", city.Name, city.CountryCode, city.District, city.Population)
if err != nil {
log.Printf("failed to insert city data: %s\n", err)
return c.NoContent(http.StatusInternalServerError)
}
id, err := result.LastInsertId()
if err != nil {
fmt.Printf("failed to get last insert id: %s\n", err)
return c.NoContent(http.StatusInternalServerError)
}
city.ID = int(id)
return c.JSON(http.StatusCreated, city)
}
type LoginRequestBody struct {
Username string `json:"username,omitempty" form:"username"`
Password string `json:"password,omitempty" form:"password"`
}
func signUpHandler(c echo.Context) error {
// リクエストを受け取り、reqに格納する
req := LoginRequestBody{}
c.Bind(&req)
// バリデーションする(PasswordかUsernameが空文字列の場合は400 BadRequestを返す)
if req.Password == "" || req.Username == "" {
return c.String(http.StatusBadRequest, "Username or Password is empty")
}
// 登録しようとしているユーザーが既にデータベース内に存在するかチェック
var count int
err := db.Get(&count, "SELECT COUNT(*) FROM users WHERE Username=?", req.Username)
if err != nil {
log.Println(err)
return c.NoContent(http.StatusInternalServerError)
}
// 存在したら409 Conflictを返す
if count > 0 {
return c.String(http.StatusConflict, "Username is already used")
}
// パスワードをハッシュ化する
pw := req.Password + salt
hashedPass, err := bcrypt.GenerateFromPassword([]byte(pw), bcrypt.DefaultCost)
// ハッシュ化に失敗したら500 InternalServerErrorを返す
if err != nil {
log.Println(err)
return c.NoContent(http.StatusInternalServerError)
}
// ユーザーを登録する
_, err = db.Exec("INSERT INTO users (Username, HashedPass) VALUES (?, ?)", req.Username, hashedPass)
// 登録に失敗したら500 InternalServerErrorを返す
if err != nil {
log.Println(err)
return c.NoContent(http.StatusInternalServerError)
}
// 登録に成功したら201 Createdを返す
return c.NoContent(http.StatusCreated)
}
type User struct {
Username string `json:"username,omitempty" form:"Username"`
HashedPass string `json:"-" form:"HashedPass"`
}
func loginHandler(c echo.Context) error {
var req LoginRequestBody
c.Bind(&req)
if req.Password == "" || req.Username == "" {
return c.String(http.StatusBadRequest, "Username or Password is empty")
}
log.Println(req.Username)
user := User{}
username := req.Username
err := db.QueryRow("SELECT * FROM users WHERE Username=?", username).Scan(&user.Username, &user.HashedPass)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return c.NoContent(http.StatusUnauthorized)
} else {
log.Println(err)
log.Println(1)
return c.NoContent(http.StatusInternalServerError)
}
}
err = bcrypt.CompareHashAndPassword([]byte(user.HashedPass), []byte(req.Password+salt))
if err != nil {
if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
return c.NoContent(http.StatusUnauthorized)
} else {
log.Println(2)
return c.NoContent(http.StatusInternalServerError)
}
}
sess, err := session.Get("sessions", c)
if err != nil {
fmt.Println(err)
return c.String(http.StatusInternalServerError, "something wrong in getting session")
}
sess.Values["userName"] = req.Username
sess.Save(c.Request(), c.Response())
return c.NoContent(http.StatusOK)
}
func userAuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
sess, err := session.Get("sessions", c)
if err != nil {
fmt.Println(err)
return c.String(http.StatusInternalServerError, "something wrong in getting session")
}
if sess.Values["userName"] == nil {
return c.String(http.StatusUnauthorized, "please login")
}
c.Set("userName", sess.Values["userName"].(string))
return next(c)
}
}
type Me struct {
Username string `json:"username,omitempty" db:"username"`
}
func getWhoAmIHandler(c echo.Context) error {
return c.JSON(http.StatusOK, Me{
Username: c.Get("userName").(string),
})
}