Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: add BookmarkRaw to represent store model for Bookmark (#869)
Yilong Lie9a91ba4f6f2
diff --git a/api/bookmark.go b/api/bookmark.goindex f3d20fbc75..c4b08f6395 100644--- a/api/bookmark.go+++ b/api/bookmark.go@@ -5,6 +5,40 @@ import ("encoding/json")+// BookmarkRaw is the store model for an Bookmark.+// Fields have exactly the same meanings as Bookmark.+type BookmarkRaw struct {+ ID int++ // Standard fields+ CreatorID int+ CreatedTs int64+ UpdaterID int+ UpdatedTs int64++ // Domain specific fields+ Name string+ Link string+}++// ToBookmark creates an instance of Bookmark based on the BookmarkRaw.+// This is intended to be called when we need to compose an Bookmark relationship.+func (raw *BookmarkRaw) ToBookmark() *Bookmark {+ return &Bookmark{+ ID: raw.ID,++ // Standard fields+ CreatorID: raw.CreatorID,+ CreatedTs: raw.CreatedTs,+ UpdaterID: raw.UpdaterID,+ UpdatedTs: raw.UpdatedTs,++ // Domain specific fields+ Name: raw.Name,+ Link: raw.Link,+ }+}+// Bookmark is the API message for a bookmark.type Bookmark struct {ID int `jsonapi:"primary,bookmark"`@@ -60,8 +94,8 @@ type BookmarkDelete struct {// BookmarkService is the service for bookmarks.type BookmarkService interface {- CreateBookmark(ctx context.Context, create *BookmarkCreate) (*Bookmark, error)- FindBookmarkList(ctx context.Context, find *BookmarkFind) ([]*Bookmark, error)- FindBookmark(ctx context.Context, find *BookmarkFind) (*Bookmark, error)+ CreateBookmark(ctx context.Context, create *BookmarkCreate) (*BookmarkRaw, error)+ FindBookmarkList(ctx context.Context, find *BookmarkFind) ([]*BookmarkRaw, error)+ FindBookmark(ctx context.Context, find *BookmarkFind) (*BookmarkRaw, error)DeleteBookmark(ctx context.Context, delete *BookmarkDelete) error}diff --git a/server/bookmark.go b/server/bookmark.goindex 99abe547e6..28f393b862 100644--- a/server/bookmark.go+++ b/server/bookmark.go@@ -22,7 +22,7 @@ func (s *Server) registerBookmarkRoutes(g *echo.Group) {bookmarkCreate.CreatorID = c.Get(getPrincipalIDContextKey()).(int)- bookmark, err := s.BookmarkService.CreateBookmark(ctx, bookmarkCreate)+ bookmarkRaw, err := s.BookmarkService.CreateBookmark(ctx, bookmarkCreate)if err != nil {if common.ErrorCode(err) == common.Conflict {return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Bookmark already exists: %s", bookmarkCreate.Link))@@ -30,7 +30,8 @@ func (s *Server) registerBookmarkRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create bookmark").SetInternal(err)}- if err := s.composeBookmarkRelationship(ctx, bookmark); err != nil {+ bookmark, err := s.composeBookmarkRelationship(ctx, bookmarkRaw)+ if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch created bookmark relationship").SetInternal(err)}@@ -51,15 +52,18 @@ func (s *Server) registerBookmarkRoutes(g *echo.Group) {bookmarkFind := &api.BookmarkFind{CreatorID: &userID,}- bookmarkList, err := s.BookmarkService.FindBookmarkList(ctx, bookmarkFind)+ bookmarkRawList, err := s.BookmarkService.FindBookmarkList(ctx, bookmarkFind)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch bookmark list").SetInternal(err)}- for _, bookmark := range bookmarkList {- if err := s.composeBookmarkRelationship(ctx, bookmark); err != nil {+ var bookmarkList []*api.Bookmark+ for _, bookmarkRaw := range bookmarkRawList {+ bookmark, err := s.composeBookmarkRelationship(ctx, bookmarkRaw)+ if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch bookmark relationship: %v", bookmark.Name)).SetInternal(err)}+ bookmarkList = append(bookmarkList, bookmark)}c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)@@ -94,18 +98,20 @@ func (s *Server) registerBookmarkRoutes(g *echo.Group) {})}-func (s *Server) composeBookmarkRelationship(ctx context.Context, bookmark *api.Bookmark) error {- var err error+func (s *Server) composeBookmarkRelationship(ctx context.Context, raw *api.BookmarkRaw) (*api.Bookmark, error) {+ bookmark := raw.ToBookmark()- bookmark.Creator, err = s.composePrincipalByID(ctx, bookmark.CreatorID)+ creator, err := s.composePrincipalByID(ctx, bookmark.CreatorID)if err != nil {- return err+ return nil, err}+ bookmark.Creator = creator- bookmark.Updater, err = s.composePrincipalByID(ctx, bookmark.UpdaterID)+ updater, err := s.composePrincipalByID(ctx, bookmark.UpdaterID)if err != nil {- return err+ return nil, err}+ bookmark.Updater = updater- return nil+ return bookmark, nil}diff --git a/store/bookmark.go b/store/bookmark.goindex ef2fad3837..08a8b8412f 100644--- a/store/bookmark.go+++ b/store/bookmark.go@@ -27,7 +27,7 @@ func NewBookmarkService(logger *zap.Logger, db *DB) *BookmarkService {}// CreateBookmark creates a new bookmark.-func (s *BookmarkService) CreateBookmark(ctx context.Context, create *api.BookmarkCreate) (*api.Bookmark, error) {+func (s *BookmarkService) CreateBookmark(ctx context.Context, create *api.BookmarkCreate) (*api.BookmarkRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -47,7 +47,7 @@ func (s *BookmarkService) CreateBookmark(ctx context.Context, create *api.Bookma}// FindBookmarkList retrieves a list of bookmarks based on find.-func (s *BookmarkService) FindBookmarkList(ctx context.Context, find *api.BookmarkFind) ([]*api.Bookmark, error) {+func (s *BookmarkService) FindBookmarkList(ctx context.Context, find *api.BookmarkFind) ([]*api.BookmarkRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -64,24 +64,24 @@ func (s *BookmarkService) FindBookmarkList(ctx context.Context, find *api.Bookma// FindBookmark retrieves a single bookmark based on find.// Returns ECONFLICT if finding more than 1 matching records.-func (s *BookmarkService) FindBookmark(ctx context.Context, find *api.BookmarkFind) (*api.Bookmark, error) {+func (s *BookmarkService) FindBookmark(ctx context.Context, find *api.BookmarkFind) (*api.BookmarkRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)}defer tx.PTx.Rollback()- list, err := findBookmarkList(ctx, tx.PTx, find)+ bookmarkRawList, err := findBookmarkList(ctx, tx.PTx, find)if err != nil {return nil, err}- if len(list) == 0 {+ if len(bookmarkRawList) == 0 {return nil, nil- } else if len(list) > 1 {- return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d activities with filter %+v, expect 1. ", len(list), find)}+ } else if len(bookmarkRawList) > 1 {+ return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d activities with filter %+v, expect 1. ", len(bookmarkRawList), find)}}- return list[0], nil+ return bookmarkRawList[0], nil}// DeleteBookmark deletes an existing bookmark by ID.@@ -105,7 +105,7 @@ func (s *BookmarkService) DeleteBookmark(ctx context.Context, delete *api.Bookma}// createBookmark creates a new bookmark.-func createBookmark(ctx context.Context, tx *sql.Tx, create *api.BookmarkCreate) (*api.Bookmark, error) {+func createBookmark(ctx context.Context, tx *sql.Tx, create *api.BookmarkCreate) (*api.BookmarkRaw, error) {// Insert row into database.row, err := tx.QueryContext(ctx, `INSERT INTO bookmark (@@ -129,23 +129,23 @@ func createBookmark(ctx context.Context, tx *sql.Tx, create *api.BookmarkCreate)defer row.Close()row.Next()- var bookmark api.Bookmark+ var bookmarkRaw api.BookmarkRawif err := row.Scan(- &bookmark.ID,- &bookmark.CreatorID,- &bookmark.CreatedTs,- &bookmark.UpdaterID,- &bookmark.UpdatedTs,- &bookmark.Name,- &bookmark.Link,+ &bookmarkRaw.ID,+ &bookmarkRaw.CreatorID,+ &bookmarkRaw.CreatedTs,+ &bookmarkRaw.UpdaterID,+ &bookmarkRaw.UpdatedTs,+ &bookmarkRaw.Name,+ &bookmarkRaw.Link,); err != nil {return nil, FormatError(err)}- return &bookmark, nil+ return &bookmarkRaw, nil}-func findBookmarkList(ctx context.Context, tx *sql.Tx, find *api.BookmarkFind) ([]*api.Bookmark, error) {+func findBookmarkList(ctx context.Context, tx *sql.Tx, find *api.BookmarkFind) ([]*api.BookmarkRaw, error) {// Build WHERE clause.where, args := []string{"1 = 1"}, []interface{}{}if v := find.ID; v != nil {@@ -173,10 +173,10 @@ func findBookmarkList(ctx context.Context, tx *sql.Tx, find *api.BookmarkFind) (}defer rows.Close()- // Iterate over result set and deserialize rows into bookmarkList.- var bookmarkList []*api.Bookmark+ // Iterate over result set and deserialize rows into bookmarkRawList.+ var bookmarkRawList []*api.BookmarkRawfor rows.Next() {- var bookmark api.Bookmark+ var bookmark api.BookmarkRawif err := rows.Scan(&bookmark.ID,&bookmark.CreatorID,@@ -189,13 +189,13 @@ func findBookmarkList(ctx context.Context, tx *sql.Tx, find *api.BookmarkFind) (return nil, FormatError(err)}- bookmarkList = append(bookmarkList, &bookmark)+ bookmarkRawList = append(bookmarkRawList, &bookmark)}if err := rows.Err(); err != nil {return nil, FormatError(err)}- return bookmarkList, nil+ return bookmarkRawList, nil}// deleteBookmark permanently deletes a bookmark by ID.