diff --git a/pkg/server/handler/handler.go b/pkg/server/handler/handler.go index 1942404..3a29b3d 100644 --- a/pkg/server/handler/handler.go +++ b/pkg/server/handler/handler.go @@ -19,6 +19,7 @@ type Handler struct { userStore store.UserStore nodeStore store.NodeStore nodeComposeProjectStore store.NodeComposeProjectStore + nodeComposeProjectVariableStore store.NodeComposeProjectVariableStore settingStore store.SettingStore variableStore store.VariableStore variableValueStore store.VariableValueStore @@ -37,6 +38,7 @@ func NewHandler( userStore store.UserStore, nodeStore store.NodeStore, nodeComposeProjectStore store.NodeComposeProjectStore, + nodeComposeProjectVariableStore store.NodeComposeProjectVariableStore, settingStore store.SettingStore, variableStore store.VariableStore, variableValueStore store.VariableValueStore, @@ -50,6 +52,7 @@ func NewHandler( userStore: userStore, nodeStore: nodeStore, nodeComposeProjectStore: nodeComposeProjectStore, + nodeComposeProjectVariableStore: nodeComposeProjectVariableStore, settingStore: settingStore, variableStore: variableStore, variableValueStore: variableValueStore, @@ -146,22 +149,31 @@ func (h *Handler) Register(e *echo.Echo) { githubcomposelibrary.DELETE("/:id", h.DeleteGitHubComposeProject) githubcomposelibrary.GET("/:id", h.GetGitHubComposeProjectById) - node_compose := nodes.Group("/:nodeId/compose") - node_compose.GET("", h.GetNodeComposeProjectList) - node_compose.POST("/create/github", h.CreateGitHubNodeComposeProject) - node_compose.POST("/create/local", h.CreateLocalNodeComposeProject) - node_compose.POST("/create/library", h.AddNodeComposeProjectFromLibrary) - node_compose.GET("/uniquename", h.IsUniqueNodeComposeProjectName) - node_compose.GET("/:id/uniquename", h.IsUniqueNodeComposeProjectNameExcludeItself) - node_compose.PUT("/:id/github", h.UpdateGitHubNodeComposeProject) - node_compose.PUT("/:id/local", h.UpdateLocalNodeComposeProject) - node_compose.GET("/:id", h.GetNodeComposeProject) - node_compose.DELETE("/:id", h.DeleteNodeComposeProject) - node_compose.GET("/:id/containers", h.GetNodeComposeContainerList) - node_compose.GET("/:id/logs", h.GetNodeComposeLogs) - node_compose.GET("/:id/pull", h.GetNodeComposePull) - node_compose.GET("/:id/up", h.GetNodeComposeUp) - node_compose.GET("/:id/down", h.GetNodeComposeDown) + node_compose_project := nodes.Group("/:nodeId/compose") + node_compose_project.GET("", h.GetNodeComposeProjectList) + node_compose_project.POST("/create/github", h.CreateGitHubNodeComposeProject) + node_compose_project.POST("/create/local", h.CreateLocalNodeComposeProject) + node_compose_project.POST("/create/library", h.AddNodeComposeProjectFromLibrary) + node_compose_project.GET("/uniquename", h.IsUniqueNodeComposeProjectName) + node_compose_project.GET("/:id/uniquename", h.IsUniqueNodeComposeProjectNameExcludeItself) + node_compose_project.PUT("/:id/github", h.UpdateGitHubNodeComposeProject) + node_compose_project.PUT("/:id/local", h.UpdateLocalNodeComposeProject) + node_compose_project.GET("/:id", h.GetNodeComposeProject) + node_compose_project.DELETE("/:id", h.DeleteNodeComposeProject) + node_compose_project.GET("/:id/containers", h.GetNodeComposeContainerList) + node_compose_project.GET("/:id/logs", h.GetNodeComposeLogs) + node_compose_project.GET("/:id/pull", h.GetNodeComposePull) + node_compose_project.GET("/:id/up", h.GetNodeComposeUp) + node_compose_project.GET("/:id/down", h.GetNodeComposeDown) + + node_compose_project_variables := node_compose_project.Group("/:node_compose_project_id/variables") + node_compose_project_variables.POST("", h.CreateVariable) + node_compose_project_variables.PUT("/:id", h.UpdateVariable) + node_compose_project_variables.GET("", h.GetVariableList) + node_compose_project_variables.GET("/:id", h.GetVariableById) + node_compose_project_variables.DELETE("/:id", h.DeleteVariableById) + node_compose_project_variables.GET("/uniquename", h.IsUniqueVariableName) + node_compose_project_variables.GET("/:id/uniquename", h.IsUniqueVariableNameExcludeItself) variables := v1.Group("/variables") variables.POST("", h.CreateVariable) diff --git a/pkg/server/handler/node_compose_project_variable.go b/pkg/server/handler/node_compose_project_variable.go new file mode 100644 index 0000000..ec2cb00 --- /dev/null +++ b/pkg/server/handler/node_compose_project_variable.go @@ -0,0 +1,198 @@ +package handler + +import ( + "strconv" + + "github.com/productiveops/dokemon/pkg/server/model" + + "github.com/labstack/echo/v4" +) + +func (h *Handler) CreateNodeComposeProjectVariable(c echo.Context) error { + nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id")) + } + + m := model.NodeComposeProjectVariable{} + r := &nodeComposeProjectVariableCreateRequest{} + if err := r.bind(c, &m); err != nil { + return unprocessableEntity(c, err) + } + + isUnique, err := h.nodeComposeProjectVariableStore.IsUniqueName(uint(nodeComposeProjectid), r.Name) + if err != nil { + panic(err) + } + + if !isUnique { + return unprocessableEntity(c, duplicateNameError()) + } + + if err := h.nodeComposeProjectVariableStore.Create(&m); err != nil { + panic(err) + } + + return created(c, m.Id) +} + +func (h *Handler) UpdateNodeComposeProjectVariable(c echo.Context) error { + nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id")) + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("id")) + } + + exists, err := h.nodeComposeProjectVariableStore.Exists(uint(nodeComposeProjectid), uint(id)) + if err != nil { + panic(err) + } + + if !exists { + return resourceNotFound(c, "NodeComposeProjectVariable") + } + + m := model.NodeComposeProjectVariable{} + r := &nodeComposeProjectVariableUpdateRequest{Id: uint(id)} + if err := r.bind(c, &m); err != nil { + return unprocessableEntity(c, err) + } + + isUnique, err := h.nodeComposeProjectVariableStore.IsUniqueNameExcludeItself(uint(nodeComposeProjectid), r.Name, r.Id) + if err != nil { + panic(err) + } + + if !isUnique { + return unprocessableEntity(c, duplicateNameError()) + } + + if err := h.nodeComposeProjectVariableStore.Update(&m); err != nil { + panic(err) + } + + return noContent(c) +} + +func (h *Handler) DeleteNodeComposeProjectVariableById(c echo.Context) error { + nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id")) + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("id")) + } + + exists, err := h.nodeComposeProjectVariableStore.Exists(uint(nodeComposeProjectid), uint(id)) + if err != nil { + panic(err) + } + + if !exists { + return resourceNotFound(c, "NodeComposeProjectVariable") + } + + if err := h.nodeComposeProjectVariableStore.DeleteById(uint(nodeComposeProjectid), uint(id)); err != nil { + panic(err) + } + + return noContent(c) +} + +func (h *Handler) GetNodeComposeProjectVariableList(c echo.Context) error { + nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id")) + } + + p, err := strconv.Atoi(c.QueryParam("p")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("p")) + } + + if p < 1 { + return unprocessableEntity(c, queryGte1ExpectedError("p")) + } + + s, err := strconv.Atoi(c.QueryParam("s")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("s")) + } + + if s < 1 { + return unprocessableEntity(c, queryGte1ExpectedError("s")) + } + + rows, totalRows, err := h.nodeComposeProjectVariableStore.GetList(uint(nodeComposeProjectid), uint(p), uint(s)) + if err != nil { + panic(err) + } + + return ok(c, newPageResponse[nodeComposeProjectVariableResponse](newNodeComposeProjectVariableList(rows), uint(p), uint(s), uint(totalRows))) +} + +func (h *Handler) GetNodeComposeProjectVariableById(c echo.Context) error { + nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id")) + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("id")) + } + + m, err := h.nodeComposeProjectVariableStore.GetById(uint(nodeComposeProjectid), uint(id)) + if err != nil { + panic(err) + } + + if m == nil { + return resourceNotFound(c, "NodeComposeProjectVariable") + } + + return ok(c, newNodeComposeProjectVariableResponse(m)) +} + +func (h *Handler) IsUniqueNodeComposeProjectVariableName(c echo.Context) error { + nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id")) + } + + value := c.QueryParam("value") + + unique, err := h.nodeComposeProjectVariableStore.IsUniqueName(uint(nodeComposeProjectid), value) + if err != nil { + panic(err) + } + + return ok(c, newUniqueResponse(unique)) +} + +func (h *Handler) IsUniqueNodeComposeProjectVariableNameExcludeItself(c echo.Context) error { + nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id")) + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return unprocessableEntity(c, routeIntExpectedError("id")) + } + + value := c.QueryParam("value") + + unique, err := h.nodeComposeProjectVariableStore.IsUniqueNameExcludeItself(uint(nodeComposeProjectid), value, uint(id)) + if err != nil { + panic(err) + } + + return ok(c, newUniqueResponse(unique)) +} \ No newline at end of file diff --git a/pkg/server/handler/response_node_compose_project_variable.go b/pkg/server/handler/response_node_compose_project_variable.go index 489b907..9e1b102 100644 --- a/pkg/server/handler/response_node_compose_project_variable.go +++ b/pkg/server/handler/response_node_compose_project_variable.go @@ -10,12 +10,20 @@ type nodeComposeProjectVariableResponse struct { Value string `json:"value"` } -func newNodeComposeProjectVariableResponse(m *model.NodeComposeProjectVariable) *nodeComposeProjectVariableResponse { - return &nodeComposeProjectVariableResponse{ +func newNodeComposeProjectVariableResponse(m *model.NodeComposeProjectVariable) nodeComposeProjectVariableResponse { + return nodeComposeProjectVariableResponse{ Id: m.Id, NodeComposeProjectId: m.NodeComposeProjectId, Name: m.Name, IsSecret: m.IsSecret, Value: m.Value, } +} + +func newNodeComposeProjectVariableList(rows []model.NodeComposeProjectVariable) []nodeComposeProjectVariableResponse { + headRows := make([]nodeComposeProjectVariableResponse, len(rows)) + for i, r := range rows { + headRows[i] = newNodeComposeProjectVariableResponse(&r) + } + return headRows } \ No newline at end of file diff --git a/pkg/server/server.go b/pkg/server/server.go index 3ffe80b..4b56f75 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -69,6 +69,7 @@ func NewServer(dbConnectionString string, dataPath string, logLevel string, sslE store.NewSqlUserStore(db), store.NewSqlNodeStore(db), sqlNodeComposeProjectStore, + store.NewSqlNodeComposeProjectVariableStore(db), store.NewSqlSettingStore(db), store.NewSqlVariableStore(db), store.NewSqlVariableValueStore(db), diff --git a/pkg/server/store/interfaces.go b/pkg/server/store/interfaces.go index 569e3ae..a730b61 100644 --- a/pkg/server/store/interfaces.go +++ b/pkg/server/store/interfaces.go @@ -48,6 +48,18 @@ type NodeComposeProjectStore interface { IsUniqueNameExcludeItself(nodeId uint, name string, id uint) (bool, error) } +type NodeComposeProjectVariableStore interface { + Create(m *model.NodeComposeProjectVariable) error + Update(m *model.NodeComposeProjectVariable) error + GetById(nodeComposeProjectId uint, id uint) (*model.NodeComposeProjectVariable, error) + GetList(nodeComposeProjectId uint, pageNo, pageSize uint) ([]model.NodeComposeProjectVariable, int64, error) + DeleteById(nodeComposeProjectId uint, id uint) error + Exists(nodeComposeProjectId uint, id uint) (bool, error) + + IsUniqueName(nodeComposeProjectId uint, name string) (bool, error) + IsUniqueNameExcludeItself(nodeComposeProjectId uint, name string, id uint) (bool, error) +} + type SettingStore interface { Create(m *model.Setting) error Update(m *model.Setting) error