Skip to content

Commit

Permalink
Compose Library DB Store implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
salilponde committed Dec 18, 2023
1 parent 54dbf68 commit 70f8804
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 178 deletions.
14 changes: 7 additions & 7 deletions pkg/server/handler/compose_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (h *Handler) GetComposeProjectList(c echo.Context) error {
return unprocessableEntity(c, queryGte1ExpectedError("s"))
}

rows, totalRows, err := h.composeLibraryStore.GetList(uint(p), uint(s))
rows, totalRows, err := h.localComposeLibraryStore.GetList(uint(p), uint(s))
if err != nil {
panic(err)
}
Expand All @@ -36,13 +36,13 @@ func (h *Handler) GetComposeProjectList(c echo.Context) error {
}

func (h *Handler) CreateComposeProject(c echo.Context) error {
m := model.ComposeLibraryItem{}
m := model.LocalComposeLibraryItem{}
r := &composeProjectCreateRequest{}
if err := r.bind(c, &m); err != nil {
return unprocessableEntity(c, err)
}

if err := h.composeLibraryStore.Create(&m); err != nil {
if err := h.localComposeLibraryStore.Create(&m); err != nil {
panic(err)
}

Expand All @@ -52,13 +52,13 @@ func (h *Handler) CreateComposeProject(c echo.Context) error {
func (h *Handler) UpdateComposeProject(c echo.Context) error {
projectName := c.Param("projectName")

m := model.ComposeLibraryItemUpdate{}
m := model.LocalComposeLibraryItemUpdate{}
r := &composeProjectUpdateRequest{ProjectName: projectName}
if err := r.bind(c, &m); err != nil {
return unprocessableEntity(c, err)
}

if err := h.composeLibraryStore.Update(&m); err != nil {
if err := h.localComposeLibraryStore.Update(&m); err != nil {
panic(err)
}

Expand All @@ -68,7 +68,7 @@ func (h *Handler) UpdateComposeProject(c echo.Context) error {
func (h *Handler) DeleteComposeProject(c echo.Context) error {
projectName := c.Param("projectName")

if err := h.composeLibraryStore.DeleteByName(projectName); err != nil {
if err := h.localComposeLibraryStore.DeleteByName(projectName); err != nil {
return unprocessableEntity(c, err)
}

Expand All @@ -78,7 +78,7 @@ func (h *Handler) DeleteComposeProject(c echo.Context) error {
func (h *Handler) GetComposeProject(c echo.Context) error {
projectName := c.Param("projectName")

m, err := h.composeLibraryStore.GetByName(projectName)
m, err := h.localComposeLibraryStore.GetByName(projectName)
if err != nil {
return notFound(c, "ComposeProject")
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/server/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

type Handler struct {
composeProjectsPath string
composeLibraryStore store.ComposeLibraryStore
credentialStore store.CredentialStore
environmentStore store.EnvironmentStore
userStore store.UserStore
Expand All @@ -21,7 +22,7 @@ type Handler struct {
settingStore store.SettingStore
variableStore store.VariableStore
variableValueStore store.VariableValueStore
composeLibraryStore store.ComposeLibraryStore
localComposeLibraryStore store.LocalComposeLibraryStore
}

var (
Expand All @@ -30,6 +31,7 @@ var (

func NewHandler(
composeProjectsPath string,
composeLibraryStore store.ComposeLibraryStore,
credentialStore store.CredentialStore,
environmentStore store.EnvironmentStore,
userStore store.UserStore,
Expand All @@ -38,10 +40,11 @@ func NewHandler(
settingStore store.SettingStore,
variableStore store.VariableStore,
variableValueStore store.VariableValueStore,
composeLibraryStore store.ComposeLibraryStore,
localComposeLibraryStore store.LocalComposeLibraryStore,
) *Handler {
return &Handler{
composeProjectsPath: composeProjectsPath,
composeLibraryStore: composeLibraryStore,
credentialStore: credentialStore,
environmentStore: environmentStore,
userStore: userStore,
Expand All @@ -50,7 +53,7 @@ func NewHandler(
settingStore: settingStore,
variableStore: variableStore,
variableValueStore: variableValueStore,
composeLibraryStore: composeLibraryStore,
localComposeLibraryStore: localComposeLibraryStore,
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/server/handler/node_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (h *Handler) GetNodeComposePull(c echo.Context) error {
return unprocessableEntity(c, errors.New("Project not found"))
}

clp, err := h.composeLibraryStore.GetByName(ncp.LibraryProjectName)
clp, err := h.localComposeLibraryStore.GetByName(ncp.LibraryProjectName)
if err != nil {
return unprocessableEntity(c, errors.New("Library Project not found"))
}
Expand Down Expand Up @@ -253,7 +253,7 @@ func (h *Handler) GetNodeComposeUp(c echo.Context) error {
return unprocessableEntity(c, errors.New("Project not found"))
}

clp, err := h.composeLibraryStore.GetByName(ncp.LibraryProjectName)
clp, err := h.localComposeLibraryStore.GetByName(ncp.LibraryProjectName)
if err != nil {
return unprocessableEntity(c, errors.New("Library Project not found"))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/handler/request_compose_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type composeProjectCreateRequest struct {
Definition string `json:"definition"`
}

func (r *composeProjectCreateRequest) bind(c echo.Context, m *model.ComposeLibraryItem) error {
func (r *composeProjectCreateRequest) bind(c echo.Context, m *model.LocalComposeLibraryItem) error {
if err := c.Bind(r); err != nil {
return err
}
Expand All @@ -32,7 +32,7 @@ type composeProjectUpdateRequest struct {
Definition string `json:"definition"`
}

func (r *composeProjectUpdateRequest) bind(c echo.Context, m *model.ComposeLibraryItemUpdate) error {
func (r *composeProjectUpdateRequest) bind(c echo.Context, m *model.LocalComposeLibraryItemUpdate) error {
if err := c.Bind(r); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/handler/response_compose_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ type composeLibraryItem struct {
Definition string `json:"definition"`
}

func newComposeLibraryItemHead(m *model.ComposeLibraryItemHead) composeLibraryItemHead {
func newComposeLibraryItemHead(m *model.LocalComposeLibraryItemHead) composeLibraryItemHead {
return composeLibraryItemHead{ProjectName: m.ProjectName}
}

func newComposeLibraryItemHeadList(rows []model.ComposeLibraryItemHead) []composeLibraryItemHead {
func newComposeLibraryItemHeadList(rows []model.LocalComposeLibraryItemHead) []composeLibraryItemHead {
headRows := make([]composeLibraryItemHead, len(rows))
for i, r := range rows {
headRows[i] = newComposeLibraryItemHead(&r)
}
return headRows
}

func newComposeLibraryItem(m *model.ComposeLibraryItem) composeLibraryItem {
func newComposeLibraryItem(m *model.LocalComposeLibraryItem) composeLibraryItem {
return composeLibraryItem{ProjectName: m.ProjectName, Definition: m.Definition}
}
27 changes: 27 additions & 0 deletions pkg/server/model/compose_library_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package model

// Local: This is not a DB model
type LocalComposeLibraryItemHead struct {
ProjectName string
}

type LocalComposeLibraryItem struct {
ProjectName string
Definition string
}

type LocalComposeLibraryItemUpdate struct {
ProjectName string
NewProjectName string
Definition string
}

// Remote: This is a DB model
type ComposeLibraryItem struct {
Id uint
CredentialId *uint
Credential *Credential
ProjectName string
Type string // github
Url string
}
18 changes: 0 additions & 18 deletions pkg/server/model/composelibraryitem.go

This file was deleted.

2 changes: 2 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (s *Server) Init(dbConnectionString string, dataPath string, logLevel strin
}

db.AutoMigrate(
&model.ComposeLibraryItem{},
&model.Credential{},
&model.Environment{},
&model.Node{},
Expand All @@ -68,6 +69,7 @@ func (s *Server) Init(dbConnectionString string, dataPath string, logLevel strin

h := handler.NewHandler(
composeProjectsPath,
store.NewSqlComposeLibraryStore(db),
store.NewSqlCredentialStore(db),
store.NewSqlEnvironmentStore(db),
store.NewSqlUserStore(db),
Expand Down
Loading

0 comments on commit 70f8804

Please sign in to comment.