feat: create user, read users, batch delete users

This commit is contained in:
naiba 2024-10-22 21:19:30 +08:00
parent 73e3e4f3a1
commit 387da11f1b
7 changed files with 107 additions and 11 deletions

View File

@ -63,17 +63,21 @@ func routers(r *gin.Engine) {
auth := api.Group("", authMiddleware.MiddlewareFunc())
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
auth.POST("/server-group", commonHandler(newServerGroup))
auth.PATCH("/server-group/:id", commonHandler(editServerGroup))
auth.GET("/user", commonHandler(listUser))
auth.POST("/user", commonHandler(createUser))
auth.POST("/batch-delete/user", commonHandler(batchDeleteUser))
auth.POST("/server-group", commonHandler(createServerGroup))
auth.PATCH("/server-group/:id", commonHandler(updateServerGroup))
auth.POST("/batch-delete/server-group", commonHandler(batchDeleteServerGroup))
auth.PATCH("/server/:id", commonHandler(editServer))
auth.PATCH("/server/:id", commonHandler(updateServer))
auth.POST("/batch-delete/server", commonHandler(batchDeleteServer))
auth.GET("/ddns", commonHandler(listDDNS))
auth.GET("/ddns/providers", commonHandler(listProviders))
auth.POST("/ddns", commonHandler(newDDNS))
auth.PATCH("/ddns/:id", commonHandler(editDDNS))
auth.POST("/ddns", commonHandler(createDDNS))
auth.PATCH("/ddns/:id", commonHandler(updateDDNS))
auth.POST("/batch-delete/ddns", commonHandler(batchDeleteDDNS))
}

View File

@ -25,7 +25,7 @@ import (
// @Produce json
// @Success 200 {object} model.CommonResponse[any]
// @Router /ddns [post]
func newDDNS(c *gin.Context) error {
func createDDNS(c *gin.Context) error {
var df model.DDNSForm
var p model.DDNSProfile
@ -86,7 +86,7 @@ func newDDNS(c *gin.Context) error {
// @Produce json
// @Success 200 {object} model.CommonResponse[any]
// @Router /ddns/{id} [patch]
func editDDNS(c *gin.Context) error {
func updateDDNS(c *gin.Context) error {
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)

View File

@ -24,7 +24,7 @@ import (
// @Produce json
// @Success 200 {object} model.CommonResponse[any]
// @Router /server/{id} [patch]
func editServer(c *gin.Context) error {
func updateServer(c *gin.Context) error {
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {

View File

@ -64,7 +64,7 @@ func listServerGroup(c *gin.Context) error {
// @Produce json
// @Success 200 {object} model.CommonResponse[any]
// @Router /server-group [post]
func newServerGroup(c *gin.Context) error {
func createServerGroup(c *gin.Context) error {
var sgf model.ServerGroupForm
if err := c.ShouldBindJSON(&sgf); err != nil {
return err
@ -114,7 +114,7 @@ func newServerGroup(c *gin.Context) error {
// @Produce json
// @Success 200 {object} model.CommonResponse[any]
// @Router /server-group/{id} [patch]
func editServerGroup(c *gin.Context) error {
func updateServerGroup(c *gin.Context) error {
id := c.Param("id")
var sg model.ServerGroupForm
if err := c.ShouldBindJSON(&sg); err != nil {

View File

@ -0,0 +1,86 @@
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]
func listUser(c *gin.Context) error {
var users []model.User
if err := singleton.DB.Find(&users).Error; err != nil {
return err
}
c.JSON(200, model.CommonResponse[[]model.User]{
Success: true,
Data: users,
})
return nil
}
// 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
// @Success 200 {object} model.CommonResponse[any]
// @Router /user [post]
func createUser(c *gin.Context) error {
var uf model.UserForm
if err := c.ShouldBindJSON(&uf); err != nil {
return err
}
if len(uf.Password) < 6 {
return errors.New("password length must be greater than 6")
}
if uf.Username == "" {
return errors.New("username can't be empty")
}
var u model.User
u.Username = uf.Username
hash, err := bcrypt.GenerateFromPassword([]byte(uf.Password), bcrypt.DefaultCost)
if err != nil {
return err
}
u.Password = string(hash)
return singleton.DB.Create(&u).Error
}
// 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]
func batchDeleteUser(c *gin.Context) error {
var ids []uint
if err := c.ShouldBindJSON(&ids); err != nil {
return err
}
return singleton.DB.Where("id IN (?)", ids).Delete(&model.User{}).Error
}

View File

@ -2,6 +2,6 @@ package model
type User struct {
Common
Username string `json:"username,omitempty"`
Username string `json:"username,omitempty" gorm:"uniqueIndex"`
Password string `json:"password,omitempty" gorm:"type:char(72)"`
}

6
model/user_api.go Normal file
View File

@ -0,0 +1,6 @@
package model
type UserForm struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty" gorm:"type:char(72)"`
}