Files
oidc-server/handlers/client_handler.go

76 lines
1.7 KiB
Go

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)
}