2024-10-22 09:19:30 -04:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/naiba/nezha/model"
|
|
|
|
"github.com/naiba/nezha/service/singleton"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// List user
|
|
|
|
// @Summary List user
|
|
|
|
// @Security BearerAuth
|
|
|
|
// @Schemes
|
|
|
|
// @Description List user
|
|
|
|
// @Tags auth required
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} model.CommonResponse[[]model.User]
|
|
|
|
// @Router /user [get]
|
2024-10-23 05:56:51 -04:00
|
|
|
func listUser(c *gin.Context) ([]model.User, error) {
|
2024-10-22 09:19:30 -04:00
|
|
|
var users []model.User
|
|
|
|
if err := singleton.DB.Find(&users).Error; err != nil {
|
2024-10-23 05:56:51 -04:00
|
|
|
return nil, err
|
2024-10-22 09:19:30 -04:00
|
|
|
}
|
2024-10-23 05:56:51 -04:00
|
|
|
return users, nil
|
2024-10-22 09:19:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create user
|
|
|
|
// @Summary Create user
|
|
|
|
// @Security BearerAuth
|
|
|
|
// @Schemes
|
|
|
|
// @Description Create user
|
|
|
|
// @Tags auth required
|
|
|
|
// @Accept json
|
|
|
|
// @param request body model.UserForm true "User Request"
|
|
|
|
// @Produce json
|
2024-10-23 05:56:51 -04:00
|
|
|
// @Success 200 {object} model.CommonResponse[uint64]
|
2024-10-22 09:19:30 -04:00
|
|
|
// @Router /user [post]
|
2024-10-23 05:56:51 -04:00
|
|
|
func createUser(c *gin.Context) (uint64, error) {
|
2024-10-22 09:19:30 -04:00
|
|
|
var uf model.UserForm
|
|
|
|
if err := c.ShouldBindJSON(&uf); err != nil {
|
2024-10-23 05:56:51 -04:00
|
|
|
return 0, err
|
2024-10-22 09:19:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(uf.Password) < 6 {
|
2024-10-23 05:56:51 -04:00
|
|
|
return 0, errors.New("password length must be greater than 6")
|
2024-10-22 09:19:30 -04:00
|
|
|
}
|
|
|
|
if uf.Username == "" {
|
2024-10-23 05:56:51 -04:00
|
|
|
return 0, errors.New("username can't be empty")
|
2024-10-22 09:19:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var u model.User
|
|
|
|
u.Username = uf.Username
|
|
|
|
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(uf.Password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
2024-10-23 05:56:51 -04:00
|
|
|
return 0, err
|
2024-10-22 09:19:30 -04:00
|
|
|
}
|
|
|
|
u.Password = string(hash)
|
|
|
|
|
2024-10-23 05:56:51 -04:00
|
|
|
if err := singleton.DB.Create(&u).Error; err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.ID, nil
|
2024-10-22 09:19:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Batch delete users
|
|
|
|
// @Summary Batch delete users
|
|
|
|
// @Security BearerAuth
|
|
|
|
// @Schemes
|
|
|
|
// @Description Batch delete users
|
|
|
|
// @Tags auth required
|
|
|
|
// @Accept json
|
|
|
|
// @param request body []uint true "id list"
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} model.CommonResponse[any]
|
|
|
|
// @Router /batch-delete/user [post]
|
2024-10-23 05:56:51 -04:00
|
|
|
func batchDeleteUser(c *gin.Context) (any, error) {
|
2024-10-22 09:19:30 -04:00
|
|
|
var ids []uint
|
|
|
|
if err := c.ShouldBindJSON(&ids); err != nil {
|
2024-10-23 05:56:51 -04:00
|
|
|
return nil, err
|
2024-10-22 09:19:30 -04:00
|
|
|
}
|
2024-10-23 05:56:51 -04:00
|
|
|
return nil, singleton.DB.Where("id IN (?)", ids).Delete(&model.User{}).Error
|
2024-10-22 09:19:30 -04:00
|
|
|
}
|