Skip to content

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

Implement /principal/:id

天舟6c916c44d891
diff --git a/backend/api/principal.go b/backend/api/principal.go
index 25035333cd..39761798f1 100644
--- a/backend/api/principal.go
+++ b/backend/api/principal.go
@@ -18,10 +18,10 @@ const (
)
type Principal struct {
- ID uint `jsonapi:"primary,principal"`
- CreatorId uint `jsonapi:"attr,creatorId"`
+ ID int `jsonapi:"primary,principal"`
+ CreatorId int `jsonapi:"attr,creatorId"`
CreatorTs int64 `jsonapi:"attr,creatorTs"`
- UpdaterId uint `jsonapi:"attr,updaterId"`
+ UpdaterId int `jsonapi:"attr,updaterId"`
UpdatedTs int64 `jsonapi:"attr,updatedTs"`
Status PrincipalStatus `jsonapi:"attr,status"`
Type PrincipalType `jsonapi:"attr,type"`
@@ -34,4 +34,5 @@ type Principal struct {
type PrincipalService interface {
FindPrincipalList(ctx context.Context) ([]*Principal, error)
FindPrincipalByEmail(ctx context.Context, email string) (*Principal, error)
+ FindPrincipalByID(ctx context.Context, ID int) (*Principal, error)
}
diff --git a/backend/server/principal.go b/backend/server/principal.go
index e26f165b5f..d5d12b25ac 100644
--- a/backend/server/principal.go
+++ b/backend/server/principal.go
@@ -2,8 +2,11 @@ package server
import (
"context"
+ "fmt"
"net/http"
+ "strconv"
+ "github.com/bytebase/bytebase"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
)
@@ -18,7 +21,30 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
if err := jsonapi.MarshalPayload(c.Response().Writer, list); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal principal list")
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal principal list.")
+ }
+
+ return nil
+ })
+
+ g.GET("/principal/:id", func(c echo.Context) error {
+ id, err := strconv.Atoi(c.Param("id"))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s.", c.Param("id")))
+ }
+
+ principal, err := s.PrincipalService.FindPrincipalByID(context.Background(), id)
+ if err != nil {
+ if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Failed to find principal with ID: %v", id))
+ }
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch principal.")
+ }
+
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ c.Response().WriteHeader(http.StatusOK)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, principal); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal principal.")
}
return nil
diff --git a/backend/sqlite/principal.go b/backend/sqlite/principal.go
index 2a8bd74b9b..cac5d980b5 100644
--- a/backend/sqlite/principal.go
+++ b/backend/sqlite/principal.go
@@ -40,6 +40,22 @@ func (s *PrincipalService) FindPrincipalByEmail(ctx context.Context, email strin
return list[0], nil
}
+func (s *PrincipalService) FindPrincipalByID(ctx context.Context, id int) (*api.Principal, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, err
+ }
+ defer tx.Rollback()
+
+ list, err := findPrincipalList(ctx, tx, principalFilter{ID: &id})
+ if err != nil {
+ return nil, err
+ } else if len(list) == 0 {
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: "Principal not found."}
+ }
+ return list[0], nil
+}
+
// FindPrincipalList retrieves a list of principals.
func (s *PrincipalService) FindPrincipalList(ctx context.Context) ([]*api.Principal, error) {
tx, err := s.db.BeginTx(ctx, nil)