This commit is contained in:
2025-11-17 01:06:03 +01:00
parent bf78faba28
commit 4e76ee468b
16 changed files with 617 additions and 42 deletions

View File

@@ -19,12 +19,11 @@ func NewCategoryHandler(h *handler.Handler) *CategoryHandler {
}
func (h *CategoryHandler) RegisterRoutes(rg *gin.RouterGroup) {
category := rg.Group("/category")
category.GET("/", h.getCategories)
category.GET("/:id/", h.getCategory)
category.POST("/", h.addCategory)
category.PUT("/:id/", h.updateCategory)
category.DELETE("/:id/", h.deleteCategory)
rg.GET("/", h.getCategories)
rg.GET("/:id/", h.getCategory)
rg.POST("/", h.addCategory)
rg.PUT("/:id/", h.updateCategory)
rg.DELETE("/:id/", h.deleteCategory)
}
func (h *CategoryHandler) getCategories(c *gin.Context) {

View File

@@ -19,12 +19,11 @@ func NewChannelHandler(h *handler.Handler) *ChannelHandler {
}
func (h *ChannelHandler) RegisterRoutes(rg *gin.RouterGroup) {
channel := rg.Group("/channel")
channel.GET("/", h.getChannels)
channel.GET("/:id/", h.getChannel)
channel.POST("/", h.addChannel)
channel.PUT("/:id/", h.updateChannel)
channel.DELETE("/:id/", h.deleteChannel)
rg.GET("/", h.getChannels)
rg.GET("/:id/", h.getChannel)
rg.POST("/", h.addChannel)
rg.PUT("/:id/", h.updateChannel)
rg.DELETE("/:id/", h.deleteChannel)
}

View File

@@ -19,12 +19,11 @@ func NewMessageHandler(h *handler.Handler) *MessageHandler {
}
func (h *MessageHandler) RegisterRoutes(rg *gin.RouterGroup) {
message := rg.Group("/message")
message.GET("/", h.getMessages)
message.GET("/:id/", h.getMessage)
message.POST("/", h.addMessage)
message.PUT("/:id/", h.updateMessage)
message.DELETE("/:id/", h.deleteMessage)
rg.GET("/", h.getMessages)
rg.GET("/:id/", h.getMessage)
rg.POST("/", h.addMessage)
rg.PUT("/:id/", h.updateMessage)
rg.DELETE("/:id/", h.deleteMessage)
}
func (h *MessageHandler) getMessages(c *gin.Context) {

View File

@@ -19,12 +19,12 @@ func NewServerHandler(h *handler.Handler) *ServerHandler {
}
func (h *ServerHandler) RegisterRoutes(rg *gin.RouterGroup) {
server := rg.Group("/server")
server.GET("/", h.serverList)
server.GET("/:id/", h.serverDetail)
server.POST("/", h.serverAdd)
server.PUT("/:id/", h.serverUpdate)
server.DELETE("/:id/", h.serverDelete)
rg.GET("/", h.serverList)
rg.GET("/:id/", h.serverDetail)
rg.POST("/", h.serverAdd)
rg.PUT("/:id/", h.serverUpdate)
rg.DELETE("/:id/", h.serverDelete)
rg.POST("/:id/join/", h.serverJoin)
}
func (h *ServerHandler) serverList(c *gin.Context) {
@@ -126,3 +126,31 @@ func (h *ServerHandler) serverDelete(c *gin.Context) {
c.Status(http.StatusNoContent)
}
func (h *ServerHandler) serverJoin(c *gin.Context) {
id := c.Param("id")
var server models.Server
result := h.DB.First(&server, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "Server not found"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
return
}
var req JoinServerRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if server.Password != req.Password {
c.JSON(http.StatusForbidden, gin.H{"error": "Wrong password"})
return
}
c.JSON(http.StatusOK, server)
}

View File

@@ -9,3 +9,7 @@ type UpdateServerRequest struct {
Name string `json:"name" binding:"required"`
Password *string `json:"password,omitempty"`
}
type JoinServerRequest struct {
Password *string `json:"password,omitempty"`
}

View File

@@ -54,7 +54,7 @@ func (h *AuthHandler) authenticate(c *gin.Context) {
// Generate token
claims := jwt.MapClaims{
"user_id": user.ID,
"expiration_date": time.Now().Add(time.Hour * 72).Unix(),
"expiration_date": time.Now().Add(time.Minute * 15).Unix(),
"creation_date": time.Now().Unix(),
}

View File

@@ -32,14 +32,19 @@ func CreateRouter() *gin.Engine {
apiGroup := router.Group("/api")
{
serverHandler.RegisterRoutes(apiGroup)
channelHandler.RegisterRoutes(apiGroup)
categoryHandler.RegisterRoutes(apiGroup)
messageHandler.RegisterRoutes(apiGroup)
serverHandler.RegisterRoutes(apiGroup.Group("/server"))
channelHandler.RegisterRoutes(apiGroup.Group("/channel"))
categoryHandler.RegisterRoutes(apiGroup.Group("/category"))
messageHandler.RegisterRoutes(apiGroup.Group("/message"))
}
router.GET("/health", healthcheck)
// Expose OpenAPI specification (static file placed at repository root)
router.GET("/openapi.yaml", func(c *gin.Context) {
c.File("openapi.yaml")
})
return router
}