105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/dusiburg/kotosoft-id/internal/models"
|
|
"github.com/dusiburg/kotosoft-id/internal/services"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type ClientHandler struct {
|
|
svc *services.Service
|
|
}
|
|
|
|
func NewClientHandler(svc *services.Service) *ClientHandler {
|
|
return &ClientHandler{svc: svc}
|
|
}
|
|
|
|
func (h *ClientHandler) Create(c *gin.Context) {
|
|
var input models.ClientCreateInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.Error(models.ErrBadRequest)
|
|
return
|
|
}
|
|
|
|
client, rawSecret, err := h.svc.CreateClient(c.Request.Context(), input)
|
|
if err != nil {
|
|
c.Error(err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, gin.H{
|
|
"id": client.ID,
|
|
"client_id": client.ClientID,
|
|
"client_secret": rawSecret,
|
|
"name": client.Name,
|
|
"redirect_uris": client.RedirectURIs,
|
|
"grant_types": client.GrantTypes,
|
|
"scopes": client.Scopes,
|
|
"trusted": client.Trusted,
|
|
"is_active": client.IsActive,
|
|
"created_at": client.CreatedAt,
|
|
})
|
|
}
|
|
|
|
func (h *ClientHandler) List(c *gin.Context) {
|
|
clients, err := h.svc.ListClients(c.Request.Context())
|
|
if err != nil {
|
|
c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, clients)
|
|
}
|
|
|
|
func (h *ClientHandler) Get(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
c.Error(models.ErrBadRequest)
|
|
return
|
|
}
|
|
|
|
client, err := h.svc.GetClient(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, client)
|
|
}
|
|
|
|
func (h *ClientHandler) Update(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
c.Error(models.ErrBadRequest)
|
|
return
|
|
}
|
|
|
|
var input models.ClientUpdateInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.Error(models.ErrBadRequest)
|
|
return
|
|
}
|
|
|
|
client, err := h.svc.UpdateClient(c.Request.Context(), id, input)
|
|
if err != nil {
|
|
c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, client)
|
|
}
|
|
|
|
func (h *ClientHandler) Deactivate(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
c.Error(models.ErrBadRequest)
|
|
return
|
|
}
|
|
|
|
if err := h.svc.DeactivateClient(c.Request.Context(), id); err != nil {
|
|
c.Error(err)
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|