package app

import (
	"bwallet/pkg/errno"
	"bwallet/pkg/handler"
	"bwallet/service/user_service"
	"bwallet/validate_service"
	"github.com/gin-gonic/gin"
	"strings"
)

func AddUser(c *gin.Context) {
	user := validate_service.User{}
	c.ShouldBindJSON(&user)

	//方法一
	if ok, errors := validate_service.ValidateInputs(user); !ok {
		for _, err := range errors {
			handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
			return
		}
	}
	user_service := user_service.User{
		UserName: user.UserName,
	}

	if err := user_service.Add(); err != nil {
		handler.SendResponse(c, errno.ErrAddUser, nil)
		return
	}

	handler.SendResponse(c, nil, nil)
}
