package router

import (
	"exam_system/entity"
	"exam_system/result"
	"exam_system/service"
	"exam_system/vo"
	"github.com/gin-gonic/gin"
)

func Auth(router *RouterPlus) {

	authGroup := router.Group("/auth")
	{
		// 注册
		//authGroup.POST("/register", Register)
		// 登录
		authGroup.POST("/login", Login)
	}

}
func Register(context *gin.Context) *result.Result {
	var body struct {
		entity.User
		UserType string `json:"user_type,omitempty"`
	}
	if err := context.ShouldBindJSON(&body); err != nil {
		return result.PASSWORD_ERROR
	}
	if body.Username == "" || body.Password == "" || body.Sid == "" {
		return result.PASSWORD_ERROR
	}
	status :=entity.UNDER_REVIEW
	body.Status = &status
	return service.AddUser(&body.User, body.UserType)
}

func Login(context *gin.Context) *result.Result {
	var user vo.UserVo
	if err := context.ShouldBindJSON(&user); err != nil {
		return result.PASSWORD_ERROR
	}
	if user.RoleName == nil {
		return result.PARAM_ERROR
	}
	return service.Login(&user)
}
