configstore: move get projecgroup projects/subgroups to action

This commit is contained in:
Simone Gotti 2019-05-13 00:21:10 +02:00
parent 5dfe7f8ce9
commit 0f9445aabe
3 changed files with 59 additions and 54 deletions

View File

@ -28,6 +28,51 @@ import (
uuid "github.com/satori/go.uuid"
)
func (h *ActionHandler) GetProjectGroupSubgroups(ctx context.Context, projectGroupRef string) ([]*types.ProjectGroup, error) {
var projectGroups []*types.ProjectGroup
err := h.readDB.Do(func(tx *db.Tx) error {
var err error
projectGroup, err := h.readDB.GetProjectGroup(tx, projectGroupRef)
if err != nil {
return err
}
if projectGroup == nil {
return util.NewErrNotFound(errors.Errorf("project group %q doesn't exist", projectGroupRef))
}
projectGroups, err = h.readDB.GetProjectGroupSubgroups(tx, projectGroup.ID)
return err
})
if err != nil {
return nil, err
}
return projectGroups, nil
}
func (h *ActionHandler) GetProjectGroupProjects(ctx context.Context, projectGroupRef string) ([]*types.Project, error) {
var projects []*types.Project
err := h.readDB.Do(func(tx *db.Tx) error {
var err error
projectGroup, err := h.readDB.GetProjectGroup(tx, projectGroupRef)
if err != nil {
return err
}
if projectGroup == nil {
return util.NewErrNotFound(errors.Errorf("project group %q doesn't exist", projectGroupRef))
}
projects, err = h.readDB.GetProjectGroupProjects(tx, projectGroup.ID)
return err
})
if err != nil {
return nil, err
}
return projects, nil
}
func (h *ActionHandler) CreateProjectGroup(ctx context.Context, projectGroup *types.ProjectGroup) (*types.ProjectGroup, error) {
if projectGroup.Name == "" {
return nil, util.NewErrBadRequest(errors.Errorf("project group name required"))

View File

@ -137,14 +137,16 @@ func (h *ProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type ProjectGroupProjectsHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
readDB *readdb.ReadDB
}
func NewProjectGroupProjectsHandler(logger *zap.Logger, readDB *readdb.ReadDB) *ProjectGroupProjectsHandler {
return &ProjectGroupProjectsHandler{log: logger.Sugar(), readDB: readDB}
func NewProjectGroupProjectsHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *ProjectGroupProjectsHandler {
return &ProjectGroupProjectsHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
}
func (h *ProjectGroupProjectsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
if err != nil {
@ -152,31 +154,9 @@ func (h *ProjectGroupProjectsHandler) ServeHTTP(w http.ResponseWriter, r *http.R
return
}
var projectGroup *types.ProjectGroup
err = h.readDB.Do(func(tx *db.Tx) error {
projectGroup, err = h.readDB.GetProjectGroup(tx, projectGroupRef)
return err
})
if err != nil {
projects, err := h.ah.GetProjectGroupProjects(ctx, projectGroupRef)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
httpError(w, err)
return
}
if projectGroup == nil {
httpError(w, util.NewErrNotFound(errors.Errorf("project group %q doesn't exist", projectGroupRef)))
return
}
var projects []*types.Project
err = h.readDB.Do(func(tx *db.Tx) error {
var err error
projects, err = h.readDB.GetProjectGroupProjects(tx, projectGroup.ID)
return err
})
if err != nil {
h.log.Errorf("err: %+v", err)
httpError(w, err)
return
}
@ -193,14 +173,16 @@ func (h *ProjectGroupProjectsHandler) ServeHTTP(w http.ResponseWriter, r *http.R
type ProjectGroupSubgroupsHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
readDB *readdb.ReadDB
}
func NewProjectGroupSubgroupsHandler(logger *zap.Logger, readDB *readdb.ReadDB) *ProjectGroupSubgroupsHandler {
return &ProjectGroupSubgroupsHandler{log: logger.Sugar(), readDB: readDB}
func NewProjectGroupSubgroupsHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *ProjectGroupSubgroupsHandler {
return &ProjectGroupSubgroupsHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
}
func (h *ProjectGroupSubgroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
if err != nil {
@ -208,31 +190,9 @@ func (h *ProjectGroupSubgroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.
return
}
var projectGroup *types.ProjectGroup
err = h.readDB.Do(func(tx *db.Tx) error {
projectGroup, err = h.readDB.GetProjectGroup(tx, projectGroupRef)
return err
})
if err != nil {
projectGroups, err := h.ah.GetProjectGroupSubgroups(ctx, projectGroupRef)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
httpError(w, err)
return
}
if projectGroup == nil {
httpError(w, util.NewErrNotFound(errors.Errorf("project group %q doesn't exist", projectGroupRef)))
return
}
var projectGroups []*types.ProjectGroup
err = h.readDB.Do(func(tx *db.Tx) error {
var err error
projectGroups, err = h.readDB.GetProjectGroupSubgroups(tx, projectGroup.ID)
return err
})
if err != nil {
h.log.Errorf("err: %+v", err)
httpError(w, err)
return
}

View File

@ -126,8 +126,8 @@ func (s *Configstore) Run(ctx context.Context) error {
corsHandler = ghandlers.CORS(corsAllowedMethodsOptions, corsAllowedHeadersOptions, corsAllowedOriginsOptions)
projectGroupHandler := api.NewProjectGroupHandler(logger, s.readDB)
projectGroupSubgroupsHandler := api.NewProjectGroupSubgroupsHandler(logger, s.readDB)
projectGroupProjectsHandler := api.NewProjectGroupProjectsHandler(logger, s.readDB)
projectGroupSubgroupsHandler := api.NewProjectGroupSubgroupsHandler(logger, s.ah, s.readDB)
projectGroupProjectsHandler := api.NewProjectGroupProjectsHandler(logger, s.ah, s.readDB)
createProjectGroupHandler := api.NewCreateProjectGroupHandler(logger, s.ah, s.readDB)
projectHandler := api.NewProjectHandler(logger, s.readDB)