新增客户端管理功能,包括创建、编辑和删除客户端的API,更新用户管理功能,添加用户创建和编辑页面,优化管理员功能,增强用户和客户端的管理体验。
This commit is contained in:
75
handlers/client_handler.go
Normal file
75
handlers/client_handler.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"oidc-oauth2-server/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ClientHandler struct {
|
||||
clientService *services.ClientService
|
||||
}
|
||||
|
||||
func NewClientHandler(clientService *services.ClientService) *ClientHandler {
|
||||
return &ClientHandler{clientService: clientService}
|
||||
}
|
||||
|
||||
// RegisterRoutes 注册路由
|
||||
func (h *ClientHandler) RegisterRoutes(router *gin.Engine) {
|
||||
api := router.Group("/api")
|
||||
{
|
||||
api.POST("/clients", h.CreateClient)
|
||||
api.PUT("/clients/:id", h.UpdateClient)
|
||||
api.DELETE("/clients/:id", h.DeleteClient)
|
||||
}
|
||||
}
|
||||
|
||||
// CreateClient 创建客户端
|
||||
func (h *ClientHandler) CreateClient(c *gin.Context) {
|
||||
var req services.ClientRegistrationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
client, err := h.clientService.RegisterClient(&req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, client)
|
||||
}
|
||||
|
||||
// UpdateClient 更新客户端
|
||||
func (h *ClientHandler) UpdateClient(c *gin.Context) {
|
||||
clientID := c.Param("id")
|
||||
|
||||
var req services.ClientRegistrationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
client, err := h.clientService.UpdateClient(clientID, &req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, client)
|
||||
}
|
||||
|
||||
// DeleteClient 删除客户端
|
||||
func (h *ClientHandler) DeleteClient(c *gin.Context) {
|
||||
clientID := c.Param("id")
|
||||
|
||||
if err := h.clientService.DeleteClient(clientID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
Reference in New Issue
Block a user