簡化傳參
This commit is contained in:
		@@ -13,6 +13,7 @@ type User struct {
 | 
				
			|||||||
	Email     string    `json:"email"`
 | 
						Email     string    `json:"email"`
 | 
				
			||||||
	Password  string    `json:"-"`
 | 
						Password  string    `json:"-"`
 | 
				
			||||||
	Slat      string    `json:"-"`
 | 
						Slat      string    `json:"-"`
 | 
				
			||||||
 | 
						Admin     bool      `json:"admin"`
 | 
				
			||||||
	CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
 | 
						CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
 | 
				
			||||||
	UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
 | 
						UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,6 +10,7 @@ type Account struct {
 | 
				
			|||||||
	ID        int       `json:"id"`
 | 
						ID        int       `json:"id"`
 | 
				
			||||||
	Name      string    `json:"name"`
 | 
						Name      string    `json:"name"`
 | 
				
			||||||
	Email     string    `json:"email"`
 | 
						Email     string    `json:"email"`
 | 
				
			||||||
 | 
						Admin     bool      `json:"admin"`
 | 
				
			||||||
	SessionID string    `json:"session_id"`
 | 
						SessionID string    `json:"session_id"`
 | 
				
			||||||
	CreatedAt time.Time `json:"created_at"`
 | 
						CreatedAt time.Time `json:"created_at"`
 | 
				
			||||||
	UpdatedAt time.Time `json:"updated_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.ID = user.ID
 | 
				
			||||||
	account.Name = user.Name
 | 
						account.Name = user.Name
 | 
				
			||||||
	account.Email = user.Email
 | 
						account.Email = user.Email
 | 
				
			||||||
 | 
						account.Admin = user.Admin
 | 
				
			||||||
	account.SessionID = session.ID
 | 
						account.SessionID = session.ID
 | 
				
			||||||
	account.CreatedAt = user.CreatedAt
 | 
						account.CreatedAt = user.CreatedAt
 | 
				
			||||||
	account.UpdatedAt = user.UpdatedAt
 | 
						account.UpdatedAt = user.UpdatedAt
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,9 +2,7 @@ package routers
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"crypto/md5"
 | 
						"crypto/md5"
 | 
				
			||||||
	"encoding/json"
 | 
					 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io/ioutil"
 | 
					 | 
				
			||||||
	"main/configs"
 | 
						"main/configs"
 | 
				
			||||||
	"main/models"
 | 
						"main/models"
 | 
				
			||||||
	"main/utils"
 | 
						"main/utils"
 | 
				
			||||||
@@ -32,42 +30,26 @@ func UsersGet(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// 創建用戶
 | 
					// 創建用戶
 | 
				
			||||||
func UsersPost(w http.ResponseWriter, r *http.Request) {
 | 
					func UsersPost(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	var form struct {
 | 
						var form map[string]interface{} = utils.BodyRead(r)
 | 
				
			||||||
		Name     string `json:"name"`
 | 
						if form["name"] == nil || form["email"] == nil || form["password"] == nil {
 | 
				
			||||||
		Email    string `json:"email"`
 | 
							w.WriteHeader(http.StatusBadRequest)
 | 
				
			||||||
		Password string `json:"password"`
 | 
							w.Write([]byte("400 - name, email, password cannot be empty"))
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	body, err := ioutil.ReadAll(r.Body)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		fmt.Println(err)
 | 
					 | 
				
			||||||
		return
 | 
							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 slat string = uuid.New().String()
 | 
				
			||||||
	var user models.User = models.User{
 | 
						var user models.User = models.User{
 | 
				
			||||||
		Name:     form.Name,
 | 
							Name:     form["name"].(string),
 | 
				
			||||||
		Email:    form.Email,
 | 
							Email:    form["email"].(string),
 | 
				
			||||||
		Password: fmt.Sprintf("%x", md5.Sum([]byte(form.Password+slat))),
 | 
							Password: fmt.Sprintf("%x", md5.Sum([]byte(form["password"].(string)+slat))),
 | 
				
			||||||
		Slat:     slat,
 | 
							Slat:     slat,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 寫入數據庫
 | 
						// 寫入數據庫
 | 
				
			||||||
	if err := configs.ORMDB().Create(&user).Error; err != nil {
 | 
						if err := configs.ORMDB().Create(&user).Error; err != nil {
 | 
				
			||||||
		fmt.Println(err)
 | 
							w.WriteHeader(http.StatusBadRequest)
 | 
				
			||||||
 | 
							w.Write([]byte("400 - " + err.Error()))
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 返回信息
 | 
						// 返回信息
 | 
				
			||||||
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
 | 
						w.Header().Set("Content-Type", "application/json; charset=utf-8")
 | 
				
			||||||
	w.Write(utils.ToJSON(user))
 | 
						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) {
 | 
					func UsersItemGet(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
 | 
						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.Header().Set("Content-Type", "application/json; charset=utf-8")
 | 
				
			||||||
	w.Write(utils.ToJSON(user))
 | 
						w.Write(utils.ToJSON(user))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 更新用戶
 | 
					// 更新用戶
 | 
				
			||||||
func UsersItemPatch(w http.ResponseWriter, r *http.Request) {
 | 
					func UsersItemPatch(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	var form struct {
 | 
						form := utils.BodyRead(r)
 | 
				
			||||||
		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
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
 | 
						user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
 | 
				
			||||||
	configs.ORMDB().First(&user)
 | 
						configs.ORMDB().First(&user)
 | 
				
			||||||
	if form.Name != "" {
 | 
						if name, ok := form["name"]; ok {
 | 
				
			||||||
		user.Name = form.Name
 | 
							user.Name = name.(string)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if form.Email != "" {
 | 
						if email, ok := form["email"]; ok {
 | 
				
			||||||
		user.Email = form.Email
 | 
							user.Email = email.(string)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if form.Password != "" {
 | 
						if password, ok := form["password"]; ok {
 | 
				
			||||||
		user.Slat = uuid.New().String()
 | 
							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)
 | 
						configs.ORMDB().Save(&user)
 | 
				
			||||||
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
 | 
						w.Header().Set("Content-Type", "application/json; charset=utf-8")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,7 @@ package utils
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io/ioutil"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"math/rand"
 | 
						"math/rand"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
@@ -10,6 +11,20 @@ import (
 | 
				
			|||||||
	"time"
 | 
						"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 類型)
 | 
					// 獲取查詢參數(int 類型)
 | 
				
			||||||
func ParamInt(value string, defaultValue int) int {
 | 
					func ParamInt(value string, defaultValue int) int {
 | 
				
			||||||
	if value == "" {
 | 
						if value == "" {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user