code on mac
This commit is contained in:
1 parent
0885214afc
commit
ce0508294c
4 files changed
+51
-119
No files matched your search
+18
-34
@@ -2,50 +2,34 @@ package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"errors"
|
||||
"github.com/gorilla/mux"
|
||||
"linhdevtran99/rest-api/models"
|
||||
"linhdevtran99/rest-api/rest-api/services"
|
||||
"linhdevtran99/rest-api/utils"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type AuthRouter struct{}
|
||||
|
||||
func WriteJSON(w http.ResponseWriter, status int, v any) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
return json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
type ApiError struct {
|
||||
Error string
|
||||
}
|
||||
|
||||
type apiFunc func(http.ResponseWriter, *http.Request) error
|
||||
|
||||
func MakeHTTPHandlerFn(fn apiFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := fn(w, r); err != nil {
|
||||
if err := WriteJSON(w, http.StatusInternalServerError, ApiError{Error: err.Error()}); err != nil {
|
||||
fmt.Print(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterNewAccount(w http.ResponseWriter, r *http.Request) error {
|
||||
if r.Method == http.MethodGet {
|
||||
fmt.Println("API Route Healthy")
|
||||
}
|
||||
|
||||
client := utils.MongoDB
|
||||
if r.Method == http.MethodPost {
|
||||
fmt.Println("method Post")
|
||||
}
|
||||
var registerInfo models.CreateUser
|
||||
|
||||
_ = json.NewDecoder(r.Body).Decode(®isterInfo)
|
||||
//call function check info user type in
|
||||
validRegisterInfo := services.CheckAndValidRegisterFiled(®isterInfo, w)
|
||||
//call function check data user use in past or not
|
||||
isValidData := services.CheckAccountExist(client, registerInfo.Username, registerInfo.Email, w)
|
||||
|
||||
if isValidData == false || validRegisterInfo == false {
|
||||
return errors.New("USER DON'T HAVE VALID INFO FOR REGISTER ACCOUNT")
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AuthRouterSetup(router *mux.Router) {
|
||||
authRouter := router.PathPrefix("/account").Subrouter()
|
||||
authRouter.Handle("/register", MakeHTTPHandlerFn(RegisterNewAccount)).Methods("GET")
|
||||
linh := authRouter.Handle("/register", MakeHTTPHandlerFn(RegisterNewAccount)).GetError()
|
||||
fmt.Println(linh)
|
||||
authRouter.Handle("/register", utils.MakeHTTPHandlerFn(RegisterNewAccount)).Methods("POST")
|
||||
}
|
||||
Reference in new issue
Block a user