This commit is contained in:
vilet.yy 2021-04-06 18:19:45 +08:00
parent d0cb37ec3e
commit 6b1fafb258
7 changed files with 46 additions and 18 deletions

View File

@ -109,10 +109,17 @@ func UpdateVendor(c *gin.Context) {
}
if err := c.ShouldBindJSON(&vendor); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
if errs, ok := err.(validator.ValidationErrors); !ok {
c.JSON(http.StatusBadRequest, gin.H{
"msg": err.Error(),
})
return
} else {
c.JSON(http.StatusBadRequest, gin.H{
"msg": errs.Translate(utils.Trans),
})
return
}
}
if exist := basic.ExistVendorByName(vendor.Name); exist {

View File

@ -1,7 +1,7 @@
/*
* @Date: 2021-03-21 19:54:57
* @LastEditors: viletyy
* @LastEditTime: 2021-04-06 17:02:38
* @LastEditTime: 2021-04-06 18:01:36
* @FilePath: /potato/controller/api/v1/user.go
*/
package v1
@ -10,6 +10,7 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/viletyy/potato/global"
"github.com/viletyy/potato/models"
"github.com/viletyy/potato/utils"
@ -24,12 +25,12 @@ type AuthResponse struct {
type AuthRequest struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required,gte=6`
Password string `json:"password" validate:"required,gte=6"`
}
type RegisterRequest struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required,gte=6`
Password string `json:"password" validate:"required,gte=6"`
Nickname string `json:"nickname"`
}
@ -43,10 +44,17 @@ type RegisterRequest struct {
func Auth(c *gin.Context) {
var user AuthRequest
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
if errs, ok := err.(validator.ValidationErrors); !ok {
c.JSON(http.StatusBadRequest, gin.H{
"msg": err.Error(),
})
return
} else {
c.JSON(http.StatusBadRequest, gin.H{
"msg": errs.Translate(utils.Trans),
})
return
}
}
mUser, gErr := models.GetUserByUsername(user.Username)
@ -86,10 +94,17 @@ func Auth(c *gin.Context) {
func Register(c *gin.Context) {
var user RegisterRequest
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
if errs, ok := err.(validator.ValidationErrors); !ok {
c.JSON(http.StatusBadRequest, gin.H{
"msg": err.Error(),
})
return
} else {
c.JSON(http.StatusBadRequest, gin.H{
"msg": errs.Translate(utils.Trans),
})
return
}
}
if isExsit := models.ExistUserByUsername(user.Username); isExsit {

View File

@ -284,6 +284,7 @@ var doc = `{
"v1.AuthRequest": {
"type": "object",
"required": [
"password",
"username"
],
"properties": {
@ -298,6 +299,7 @@ var doc = `{
"v1.RegisterRequest": {
"type": "object",
"required": [
"password",
"username"
],
"properties": {

View File

@ -268,6 +268,7 @@
"v1.AuthRequest": {
"type": "object",
"required": [
"password",
"username"
],
"properties": {
@ -282,6 +283,7 @@
"v1.RegisterRequest": {
"type": "object",
"required": [
"password",
"username"
],
"properties": {

View File

@ -23,6 +23,7 @@ definitions:
username:
type: string
required:
- password
- username
type: object
v1.RegisterRequest:
@ -34,6 +35,7 @@ definitions:
username:
type: string
required:
- password
- username
type: object
info:

View File

@ -1,7 +1,7 @@
/*
* @Date: 2021-03-22 09:42:09
* @LastEditors: viletyy
* @LastEditTime: 2021-03-22 10:08:22
* @LastEditTime: 2021-04-06 18:18:15
* @FilePath: /potato/global/global.go
*/
package global

View File

@ -20,8 +20,8 @@ type UserSearch struct {
type User struct {
global.Model
Username string `json:"username" binding:"required"`
Password string `json:"-" binding:"required"`
Username string `json:"username"`
Password string `json:"-"`
Nickname string `json:"nickname"`
IsAdmin bool `json:"is_admin" gorm:"default: false"`
}