Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: split data model object for api.Environment (#820)

Yilong Liba10d12946ef
diff --git a/api/environment.go b/api/environment.go
index ec6e65b83f..9fc2f86cc1 100644
--- a/api/environment.go
+++ b/api/environment.go
@@ -5,6 +5,40 @@ import (
"encoding/json"
)
+// EnvironmentRaw is the store model for an Environment.
+// Fields have exactly the same meanings as Environment.
+type EnvironmentRaw struct {
+ ID int
+
+ // Standard fields
+ RowStatus RowStatus
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Domain specific fields
+ Name string
+ Order int
+}
+
+// ToEnvironment creates an instance of Environment based on the EnvironmentRaw.
+// This is intended to be called when we need to compose an Environment relationship.
+func (raw *EnvironmentRaw) ToEnvironment() *Environment {
+ return &Environment{
+ ID: raw.ID,
+
+ RowStatus: raw.RowStatus,
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ Name: raw.Name,
+ Order: raw.Order,
+ }
+}
+
// Environment is the API message for an environment.
type Environment struct {
ID int `jsonapi:"primary,environment"`
@@ -74,8 +108,8 @@ type EnvironmentDelete struct {
// EnvironmentService is the service for environments.
type EnvironmentService interface {
- CreateEnvironment(ctx context.Context, create *EnvironmentCreate) (*Environment, error)
- FindEnvironmentList(ctx context.Context, find *EnvironmentFind) ([]*Environment, error)
- FindEnvironment(ctx context.Context, find *EnvironmentFind) (*Environment, error)
- PatchEnvironment(ctx context.Context, patch *EnvironmentPatch) (*Environment, error)
+ CreateEnvironment(ctx context.Context, create *EnvironmentCreate) (*EnvironmentRaw, error)
+ FindEnvironmentList(ctx context.Context, find *EnvironmentFind) ([]*EnvironmentRaw, error)
+ FindEnvironment(ctx context.Context, find *EnvironmentFind) (*EnvironmentRaw, error)
+ PatchEnvironment(ctx context.Context, patch *EnvironmentPatch) (*EnvironmentRaw, error)
}
diff --git a/api/project.go b/api/project.go
index fbff701adb..7bde4e6c4b 100644
--- a/api/project.go
+++ b/api/project.go
@@ -64,7 +64,7 @@ const (
TenantModeTenant ProjectTenantMode = "TENANT"
)
-// ProjectRaw is the store model for a project.
+// ProjectRaw is the store model for a Project.
// Fields have exactly the same meanings as Project.
type ProjectRaw struct {
ID int
diff --git a/server/anomaly_scanner.go b/server/anomaly_scanner.go
index 1289733424..308af2bb0b 100644
--- a/server/anomaly_scanner.go
+++ b/server/anomaly_scanner.go
@@ -58,15 +58,28 @@ func (s *AnomalyScanner) Run(ctx context.Context, wg *sync.WaitGroup) {
ctx := context.Background()
- environmentFind := &api.EnvironmentFind{}
- environmentList, err := s.server.EnvironmentService.FindEnvironmentList(ctx, environmentFind)
+ envFind := &api.EnvironmentFind{}
+ envRawList, err := s.server.EnvironmentService.FindEnvironmentList(ctx, envFind)
if err != nil {
s.l.Error("Failed to retrieve instance list", zap.Error(err))
return
}
+ // compose environments
+ var envList []*api.Environment
+ for _, envRaw := range envRawList {
+ env, err := s.server.composeEnvironmentRelationship(ctx, envRaw)
+ if err != nil {
+ s.l.Error("Failed to compose environment relationship",
+ zap.String("environment", envRaw.Name),
+ zap.Error(err))
+ return
+ }
+ envList = append(envList, env)
+ }
+
backupPlanPolicyMap := make(map[int]*api.BackupPlanPolicy)
- for _, env := range environmentList {
+ for _, env := range envList {
policy, err := s.server.PolicyService.GetBackupPlanPolicy(ctx, env.ID)
if err != nil {
s.l.Error("Failed to retrieve backup policy",
@@ -92,16 +105,19 @@ func (s *AnomalyScanner) Run(ctx context.Context, wg *sync.WaitGroup) {
s.l.Error(fmt.Sprintf("Failed to compose instance relationship, ID %v, name %q.", instance.ID, instance.Name), zap.Error(err))
continue
}
- for _, env := range environmentList {
+
+ foundEnv := false
+ for _, env := range envList {
if env.ID == instance.EnvironmentID {
if env.RowStatus == api.Normal {
instance.Environment = env
}
+ foundEnv = true
break
}
}
- if instance.Environment == nil {
+ if !foundEnv {
continue
}
diff --git a/server/environment.go b/server/environment.go
index 45c34d9cab..cb59636830 100644
--- a/server/environment.go
+++ b/server/environment.go
@@ -2,6 +2,7 @@ package server
import (
"context"
+ "errors"
"fmt"
"net/http"
"reflect"
@@ -16,27 +17,28 @@ import (
func (s *Server) registerEnvironmentRoutes(g *echo.Group) {
g.POST("/environment", func(c echo.Context) error {
ctx := context.Background()
- environmentCreate := &api.EnvironmentCreate{}
- if err := jsonapi.UnmarshalPayload(c.Request().Body, environmentCreate); err != nil {
+ envCreate := &api.EnvironmentCreate{}
+ if err := jsonapi.UnmarshalPayload(c.Request().Body, envCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create environment request").SetInternal(err)
}
- environmentCreate.CreatorID = c.Get(getPrincipalIDContextKey()).(int)
+ envCreate.CreatorID = c.Get(getPrincipalIDContextKey()).(int)
- environment, err := s.EnvironmentService.CreateEnvironment(ctx, environmentCreate)
+ envRaw, err := s.EnvironmentService.CreateEnvironment(ctx, envCreate)
if err != nil {
if common.ErrorCode(err) == common.Conflict {
- return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Environment name already exists: %s", environmentCreate.Name))
+ return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Environment name already exists: %s", envCreate.Name))
}
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create environment").SetInternal(err)
}
- if err := s.composeEnvironmentRelationship(ctx, environment); err != nil {
+ env, err := s.composeEnvironmentRelationship(ctx, envRaw)
+ if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch created environment relationship").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- if err := jsonapi.MarshalPayload(c.Response().Writer, environment); err != nil {
+ if err := jsonapi.MarshalPayload(c.Response().Writer, env); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create environment response").SetInternal(err)
}
return nil
@@ -44,20 +46,23 @@ func (s *Server) registerEnvironmentRoutes(g *echo.Group) {
g.GET("/environment", func(c echo.Context) error {
ctx := context.Background()
- environmentFind := &api.EnvironmentFind{}
+ envFind := &api.EnvironmentFind{}
if rowStatusStr := c.QueryParam("rowstatus"); rowStatusStr != "" {
rowStatus := api.RowStatus(rowStatusStr)
- environmentFind.RowStatus = &rowStatus
+ envFind.RowStatus = &rowStatus
}
- envList, err := s.EnvironmentService.FindEnvironmentList(ctx, environmentFind)
+ envRawList, err := s.EnvironmentService.FindEnvironmentList(ctx, envFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch environment list").SetInternal(err)
}
- for _, environment := range envList {
- if err := s.composeEnvironmentRelationship(ctx, environment); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch environment relationship: %v", environment.Name)).SetInternal(err)
+ var envList []*api.Environment
+ for _, envRaw := range envRawList {
+ env, err := s.composeEnvironmentRelationship(ctx, envRaw)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch environment relationship: %v", envRaw.Name)).SetInternal(err)
}
+ envList = append(envList, env)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -74,15 +79,15 @@ func (s *Server) registerEnvironmentRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Environment ID is not a number: %s", c.Param("id"))).SetInternal(err)
}
- environmentPatch := &api.EnvironmentPatch{
+ envPatch := &api.EnvironmentPatch{
ID: id,
UpdaterID: c.Get(getPrincipalIDContextKey()).(int),
}
- if err := jsonapi.UnmarshalPayload(c.Request().Body, environmentPatch); err != nil {
+ if err := jsonapi.UnmarshalPayload(c.Request().Body, envPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch environment request").SetInternal(err)
}
- environment, err := s.EnvironmentService.PatchEnvironment(ctx, environmentPatch)
+ envRaw, err := s.EnvironmentService.PatchEnvironment(ctx, envPatch)
if err != nil {
if common.ErrorCode(err) == common.NotFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Environment ID not found: %d", id))
@@ -90,12 +95,13 @@ func (s *Server) registerEnvironmentRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch environment ID: %v", id)).SetInternal(err)
}
- if err := s.composeEnvironmentRelationship(ctx, environment); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch updated environment relationship: %v", environment.Name)).SetInternal(err)
+ env, err := s.composeEnvironmentRelationship(ctx, envRaw)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch updated environment relationship: %v", envRaw.Name)).SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- if err := jsonapi.MarshalPayload(c.Response().Writer, environment); err != nil {
+ if err := jsonapi.MarshalPayload(c.Response().Writer, env); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal environment ID response: %v", id)).SetInternal(err)
}
return nil
@@ -109,27 +115,32 @@ func (s *Server) registerEnvironmentRoutes(g *echo.Group) {
}
for _, item := range patchList {
- environmentPatch, _ := item.(*api.EnvironmentPatch)
- environmentPatch.UpdaterID = c.Get(getPrincipalIDContextKey()).(int)
- _, err = s.EnvironmentService.PatchEnvironment(ctx, environmentPatch)
- if err != nil {
+ envPatch, ok := item.(*api.EnvironmentPatch)
+ if !ok {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted environment reorder request").SetInternal(errors.New("Failed to convert request item to *api.EnvironmentPatch"))
+ }
+ envPatch.UpdaterID = c.Get(getPrincipalIDContextKey()).(int)
+ if _, err := s.EnvironmentService.PatchEnvironment(ctx, envPatch); err != nil {
if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Environment ID not found: %d", environmentPatch.ID))
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Environment ID not found: %d", envPatch.ID))
}
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch environment ID: %v", environmentPatch.ID)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch environment ID: %v", envPatch.ID)).SetInternal(err)
}
}
- environmentFind := &api.EnvironmentFind{}
- envList, err := s.EnvironmentService.FindEnvironmentList(ctx, environmentFind)
+ envFind := &api.EnvironmentFind{}
+ envRawList, err := s.EnvironmentService.FindEnvironmentList(ctx, envFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch environment list for reorder").SetInternal(err)
}
- for _, environment := range envList {
- if err := s.composeEnvironmentRelationship(ctx, environment); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch reordered environment relationship: %v", environment.Name)).SetInternal(err)
+ var envList []*api.Environment
+ for _, envRaw := range envRawList {
+ env, err := s.composeEnvironmentRelationship(ctx, envRaw)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch reordered environment relationship: %v", envRaw.Name)).SetInternal(err)
}
+ envList = append(envList, env)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -141,36 +152,39 @@ func (s *Server) registerEnvironmentRoutes(g *echo.Group) {
}
func (s *Server) composeEnvironmentByID(ctx context.Context, id int) (*api.Environment, error) {
- environmentFind := &api.EnvironmentFind{
+ envFind := &api.EnvironmentFind{
ID: &id,
}
- environment, err := s.EnvironmentService.FindEnvironment(ctx, environmentFind)
+ envRaw, err := s.EnvironmentService.FindEnvironment(ctx, envFind)
if err != nil {
return nil, err
}
- if environment == nil {
+ if envRaw == nil {
return nil, fmt.Errorf("environment ID not found %v", id)
}
- if err := s.composeEnvironmentRelationship(ctx, environment); err != nil {
+ env, err := s.composeEnvironmentRelationship(ctx, envRaw)
+ if err != nil {
return nil, err
}
- return environment, nil
+ return env, nil
}
-func (s *Server) composeEnvironmentRelationship(ctx context.Context, environment *api.Environment) error {
- var err error
+func (s *Server) composeEnvironmentRelationship(ctx context.Context, raw *api.EnvironmentRaw) (*api.Environment, error) {
+ env := raw.ToEnvironment()
- environment.Creator, err = s.composePrincipalByID(ctx, environment.CreatorID)
+ creator, err := s.composePrincipalByID(ctx, env.CreatorID)
if err != nil {
- return err
+ return nil, err
}
+ env.Creator = creator
- environment.Updater, err = s.composePrincipalByID(ctx, environment.UpdaterID)
+ updater, err := s.composePrincipalByID(ctx, env.UpdaterID)
if err != nil {
- return err
+ return nil, err
}
+ env.Updater = updater
- return nil
+ return env, nil
}
diff --git a/server/label.go b/server/label.go
index 33c38e4c87..df1aa1d7ac 100644
--- a/server/label.go
+++ b/server/label.go
@@ -21,24 +21,24 @@ func (s *Server) registerLabelRoutes(g *echo.Group) {
find := &api.LabelKeyFind{
RowStatus: &rowStatus,
}
- list, err := s.LabelService.FindLabelKeyList(ctx, find)
+ labelKeyList, err := s.LabelService.FindLabelKeyList(ctx, find)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch label keys").SetInternal(err)
}
// Add reserved environment key.
- envList, err := s.EnvironmentService.FindEnvironmentList(ctx, &api.EnvironmentFind{RowStatus: &rowStatus})
+ envRawList, err := s.EnvironmentService.FindEnvironmentList(ctx, &api.EnvironmentFind{RowStatus: &rowStatus})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch environments").SetInternal(err)
}
envKey := &api.LabelKey{Key: api.EnvironmentKeyName}
- for _, env := range envList {
- envKey.ValueList = append(envKey.ValueList, env.Name)
+ for _, envRaw := range envRawList {
+ envKey.ValueList = append(envKey.ValueList, envRaw.Name)
}
- list = append(list, envKey)
+ labelKeyList = append(labelKeyList, envKey)
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- if err := jsonapi.MarshalPayload(c.Response().Writer, list); err != nil {
+ if err := jsonapi.MarshalPayload(c.Response().Writer, labelKeyList); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal label keys response").SetInternal(err)
}
return nil
diff --git a/server/project.go b/server/project.go
index 45d4a4d930..39bbbf9db3 100644
--- a/server/project.go
+++ b/server/project.go
@@ -89,7 +89,7 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
}
var activeProjectList []*api.Project
- projectList := make([]*api.Project, 0, len(projectRawList))
+ var projectList []*api.Project
for _, projectRaw := range projectRawList {
project, err := s.composeProjectRelationship(ctx, projectRaw)
if err != nil {
@@ -304,7 +304,7 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Retrieved %d repository list for project ID: %d, expect at most 1", len(repoRawList), projectID)).SetInternal(err)
}
- repoList := make([]*api.Repository, 0, len(repoRawList))
+ var repoList []*api.Repository
for _, repoRaw := range repoRawList {
repo, err := s.composeRepositoryRelationship(ctx, repoRaw)
if err != nil {
diff --git a/server/vcs.go b/server/vcs.go
index 2cbf5921f7..fee69f638a 100644
--- a/server/vcs.go
+++ b/server/vcs.go
@@ -52,17 +52,17 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch vcs list").SetInternal(err)
}
- list := make([]*api.VCS, 0, len(vcsRawList))
+ var vcsList []*api.VCS
for _, vcsRaw := range vcsRawList {
vcs, err := s.composeVCSRelationship(ctx, vcsRaw)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch vcs relationship: %v", vcs.ID)).SetInternal(err)
}
- list = append(list, vcs)
+ vcsList = append(vcsList, vcs)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- if err := jsonapi.MarshalPayload(c.Response().Writer, list); err != nil {
+ if err := jsonapi.MarshalPayload(c.Response().Writer, vcsList); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal vcs list response").SetInternal(err)
}
return nil
@@ -160,7 +160,7 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch repository list for vcs ID: %v", id)).SetInternal(err)
}
- repoList := make([]*api.Repository, 0, len(repoRawList))
+ var repoList []*api.Repository
for _, repoRaw := range repoRawList {
repo, err := s.composeRepositoryRelationship(ctx, repoRaw)
if err != nil {
diff --git a/store/environment.go b/store/environment.go
index 63ea7750fa..840caeb83c 100644
--- a/store/environment.go
+++ b/store/environment.go
@@ -29,7 +29,7 @@ func NewEnvironmentService(logger *zap.Logger, db *DB, cache api.CacheService) *
}
// CreateEnvironment creates a new environment.
-func (s *EnvironmentService) CreateEnvironment(ctx context.Context, create *api.EnvironmentCreate) (*api.Environment, error) {
+func (s *EnvironmentService) CreateEnvironment(ctx context.Context, create *api.EnvironmentCreate) (*api.EnvironmentRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -53,7 +53,7 @@ func (s *EnvironmentService) CreateEnvironment(ctx context.Context, create *api.
}
// FindEnvironmentList retrieves a list of environments based on find.
-func (s *EnvironmentService) FindEnvironmentList(ctx context.Context, find *api.EnvironmentFind) ([]*api.Environment, error) {
+func (s *EnvironmentService) FindEnvironmentList(ctx context.Context, find *api.EnvironmentFind) ([]*api.EnvironmentRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -78,15 +78,15 @@ func (s *EnvironmentService) FindEnvironmentList(ctx context.Context, find *api.
// FindEnvironment retrieves a single environment based on find.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *EnvironmentService) FindEnvironment(ctx context.Context, find *api.EnvironmentFind) (*api.Environment, error) {
+func (s *EnvironmentService) FindEnvironment(ctx context.Context, find *api.EnvironmentFind) (*api.EnvironmentRaw, error) {
if find.ID != nil {
- environment := &api.Environment{}
- has, err := s.cache.FindCache(api.EnvironmentCache, *find.ID, environment)
+ envRaw := &api.EnvironmentRaw{}
+ has, err := s.cache.FindCache(api.EnvironmentCache, *find.ID, envRaw)
if err != nil {
return nil, err
}
if has {
- return environment, nil
+ return envRaw, nil
}
}
@@ -96,32 +96,32 @@ func (s *EnvironmentService) FindEnvironment(ctx context.Context, find *api.Envi
}
defer tx.PTx.Rollback()
- list, err := s.findEnvironmentList(ctx, tx.PTx, find)
+ envRawList, err := s.findEnvironmentList(ctx, tx.PTx, find)
if err != nil {
return nil, err
}
- if len(list) == 0 {
+ if len(envRawList) == 0 {
return nil, nil
- } else if len(list) > 1 {
- return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d environments with filter %+v, expect 1", len(list), find)}
+ } else if len(envRawList) > 1 {
+ return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d environments with filter %+v, expect 1", len(envRawList), find)}
}
- if err := s.cache.UpsertCache(api.EnvironmentCache, list[0].ID, list[0]); err != nil {
+ if err := s.cache.UpsertCache(api.EnvironmentCache, envRawList[0].ID, envRawList[0]); err != nil {
return nil, err
}
- return list[0], nil
+ return envRawList[0], nil
}
// PatchEnvironment updates an existing environment by ID.
// Returns ENOTFOUND if environment does not exist.
-func (s *EnvironmentService) PatchEnvironment(ctx context.Context, patch *api.EnvironmentPatch) (*api.Environment, error) {
+func (s *EnvironmentService) PatchEnvironment(ctx context.Context, patch *api.EnvironmentPatch) (*api.EnvironmentRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- environment, err := s.patchEnvironment(ctx, tx.PTx, patch)
+ envRaw, err := s.patchEnvironment(ctx, tx.PTx, patch)
if err != nil {
return nil, FormatError(err)
}
@@ -130,15 +130,15 @@ func (s *EnvironmentService) PatchEnvironment(ctx context.Context, patch *api.En
return nil, FormatError(err)
}
- if err := s.cache.UpsertCache(api.EnvironmentCache, environment.ID, environment); err != nil {
+ if err := s.cache.UpsertCache(api.EnvironmentCache, envRaw.ID, envRaw); err != nil {
return nil, err
}
- return environment, nil
+ return envRaw, nil
}
// createEnvironment creates a new environment.
-func (s *EnvironmentService) createEnvironment(ctx context.Context, tx *sql.Tx, create *api.EnvironmentCreate) (*api.Environment, error) {
+func (s *EnvironmentService) createEnvironment(ctx context.Context, tx *sql.Tx, create *api.EnvironmentCreate) (*api.EnvironmentRaw, error) {
// The order is the MAX(order) + 1
row1, err1 := tx.QueryContext(ctx, `
SELECT "order"
@@ -187,25 +187,25 @@ func (s *EnvironmentService) createEnvironment(ctx context.Context, tx *sql.Tx,
defer row2.Close()
row2.Next()
- var environment api.Environment
+ var envRaw api.EnvironmentRaw
if err := row2.Scan(
- &environment.ID,
- &environment.RowStatus,
- &environment.CreatorID,
- &environment.CreatedTs,
- &environment.UpdaterID,
- &environment.UpdatedTs,
- &environment.Name,
- &environment.Order,
+ &envRaw.ID,
+ &envRaw.RowStatus,
+ &envRaw.CreatorID,
+ &envRaw.CreatedTs,
+ &envRaw.UpdaterID,
+ &envRaw.UpdatedTs,
+ &envRaw.Name,
+ &envRaw.Order,
); err != nil {
fmt.Printf("Yang4: %v\n", err)
return nil, FormatError(err)
}
- return &environment, nil
+ return &envRaw, nil
}
-func (s *EnvironmentService) findEnvironmentList(ctx context.Context, tx *sql.Tx, find *api.EnvironmentFind) ([]*api.Environment, error) {
+func (s *EnvironmentService) findEnvironmentList(ctx context.Context, tx *sql.Tx, find *api.EnvironmentFind) ([]*api.EnvironmentRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -235,9 +235,9 @@ func (s *EnvironmentService) findEnvironmentList(ctx context.Context, tx *sql.Tx
defer rows.Close()
// Iterate over result set and deserialize rows into list.
- list := make([]*api.Environment, 0)
+ var envRawList []*api.EnvironmentRaw
for rows.Next() {
- var environment api.Environment
+ var environment api.EnvironmentRaw
if err := rows.Scan(
&environment.ID,
&environment.RowStatus,
@@ -251,17 +251,17 @@ func (s *EnvironmentService) findEnvironmentList(ctx context.Context, tx *sql.Tx
return nil, FormatError(err)
}
- list = append(list, &environment)
+ envRawList = append(envRawList, &environment)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return list, nil
+ return envRawList, nil
}
// patchEnvironment updates a environment by ID. Returns the new state of the environment after update.
-func (s *EnvironmentService) patchEnvironment(ctx context.Context, tx *sql.Tx, patch *api.EnvironmentPatch) (*api.Environment, error) {
+func (s *EnvironmentService) patchEnvironment(ctx context.Context, tx *sql.Tx, patch *api.EnvironmentPatch) (*api.EnvironmentRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
if v := patch.RowStatus; v != nil {
@@ -291,7 +291,7 @@ func (s *EnvironmentService) patchEnvironment(ctx context.Context, tx *sql.Tx, p
defer row.Close()
if row.Next() {
- var environment api.Environment
+ var environment api.EnvironmentRaw
if err := row.Scan(
&environment.ID,
&environment.RowStatus,