Skip to content

Commit

Permalink
Merge branch 'main' into fix-web/model-column
Browse files Browse the repository at this point in the history
  • Loading branch information
caichi-t authored Aug 27, 2024
2 parents 58a21c7 + f28301f commit 080b9ee
Show file tree
Hide file tree
Showing 12 changed files with 643 additions and 71 deletions.
113 changes: 113 additions & 0 deletions server/e2e/integration_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,123 @@ import (
"net/http"
"testing"

"github.com/gavv/httpexpect/v2"
"github.com/reearth/reearth-cms/server/internal/app"
"github.com/reearth/reearth-cms/server/pkg/id"
)

// GET /projects/{projectIdOrKey}/schemata
func TestIntegrationScemaFilterAPI(t *testing.T) {
endpoint := "/api/projects/{projectIdOrKey}/schemata"
e := StartServer(t, &app.Config{}, true, baseSeeder)

e.GET(endpoint, id.NewProjectID()).
Expect().
Status(http.StatusUnauthorized)

e.GET(endpoint, id.NewProjectID()).
WithHeader("authorization", "secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.GET(endpoint, id.NewProjectID()).
WithHeader("authorization", "Bearer secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.GET(endpoint, id.NewProjectID()).
WithHeader("authorization", "Bearer "+secret).
WithQuery("page", 0).
WithQuery("perPage", 5).
Expect().
Status(http.StatusNotFound)

assertRes := func(t *testing.T, res *httpexpect.Response) {
t.Helper()

models := res.Status(http.StatusOK).
JSON().
Object().
HasValue("page", 1).
HasValue("perPage", 10).
HasValue("totalCount", 5).
Value("models").
Array()
models.Length().IsEqual(5)

obj1 := models.Value(0).Object()
obj1.
HasValue("id", mId1.String()).
HasValue("name", "m1").
HasValue("description", "m1 desc").
HasValue("public", true).
HasValue("key", ikey1.String()).
HasValue("projectId", pid).
HasValue("schemaId", sid1)

obj1.Value("createdAt").NotNull()
obj1.Value("updatedAt").NotNull()
obj1.Value("lastModified").NotNull()

obj2 := models.Value(1).Object()
obj2.
HasValue("id", mId2.String()).
HasValue("name", "m2").
HasValue("description", "m2 desc").
HasValue("public", true).
HasValue("key", ikey2.String()).
HasValue("projectId", pid).
HasValue("schemaId", sid2)

obj2.Value("createdAt").NotNull()
obj2.Value("updatedAt").NotNull()
obj2.Value("lastModified").NotNull()
}

assertRes(t, e.GET(endpoint, pid).
WithHeader("authorization", "Bearer "+secret).
WithQuery("page", 1).
WithQuery("perPage", 10).
Expect())

assertRes(t, e.GET(endpoint, palias).
WithHeader("authorization", "Bearer "+secret).
WithQuery("page", 1).
WithQuery("perPage", 10).
Expect())

res := e.GET(endpoint, palias).
WithHeader("authorization", "Bearer "+secret).
WithQuery("page", 1).
WithQuery("perPage", 10).
WithQuery("keyword", "m1").
Expect()

models := res.Status(http.StatusOK).
JSON().
Object().
HasValue("page", 1).
HasValue("perPage", 10).
HasValue("totalCount", 1).
Value("models").
Array()
models.Length().IsEqual(1)

obj1 := models.Value(0).Object()
obj1.
HasValue("id", mId1.String()).
HasValue("name", "m1").
HasValue("description", "m1 desc").
HasValue("public", true).
HasValue("key", ikey1.String()).
HasValue("projectId", pid).
HasValue("schemaId", sid1)

obj1.Value("createdAt").NotNull()
obj1.Value("updatedAt").NotNull()
obj1.Value("lastModified").NotNull()
}

// POST /api/models/{modelId}/fields
func TestIntegrationFieldCreateAPI(t *testing.T) {
endpoint := "/api/schemata/{schemaId}/fields"
Expand Down
39 changes: 39 additions & 0 deletions server/internal/adapter/integration/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,45 @@ var (
ErrInvalidTypeProperty = errors.New("invalid type property")
)

func (s *Server) SchemaFilter(ctx context.Context, request SchemaFilterRequestObject) (SchemaFilterResponseObject, error) {
uc := adapter.Usecases(ctx)
op := adapter.Operator(ctx)

prj, err := uc.Project.FindByIDOrAlias(ctx, request.ProjectIdOrAlias, op)
if err != nil {
if errors.Is(err, rerror.ErrNotFound) {
return SchemaFilter404Response{}, err
}
return nil, err
}

p := fromPagination(request.Params.Page, request.Params.PerPage)
ms, pi, err := uc.Model.FindByProjectAndKeyword(ctx, prj.ID(), lo.FromPtrOr(request.Params.Keyword, ""), p, op)
if err != nil {
return nil, err
}

models := make([]integrationapi.Model, 0, len(ms))
for _, m := range ms {
sp, err := uc.Schema.FindByModel(ctx, m.ID(), op)
if err != nil {
return nil, err
}
lastModified, err := uc.Item.LastModifiedByModel(ctx, m.ID(), op)
if err != nil && !errors.Is(err, rerror.ErrNotFound) {
return nil, err
}
models = append(models, integrationapi.NewModel(m, sp, lastModified))
}

return SchemaFilter200JSONResponse{
Models: &models,
Page: lo.ToPtr(Page(*p.Offset)),
PerPage: lo.ToPtr(int(p.Offset.Limit)),
TotalCount: lo.ToPtr(int(pi.TotalCount)),
}, nil
}

func (s *Server) FieldCreate(ctx context.Context, request FieldCreateRequestObject) (FieldCreateResponseObject, error) {
uc := adapter.Usecases(ctx)
op := adapter.Operator(ctx)
Expand Down
Loading

0 comments on commit 080b9ee

Please sign in to comment.