You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.0 KiB
47 lines
1.0 KiB
package controller |
|
|
|
import ( |
|
"net/http" |
|
|
|
"gofaster/internal/model" |
|
"gofaster/internal/service" |
|
|
|
"github.com/gin-gonic/gin" |
|
) |
|
|
|
type UserController struct { |
|
userService *service.UserService |
|
} |
|
|
|
func NewUserController(userService *service.UserService) *UserController { |
|
return &UserController{userService: userService} |
|
} |
|
|
|
func (c *UserController) RegisterRoutes(r *gin.RouterGroup) { |
|
r.GET("/users", c.ListUsers) |
|
r.POST("/users", c.CreateUser) |
|
r.GET("/users/:id", c.GetUser) |
|
r.PUT("/users/:id", c.UpdateUser) |
|
r.DELETE("/users/:id", c.DeleteUser) |
|
} |
|
|
|
func (c *UserController) ListUsers(ctx *gin.Context) { |
|
// 实现分页查询 |
|
} |
|
|
|
func (c *UserController) CreateUser(ctx *gin.Context) { |
|
var user model.User |
|
if err := ctx.ShouldBindJSON(&user); err != nil { |
|
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
|
return |
|
} |
|
|
|
if err := c.userService.CreateUser(&user); err != nil { |
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) |
|
return |
|
} |
|
|
|
ctx.JSON(http.StatusCreated, user) |
|
} |
|
|
|
// 其他方法实现...
|
|
|