From 9681b09b052fad6140a7bb81181e756b15fcd756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Tue, 16 May 2023 02:56:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B0=A1=E5=8C=96=E5=82=B3=E5=8F=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/User.go | 1 + models/account.go | 2 ++ routers/users.go | 73 ++++++++++++++++------------------------------- utils/params.go | 15 ++++++++++ 4 files changed, 42 insertions(+), 49 deletions(-) diff --git a/models/User.go b/models/User.go index d4cf60f..8620e35 100644 --- a/models/User.go +++ b/models/User.go @@ -13,6 +13,7 @@ type User struct { Email string `json:"email"` Password string `json:"-"` Slat string `json:"-"` + Admin bool `json:"admin"` CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"` UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"` } diff --git a/models/account.go b/models/account.go index 2cdce82..86a2e05 100644 --- a/models/account.go +++ b/models/account.go @@ -10,6 +10,7 @@ type Account struct { ID int `json:"id"` Name string `json:"name"` Email string `json:"email"` + Admin bool `json:"admin"` SessionID string `json:"session_id"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` @@ -47,6 +48,7 @@ func AccountRead(w http.ResponseWriter, r *http.Request, cb func(account *Accoun account.ID = user.ID account.Name = user.Name account.Email = user.Email + account.Admin = user.Admin account.SessionID = session.ID account.CreatedAt = user.CreatedAt account.UpdatedAt = user.UpdatedAt diff --git a/routers/users.go b/routers/users.go index 477fb4f..c30ad69 100644 --- a/routers/users.go +++ b/routers/users.go @@ -2,9 +2,7 @@ package routers import ( "crypto/md5" - "encoding/json" "fmt" - "io/ioutil" "main/configs" "main/models" "main/utils" @@ -32,42 +30,26 @@ func UsersGet(w http.ResponseWriter, r *http.Request) { // 創建用戶 func UsersPost(w http.ResponseWriter, r *http.Request) { - var form struct { - Name string `json:"name"` - Email string `json:"email"` - Password string `json:"password"` - } - body, err := ioutil.ReadAll(r.Body) - if err != nil { - fmt.Println(err) + var form map[string]interface{} = utils.BodyRead(r) + if form["name"] == nil || form["email"] == nil || form["password"] == nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("400 - name, email, password cannot be empty")) return } - defer r.Body.Close() - if err = json.Unmarshal(body, &form); err != nil { - fmt.Println(err) - return - } - - if form.Name == "" || form.Email == "" || form.Password == "" { - fmt.Println("name, email, password cannot be empty") - return - } - // 創建用戶 var slat string = uuid.New().String() var user models.User = models.User{ - Name: form.Name, - Email: form.Email, - Password: fmt.Sprintf("%x", md5.Sum([]byte(form.Password+slat))), + Name: form["name"].(string), + Email: form["email"].(string), + Password: fmt.Sprintf("%x", md5.Sum([]byte(form["password"].(string)+slat))), Slat: slat, } - // 寫入數據庫 if err := configs.ORMDB().Create(&user).Error; err != nil { - fmt.Println(err) + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("400 - " + err.Error())) return } - // 返回信息 w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(utils.ToJSON(user)) @@ -76,39 +58,32 @@ func UsersPost(w http.ResponseWriter, r *http.Request) { // 獲取用戶 func UsersItemGet(w http.ResponseWriter, r *http.Request) { user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)} - configs.ORMDB().First(&user) + if err := configs.ORMDB().First(&user).Error; err != nil { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 - " + err.Error())) + return + } w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(utils.ToJSON(user)) } // 更新用戶 func UsersItemPatch(w http.ResponseWriter, r *http.Request) { - var form struct { - Name string `json:"name"` - Email string `json:"email"` - Password string `json:"password"` - } - body, err := ioutil.ReadAll(r.Body) - if err != nil { - fmt.Println(err) - return - } - defer r.Body.Close() - if err = json.Unmarshal(body, &form); err != nil { - fmt.Println(err) - return - } + form := utils.BodyRead(r) user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)} configs.ORMDB().First(&user) - if form.Name != "" { - user.Name = form.Name + if name, ok := form["name"]; ok { + user.Name = name.(string) } - if form.Email != "" { - user.Email = form.Email + if email, ok := form["email"]; ok { + user.Email = email.(string) } - if form.Password != "" { + if password, ok := form["password"]; ok { user.Slat = uuid.New().String() - user.Password = fmt.Sprintf("%x", md5.Sum([]byte(form.Password+user.Slat))) + user.Password = fmt.Sprintf("%x", md5.Sum([]byte(password.(string)+user.Slat))) + } + if admin, ok := form["admin"]; ok { + user.Admin = admin.(bool) } configs.ORMDB().Save(&user) w.Header().Set("Content-Type", "application/json; charset=utf-8") diff --git a/utils/params.go b/utils/params.go index bf034bb..7876e7c 100644 --- a/utils/params.go +++ b/utils/params.go @@ -3,6 +3,7 @@ package utils import ( "encoding/json" "fmt" + "io/ioutil" "log" "math/rand" "net/http" @@ -10,6 +11,20 @@ import ( "time" ) +func BodyRead(r *http.Request) (form map[string]interface{}) { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + log.Println(err) + return + } + defer r.Body.Close() + if err = json.Unmarshal(body, &form); err != nil { + log.Println(err) + return + } + return +} + // 獲取查詢參數(int 類型) func ParamInt(value string, defaultValue int) int { if value == "" {