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

chore(server): move export csv items and GeoJSON ability to Item Usecase Layer #1347

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
59 changes: 16 additions & 43 deletions server/internal/adapter/integration/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package integration
import (
"context"
"errors"
"io"

"github.com/reearth/reearth-cms/server/internal/usecase"
"github.com/reearth/reearth-cms/server/pkg/model"
Expand Down Expand Up @@ -74,57 +73,44 @@ func (s *Server) ItemsAsGeoJSON(ctx context.Context, request ItemsAsGeoJSONReque
op := adapter.Operator(ctx)
uc := adapter.Usecases(ctx)

sp, err := uc.Schema.FindByModel(ctx, request.ModelId, op)
schemaPackage, err := uc.Schema.FindByModel(ctx, request.ModelId, op)
if err != nil {
return ItemsAsGeoJSON400Response{}, err
return ItemsAsGeoJSON404Response{}, err
jasonkarel marked this conversation as resolved.
Show resolved Hide resolved
}

p := fromPagination(request.Params.Page, request.Params.PerPage)
items, _, err := uc.Item.FindBySchema(ctx, sp.Schema().ID(), nil, p, op)
featureCollections, err := uc.Item.ItemsAsGeoJSON(ctx, schemaPackage, request.Params.Page, request.Params.PerPage, op)
if err != nil {
if errors.Is(err, rerror.ErrNotFound) {
return ItemsAsGeoJSON404Response{}, err
}
return ItemsAsGeoJSON400Response{}, err
}

fc, err := featureCollectionFromItems(items, sp.Schema())
if err != nil {
return ItemsAsGeoJSON400Response{}, err
}

return ItemsAsGeoJSON200JSONResponse{
Features: fc.Features,
Type: fc.Type,
Features: featureCollections.FeatureCollections.Features,
Type: featureCollections.FeatureCollections.Type,
}, nil
}

func (s *Server) ItemsAsCSV(ctx context.Context, request ItemsAsCSVRequestObject) (ItemsAsCSVResponseObject, error) {
op := adapter.Operator(ctx)
uc := adapter.Usecases(ctx)

sp, err := uc.Schema.FindByModel(ctx, request.ModelId, op)
schemaPackage, err := uc.Schema.FindByModel(ctx, request.ModelId, op)
if err != nil {
return ItemsAsCSV400Response{}, err
return ItemsAsCSV404Response{}, err
}

p := fromPagination(request.Params.Page, request.Params.PerPage)
items, _, err := uc.Item.FindBySchema(ctx, sp.Schema().ID(), nil, p, op)
pr, err := uc.Item.ItemsAsCSV(ctx, schemaPackage, request.Params.Page, request.Params.PerPage, op)
if err != nil {
if errors.Is(err, rerror.ErrNotFound) {
return ItemsAsCSV404Response{}, err
}
return ItemsAsCSV400Response{}, err
}

pr, pw := io.Pipe()
err = csvFromItems(pw, items, sp.Schema())
if err != nil {
return ItemsAsCSV400Response{}, err
}

return ItemsAsCSV200TextcsvResponse{
Body: pr,
Body: pr.PipeReader,
}, nil
}

Expand Down Expand Up @@ -217,28 +203,22 @@ func (s *Server) ItemsWithProjectAsGeoJSON(ctx context.Context, request ItemsWit
return ItemsWithProjectAsGeoJSON400Response{}, err
}

sp, err := uc.Schema.FindByModel(ctx, m.ID(), op)
schemaPackage, err := uc.Schema.FindByModel(ctx, m.ID(), op)
if err != nil {
return ItemsWithProjectAsGeoJSON400Response{}, err
}

p := fromPagination(request.Params.Page, request.Params.PerPage)
items, _, err := uc.Item.FindBySchema(ctx, sp.Schema().ID(), nil, p, op)
featureCollections, err := uc.Item.ItemsAsGeoJSON(ctx, schemaPackage, request.Params.Page, request.Params.PerPage, op)
if err != nil {
if errors.Is(err, rerror.ErrNotFound) {
return ItemsWithProjectAsGeoJSON404Response{}, err
}
return ItemsWithProjectAsGeoJSON400Response{}, err
}

fc, err := featureCollectionFromItems(items, sp.Schema())
if err != nil {
return ItemsWithProjectAsGeoJSON400Response{}, err
}

return ItemsWithProjectAsGeoJSON200JSONResponse{
Features: fc.Features,
Type: fc.Type,
Features: featureCollections.FeatureCollections.Features,
Type: featureCollections.FeatureCollections.Type,
}, nil
}

Expand All @@ -262,28 +242,21 @@ func (s *Server) ItemsWithProjectAsCSV(ctx context.Context, request ItemsWithPro
return ItemsWithProjectAsCSV400Response{}, err
}

sp, err := uc.Schema.FindByModel(ctx, m.ID(), op)
schemaPackage, err := uc.Schema.FindByModel(ctx, m.ID(), op)
if err != nil {
return ItemsWithProjectAsCSV400Response{}, err
}

p := fromPagination(request.Params.Page, request.Params.PerPage)
items, _, err := uc.Item.FindBySchema(ctx, sp.Schema().ID(), nil, p, op)
pr, err := uc.Item.ItemsAsCSV(ctx, schemaPackage, request.Params.Page, request.Params.PerPage, op)
if err != nil {
if errors.Is(err, rerror.ErrNotFound) {
return ItemsWithProjectAsCSV404Response{}, err
}
return ItemsWithProjectAsCSV400Response{}, err
}

pr, pw := io.Pipe()
err = csvFromItems(pw, items, sp.Schema())
if err != nil {
return ItemsWithProjectAsCSV400Response{}, err
}

return ItemsWithProjectAsCSV200TextcsvResponse{
Body: pr,
Body: pr.PipeReader,
}, nil
}

Expand Down
82 changes: 82 additions & 0 deletions server/internal/usecase/interactor/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"time"

"github.com/reearth/reearth-cms/server/internal/usecase"
Expand All @@ -25,6 +26,9 @@ import (
"golang.org/x/exp/slices"
)

const maxPerPage = 100
const defaultPerPage int64 = 50

type Item struct {
repos *repo.Container
gateways *gateway.Container
Expand Down Expand Up @@ -1179,3 +1183,81 @@ func (i Item) getReferencedItems(ctx context.Context, fields []*item.Field) ([]i
}
return i.repos.Item.FindByIDs(ctx, ids, nil)
}

// ItemsAsCSV exports items data in content to csv file by schema package.
func (i Item) ItemsAsCSV(ctx context.Context, schemaPackage *schema.Package, page *int, perPage *int, operator *usecase.Operator) (interfaces.ExportItemsToCSVResponse, error) {
if operator.AcOperator.User == nil && operator.Integration == nil {
return interfaces.ExportItemsToCSVResponse{}, interfaces.ErrInvalidOperator
}
return Run1(ctx, operator, i.repos, Usecase().Transaction(), func(ctx context.Context) (interfaces.ExportItemsToCSVResponse, error) {

// fromPagination
paginationOffset := fromPagination(page, perPage)

items, _, err := i.repos.Item.FindBySchema(ctx, schemaPackage.Schema().ID(), nil, nil, paginationOffset)
if err != nil {
return interfaces.ExportItemsToCSVResponse{}, err
}

pr, pw := io.Pipe()
err = csvFromItems(pw, items, schemaPackage.Schema())
if err != nil {
return interfaces.ExportItemsToCSVResponse{}, err
}

return interfaces.ExportItemsToCSVResponse{
PipeReader: pr,
}, nil
})
}
jasonkarel marked this conversation as resolved.
Show resolved Hide resolved

// ItemsAsGeoJSON converts items to Geo JSON type given the schema package
func (i Item) ItemsAsGeoJSON(ctx context.Context, schemaPackage *schema.Package, page *int, perPage *int, operator *usecase.Operator) (interfaces.ExportItemsToGeoJSONResponse, error) {

if operator.AcOperator.User == nil && operator.Integration == nil {
return interfaces.ExportItemsToGeoJSONResponse{}, interfaces.ErrInvalidOperator
}

return Run1(ctx, operator, i.repos, Usecase().Transaction(), func(ctx context.Context) (interfaces.ExportItemsToGeoJSONResponse, error) {

// fromPagination
paginationOffset := fromPagination(page, perPage)

items, _, err := i.repos.Item.FindBySchema(ctx, schemaPackage.Schema().ID(), nil, nil, paginationOffset)
if err != nil {
return interfaces.ExportItemsToGeoJSONResponse{}, err
}

featureCollections, err := featureCollectionFromItems(items, schemaPackage.Schema())
if err != nil {
return interfaces.ExportItemsToGeoJSONResponse{}, err
}

return interfaces.ExportItemsToGeoJSONResponse{
FeatureCollections: featureCollections,
}, nil
})
}

func fromPagination(page, perPage *int) *usecasex.Pagination {
p := int64(1)
if page != nil && *page > 0 {
p = int64(*page)
}

pp := defaultPerPage
if perPage != nil {
if ppr := *perPage; 1 <= ppr {
if ppr > maxPerPage {
pp = int64(maxPerPage)
} else {
pp = int64(ppr)
}
}
}

return usecasex.OffsetPagination{
Offset: (p - 1) * pp,
Limit: pp,
}.Wrap()
}
62 changes: 62 additions & 0 deletions server/internal/usecase/interactor/item_export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package interactor

import (
"encoding/csv"
"io"

"github.com/labstack/gommon/log"
"github.com/reearth/reearth-cms/server/pkg/integrationapi"
"github.com/reearth/reearth-cms/server/pkg/item"
"github.com/reearth/reearth-cms/server/pkg/schema"
"github.com/reearth/reearthx/i18n"
"github.com/reearth/reearthx/rerror"
"github.com/samber/lo"
)

var (
pointFieldIsNotSupportedError = rerror.NewE(i18n.T("point type is not supported in any geometry field in this model"))
)

// GeoJSON
func featureCollectionFromItems(ver item.VersionedList, s *schema.Schema) (*integrationapi.FeatureCollection, error) {
return integrationapi.FeatureCollectionFromItems(ver, s)
}

// CSV
func csvFromItems(pw *io.PipeWriter, l item.VersionedList, s *schema.Schema) error {
if !s.IsPointFieldSupported() {
return pointFieldIsNotSupportedError
}
go handleCSVGeneration(pw, l, s)
return nil
}
jasonkarel marked this conversation as resolved.
Show resolved Hide resolved
func handleCSVGeneration(pw *io.PipeWriter, l item.VersionedList, s *schema.Schema) {
err := generateCSV(pw, l, s)
if err != nil {
log.Errorf("failed to generate CSV: %v", err)
_ = pw.CloseWithError(err)
} else {
_ = pw.Close()
}
}
func generateCSV(pw *io.PipeWriter, l item.VersionedList, s *schema.Schema) error {
w := csv.NewWriter(pw)
defer w.Flush()
headers := integrationapi.BuildCSVHeaders(s)
if err := w.Write(headers); err != nil {
return err
}
nonGeoFields := lo.Filter(s.Fields(), func(f *schema.Field, _ int) bool {
return !f.IsGeometryField()
})
for _, ver := range l {
row, ok := integrationapi.RowFromItem(ver.Value(), nonGeoFields)
if ok {
if err := w.Write(row); err != nil {
return err
}
}
}

return w.Error()
}
Loading
Loading