2021-12-08 18:43:14 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-12-09 17:02:57 +03:00
|
|
|
"memos/api/e"
|
2021-12-08 18:43:14 +03:00
|
|
|
"memos/store"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleGetMyUserInfo(w http.ResponseWriter, r *http.Request) {
|
2021-12-10 08:41:17 +03:00
|
|
|
userId, _ := GetUserIdInSession(r)
|
2021-12-08 18:43:14 +03:00
|
|
|
|
|
|
|
user, err := store.GetUserById(userId)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-12-09 17:02:57 +03:00
|
|
|
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
|
2021-12-08 18:43:14 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-09 17:02:57 +03:00
|
|
|
json.NewEncoder(w).Encode(Response{
|
|
|
|
Succeed: true,
|
|
|
|
Message: "",
|
|
|
|
Data: user,
|
|
|
|
})
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleUpdateMyUserInfo(w http.ResponseWriter, r *http.Request) {
|
2021-12-10 08:41:17 +03:00
|
|
|
userId, _ := GetUserIdInSession(r)
|
2021-12-08 18:43:14 +03:00
|
|
|
|
2021-12-09 17:02:57 +03:00
|
|
|
userPatch := store.UserPatch{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&userPatch)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Bad request")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-12 10:21:17 +03:00
|
|
|
if userPatch.Username != nil {
|
2021-12-12 09:00:25 +03:00
|
|
|
usernameUsable, _ := store.CheckUsernameUsable(*userPatch.Username)
|
|
|
|
if !usernameUsable {
|
|
|
|
json.NewEncoder(w).Encode(Response{
|
|
|
|
Succeed: false,
|
|
|
|
Message: "Username is existed",
|
|
|
|
Data: nil,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 10:21:17 +03:00
|
|
|
if userPatch.GithubName != nil {
|
2021-12-12 09:00:25 +03:00
|
|
|
githubNameUsable, _ := store.CheckGithubNameUsable(*userPatch.GithubName)
|
|
|
|
if !githubNameUsable {
|
|
|
|
json.NewEncoder(w).Encode(Response{
|
|
|
|
Succeed: false,
|
|
|
|
Message: "GitHub name is existed",
|
|
|
|
Data: nil,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 17:02:57 +03:00
|
|
|
user, err := store.UpdateUser(userId, &userPatch)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(Response{
|
|
|
|
Succeed: true,
|
|
|
|
Message: "",
|
|
|
|
Data: user,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type CheckUsername struct {
|
|
|
|
Username string
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleCheckUsername(w http.ResponseWriter, r *http.Request) {
|
|
|
|
checkUsername := CheckUsername{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&checkUsername)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Bad request")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
usable, err := store.CheckUsernameUsable(checkUsername.Username)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(Response{
|
|
|
|
Succeed: true,
|
|
|
|
Message: "",
|
|
|
|
Data: usable,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type ValidPassword struct {
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleValidPassword(w http.ResponseWriter, r *http.Request) {
|
2021-12-10 08:41:17 +03:00
|
|
|
userId, _ := GetUserIdInSession(r)
|
2021-12-09 17:02:57 +03:00
|
|
|
validPassword := ValidPassword{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&validPassword)
|
2021-12-08 18:43:14 +03:00
|
|
|
|
|
|
|
if err != nil {
|
2021-12-09 17:02:57 +03:00
|
|
|
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Bad request")
|
2021-12-08 18:43:14 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-09 17:02:57 +03:00
|
|
|
valid, err := store.CheckPasswordValid(userId, validPassword.Password)
|
2021-12-08 18:43:14 +03:00
|
|
|
|
|
|
|
if err != nil {
|
2021-12-09 17:02:57 +03:00
|
|
|
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
|
2021-12-08 18:43:14 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-09 17:02:57 +03:00
|
|
|
json.NewEncoder(w).Encode(Response{
|
|
|
|
Succeed: true,
|
|
|
|
Message: "",
|
|
|
|
Data: valid,
|
|
|
|
})
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterUserRoutes(r *mux.Router) {
|
|
|
|
userRouter := r.PathPrefix("/api/user").Subrouter()
|
|
|
|
|
2021-12-12 16:49:46 +03:00
|
|
|
userRouter.Use(JSONResponseMiddleWare)
|
2021-12-08 18:43:14 +03:00
|
|
|
userRouter.Use(AuthCheckerMiddleWare)
|
|
|
|
|
|
|
|
userRouter.HandleFunc("/me", handleGetMyUserInfo).Methods("GET")
|
|
|
|
userRouter.HandleFunc("/me", handleUpdateMyUserInfo).Methods("PATCH")
|
2021-12-09 17:02:57 +03:00
|
|
|
userRouter.HandleFunc("/checkusername", handleCheckUsername).Methods("POST")
|
|
|
|
userRouter.HandleFunc("/validpassword", handleValidPassword).Methods("POST")
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|