Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): implement public api for assets #1277

Merged
merged 9 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions server/e2e/publicapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,25 @@ func TestPublicAPI(t *testing.T) {
"error": "not found",
})

e.GET("/api/p/{project}/assets", publicAPIProjectAlias).
Expect().
Status(http.StatusOK).
JSON().
IsEqual(map[string]any{
"hasMore": false,
"limit": 50,
"offset": 0,
"page": 1,
"results": []map[string]any{
map[string]any{
"id": publicAPIAsset1ID.String(),
"type": "asset",
"url": fmt.Sprintf("https://example.com/assets/%s/%s/aaa.zip", publicAPIAssetUUID[:2], publicAPIAssetUUID[2:]),
},
},
"totalCount": 1,
})

e.GET("/api/p/{project}/assets/{assetid}", publicAPIProjectAlias, publicAPIAsset1ID).
Expect().
Status(http.StatusOK).
Expand All @@ -211,9 +230,6 @@ func TestPublicAPI(t *testing.T) {
"type": "asset",
"id": publicAPIAsset1ID.String(),
"url": fmt.Sprintf("https://example.com/assets/%s/%s/aaa.zip", publicAPIAssetUUID[:2], publicAPIAssetUUID[2:]),
"files": []string{
fmt.Sprintf("https://example.com/assets/%s/%s/aaa/bbb.txt", publicAPIAssetUUID[:2], publicAPIAssetUUID[2:]),
},
})

// make the project's assets private
Expand Down Expand Up @@ -418,7 +434,7 @@ func publicAPISeeder(ctx context.Context, r *repo.Container) error {

lo.Must0(r.Project.Save(ctx, p1))
lo.Must0(r.Asset.Save(ctx, a))
lo.Must0(r.AssetFile.Save(ctx, a.ID(), af))
lo.Must0(r.AssetFile.Save(ctx, a.ID(), af.Clone()))
lo.Must0(r.Schema.Save(ctx, s))
lo.Must0(r.Model.Save(ctx, m))
lo.Must0(r.Item.Save(ctx, i1))
Expand Down
24 changes: 15 additions & 9 deletions server/internal/adapter/publicapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,34 @@ func PublicApiItemList() echo.HandlerFunc {
ctx := c.Request().Context()
ctrl := GetController(ctx)

mKey := c.Param("model")
pKey := c.Param("project")
p, err := listParamFromEchoContext(c)
if err != nil {
return c.JSON(http.StatusBadRequest, "invalid offset or limit")
}

resType := ""
m := c.Param("model")
if strings.Contains(m, ".") {
m, resType, _ = strings.Cut(m, ".")
if strings.Contains(mKey, ".") {
mKey, resType, _ = strings.Cut(mKey, ".")
}
if resType != "csv" && resType != "json" && resType != "geojson" {
resType = "json"
}

items, _, err := ctrl.GetItems(ctx, c.Param("project"), m, p)
var res any
if mKey == "assets" {
res, err = ctrl.GetAssets(ctx, pKey, p)
} else {
res, _, err = ctrl.GetItems(ctx, pKey, mKey, p)
}
if err != nil {
return err
}

vi, s, err1 := ctrl.GetVersionedItems(ctx, c.Param("project"), m, p)
if err1 != nil {
return err1
vi, s, err := ctrl.GetVersionedItems(ctx, pKey, mKey, p)
if mKey != "assets" && err != nil {
return err
}

switch resType {
Expand All @@ -94,9 +100,9 @@ func PublicApiItemList() echo.HandlerFunc {
case "geojson":
return toGeoJSON(c, vi, s)
case "json":
return c.JSON(http.StatusOK, items)
return c.JSON(http.StatusOK, res)
default:
return c.JSON(http.StatusOK, items)
return c.JSON(http.StatusOK, res)
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions server/internal/adapter/publicapi/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"context"
"errors"

"github.com/reearth/reearth-cms/server/internal/usecase/interfaces"
"github.com/reearth/reearth-cms/server/pkg/asset"
"github.com/reearth/reearth-cms/server/pkg/id"
"github.com/reearth/reearth-cms/server/pkg/project"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/util"
)

func (c *Controller) GetAsset(ctx context.Context, prj, i string) (Asset, error) {
Expand Down Expand Up @@ -34,3 +38,40 @@ func (c *Controller) GetAsset(ctx context.Context, prj, i string) (Asset, error)

return NewAsset(a, f, c.assetUrlResolver), nil
}

func (c *Controller) GetAssets(ctx context.Context, pKey string, p ListParam) (ListResult[Asset], error) {
prj, err := c.checkProject(ctx, pKey)
if err != nil {
return ListResult[Asset]{}, err
}

if prj.Publication().Scope() != project.PublicationScopePublic || !prj.Publication().AssetPublic() {
return ListResult[Asset]{}, rerror.ErrNotFound
}
nourbalaha marked this conversation as resolved.
Show resolved Hide resolved

a, pi, err := c.usecases.Asset.FindByProject(ctx, prj.ID(), interfaces.AssetFilter{
Sort: nil,
Keyword: nil,
Pagination: p.Pagination,
}, nil)

if err != nil {
if errors.Is(err, rerror.ErrNotFound) {
return ListResult[Asset]{}, rerror.ErrNotFound
}
return ListResult[Asset]{}, err
}
nourbalaha marked this conversation as resolved.
Show resolved Hide resolved

res, err := util.TryMap(a, func(i *asset.Asset) (Asset, error) {
f, err := c.usecases.Asset.FindFileByID(ctx, i.ID(), nil)
if err != nil {
return Asset{}, err
}
return NewAsset(i, f, c.assetUrlResolver), nil
})
nourbalaha marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return ListResult[Asset]{}, err
}

return NewListResult(res, pi, p.Pagination), nil
}
2 changes: 1 addition & 1 deletion server/internal/adapter/publicapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func NewAsset(a *asset.Asset, f *asset.File, urlResolver asset.URLResolver) Asse
base, _ := url.Parse(u)
base.Path = path.Dir(base.Path)

files = lo.Map(f.FlattenChildren(), func(f *asset.File, _ int) string {
files = lo.Map(f.Files(), func(f *asset.File, _ int) string {
b := *base
b.Path = path.Join(b.Path, f.Path())
return b.String()
Expand Down
Loading