remove validate step as the duplication, and user model.User instead of new struct.

This commit is contained in:
wy65701436 2016-04-05 20:26:24 -07:00
parent 5139952cb4
commit b76acb6381
2 changed files with 8 additions and 77 deletions

View File

@ -16,7 +16,6 @@
package api
import (
"fmt"
"net/http"
"strconv"
@ -32,19 +31,6 @@ type UserAPI struct {
userID int
}
type userReq struct {
UserName string `json:"username"`
Password string `json:"password"`
Realname string `json:"realname"`
Email string `json:"email"`
Comment string `json:"comment"`
}
const userNameMaxLen int = 20
const passwordMaxLen int = 20
const realNameMaxLen int = 20
const commentsMaxLen int = 20
// Prepare validates the URL and parms
func (ua *UserAPI) Prepare() {
@ -137,37 +123,15 @@ func (ua *UserAPI) Put() { //currently only for toggle admin, so no request body
// Post ...
func (ua *UserAPI) Post() {
var req userReq
ua.DecodeJSONReq(&req)
user := models.User{}
ua.DecodeJSONReq(&user)
err := validateUserReq(req)
if err != nil {
log.Errorf("Invalid user request, error: %v", err)
ua.RenderError(http.StatusBadRequest, "Invalid request for creating user")
return
}
user := models.User{Username: req.UserName, Email: req.Email, Realname: req.Realname, Password: req.Password, Comment: req.Comment}
exist, err := dao.UserExists(user, "email")
if err != nil {
log.Errorf("Error occurred in UserExists: %v", err)
}
if exist {
ua.RenderError(http.StatusConflict, "")
return
}
userID, err := dao.Register(user)
_, err := dao.Register(user)
if err != nil {
log.Errorf("Error occurred in Register: %v", err)
ua.RenderError(http.StatusInternalServerError, "Internal error.")
return
}
if userID == 0 {
log.Errorf("Error happened on registing new user in db.")
ua.RenderError(http.StatusInternalServerError, "Internal error.")
}
}
// Delete ...
@ -189,36 +153,3 @@ func (ua *UserAPI) Delete() {
return
}
}
func validateUserReq(req userReq) error {
userName := req.UserName
if len(userName) == 0 {
return fmt.Errorf("User name can not be empty")
}
if len(userName) > userNameMaxLen {
return fmt.Errorf("User name is too long")
}
password := req.Password
if len(password) == 0 {
return fmt.Errorf("Password can not be empty")
}
if len(password) >= passwordMaxLen {
return fmt.Errorf("Password can is too long")
}
realName := req.Realname
if len(realName) == 0 {
return fmt.Errorf("Real name can not be empty")
}
if len(realName) >= realNameMaxLen {
return fmt.Errorf("Real name is too long")
}
email := req.Email
if len(email) == 0 {
return fmt.Errorf("Email can not be empty")
}
return nil
}

View File

@ -22,11 +22,11 @@ import (
// User holds the details of a user.
type User struct {
UserID int `orm:"column(user_id)" json:"UserId"`
Username string `orm:"column(username)"`
Email string `orm:"column(email)"`
Password string `orm:"column(password)"`
Realname string `orm:"column(realname)"`
Comment string `orm:"column(comment)"`
Username string `orm:"column(username)" json:"username"`
Email string `orm:"column(email)" json:"email"`
Password string `orm:"column(password)" json:"password"`
Realname string `orm:"column(realname)" json:"realname"`
Comment string `orm:"column(comment)" json:"comment"`
Deleted int `orm:"column(deleted)"`
Rolename string
RoleID int `json:"RoleId"`