Move into api routers instead of main
This commit is contained in:
32
services/entry/pkg/api/routers/applications.go
Normal file
32
services/entry/pkg/api/routers/applications.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"serverctl/pkg/api/middleware"
|
||||
"serverctl/pkg/infrastructure/dependencies"
|
||||
)
|
||||
|
||||
func applicationsRouter(router *gin.Engine, d *dependencies.Dependencies) {
|
||||
applications := router.Group("/applications", middleware.BasicAuthMiddleware(d.Logger, d.UsersService))
|
||||
applications.POST("/", func(c *gin.Context) {
|
||||
type CreateApplicationRequest struct {
|
||||
ProjectId int `json:"projectId" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
}
|
||||
|
||||
var createApplicationRequest CreateApplicationRequest
|
||||
if err := c.BindJSON(&createApplicationRequest); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
userId, _ := c.Get("userId")
|
||||
applicationId, err := d.ApplicationsService.CreateApplication(c.Request.Context(), createApplicationRequest.Name, userId.(int), createApplicationRequest.ProjectId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": "you have provided invalid input"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{"message": "application has been created", "applicationId": applicationId})
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user