Skip to content

Commit

Permalink
imp
Browse files Browse the repository at this point in the history
  • Loading branch information
yk-eukarya committed Aug 1, 2024
1 parent d72b93e commit 073667d
Show file tree
Hide file tree
Showing 14 changed files with 612 additions and 409 deletions.
56 changes: 28 additions & 28 deletions server/e2e/integration_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import (

// POST /api/models/{modelId}/fields
func TestIntegrationFieldCreateAPI(t *testing.T) {
endpoint := "/api/models/{modelId}/fields"
endpoint := "/api/schemata/{schemaId}/fields"
e := StartServer(t, &app.Config{}, true, baseSeeder)

e.POST(endpoint, id.NewModelID()).
e.POST(endpoint, id.NewSchemaID()).
Expect().
Status(http.StatusUnauthorized)

e.POST(endpoint, id.NewModelID()).
e.POST(endpoint, id.NewSchemaID()).
WithHeader("authorization", "secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.POST(endpoint, id.NewModelID()).
e.POST(endpoint, id.NewSchemaID()).
WithHeader("authorization", "Bearer secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.POST(endpoint, id.NewModelID()).
e.POST(endpoint, id.NewSchemaID()).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusNotFound)
Expand All @@ -38,7 +38,7 @@ func TestIntegrationFieldCreateAPI(t *testing.T) {
Expect().
Status(http.StatusBadRequest)

obj1 := e.POST(endpoint, mId1).
obj1 := e.POST(endpoint, sid1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"key": "fKey1",
Expand Down Expand Up @@ -72,10 +72,10 @@ func TestIntegrationFieldCreateAPI(t *testing.T) {

// PATCH /api/models/{modelId}/fields/{FieldIdOrKey}
func TestIntegrationFieldUpdateAPI(t *testing.T) {
endpoint := "/api/models/{modelId}/fields/{fieldIdOrKey}"
endpoint := "/api/schemata/{schemaId}/fields/{fieldIdOrKey}"
e := StartServer(t, &app.Config{}, true, baseSeeder)

obj := e.POST("/api/models/{modelId}/fields", mId1).
obj := e.POST("/api/schemata/{schemaId}/fields", sid1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"key": "fKey1",
Expand All @@ -91,22 +91,22 @@ func TestIntegrationFieldUpdateAPI(t *testing.T) {
Expect().
Status(http.StatusUnauthorized)

e.PATCH(endpoint, id.NewModelID(), id.NewFieldID()).
e.PATCH(endpoint, id.NewSchemaID(), id.NewFieldID()).
WithHeader("authorization", "secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.PATCH(endpoint, id.NewModelID(), id.NewFieldID()).
e.PATCH(endpoint, id.NewSchemaID(), id.NewFieldID()).
WithHeader("authorization", "Bearer secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.PATCH(endpoint, id.NewModelID(), id.NewFieldID()).
e.PATCH(endpoint, id.NewSchemaID(), id.NewFieldID()).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusNotFound)

e.PATCH(endpoint, id.NewModelID(), fId).
e.PATCH(endpoint, id.NewSchemaID(), fId).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusNotFound)
Expand All @@ -117,7 +117,7 @@ func TestIntegrationFieldUpdateAPI(t *testing.T) {
Expect().
Status(http.StatusBadRequest)

e.PATCH(endpoint, mId1, fId).
e.PATCH(endpoint, sid1, fId).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"key": "fKey1Updated",
Expand All @@ -135,7 +135,7 @@ func TestIntegrationFieldUpdateAPI(t *testing.T) {
HasValue("multiple", true).
HasValue("required", true)

e.PATCH(endpoint, mId1, "fKey1Updated").
e.PATCH(endpoint, sid1, "fKey1Updated").
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"key": "fKey1Updated1",
Expand Down Expand Up @@ -174,10 +174,10 @@ func TestIntegrationFieldUpdateAPI(t *testing.T) {

// DELETE /api/models/{modelId}/fields/{FieldIdOrKey}
func TestIntegrationFieldDeleteAPI(t *testing.T) {
endpoint := "/api/models/{modelId}/fields/{fieldIdOrKey}"
endpoint := "/api/schemata/{schemaId}/fields/{fieldIdOrKey}"
e := StartServer(t, &app.Config{}, true, baseSeeder)

obj := e.POST("/api/models/{modelId}/fields", mId1).
obj := e.POST("/api/schemata/{schemaId}/fields", sid1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"key": "fKey1",
Expand All @@ -189,26 +189,26 @@ func TestIntegrationFieldDeleteAPI(t *testing.T) {
Status(http.StatusOK)
fId := obj.JSON().Object().Value("id").String().Raw()

e.DELETE(endpoint, id.NewModelID(), id.NewFieldID()).
e.DELETE(endpoint, id.NewSchemaID(), id.NewFieldID()).
Expect().
Status(http.StatusUnauthorized)

e.DELETE(endpoint, id.NewModelID(), id.NewFieldID()).
e.DELETE(endpoint, id.NewSchemaID(), id.NewFieldID()).
WithHeader("authorization", "secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.DELETE(endpoint, id.NewModelID(), id.NewFieldID()).
e.DELETE(endpoint, id.NewSchemaID(), id.NewFieldID()).
WithHeader("authorization", "Bearer secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.DELETE(endpoint, id.NewModelID(), id.NewFieldID()).
e.DELETE(endpoint, id.NewSchemaID(), id.NewFieldID()).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusNotFound)

e.DELETE(endpoint, id.NewModelID(), fId).
e.DELETE(endpoint, id.NewSchemaID(), fId).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusNotFound)
Expand All @@ -219,15 +219,15 @@ func TestIntegrationFieldDeleteAPI(t *testing.T) {
Expect().
Status(http.StatusBadRequest)

e.DELETE(endpoint, mId1, fId).
e.DELETE(endpoint, sid1, fId).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusOK).
JSON().
Object().
HasValue("id", fId)

obj = e.POST("/api/models/{modelId}/fields", mId1).
obj = e.POST("/api/schemata/{schemaId}/fields", sid1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"key": "fKey1",
Expand All @@ -239,7 +239,7 @@ func TestIntegrationFieldDeleteAPI(t *testing.T) {
Status(http.StatusOK)
fId = obj.JSON().Object().Value("id").String().Raw()

e.DELETE(endpoint, mId1, "fKey1").
e.DELETE(endpoint, sid1, "fKey1").
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusOK).
Expand Down Expand Up @@ -340,7 +340,7 @@ func TestIntegrationFieldUpdateWithProjectAPI(t *testing.T) {
endpoint := "/api/projects/{projectIdOrAlias}/models/{modelIdOrKey}/fields/{fieldIdOrKey}"
e := StartServer(t, &app.Config{}, true, baseSeeder)

obj := e.POST("/api/models/{modelId}/fields", mId1).
obj := e.POST("/api/schemata/{schemaId}/fields", sid1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"key": "fKey1",
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestIntegrationFieldDeleteWithProjectAPI(t *testing.T) {
endpoint := "/api/projects/{projectIdOrAlias}/models/{modelIdOrKey}/fields/{fieldIdOrKey}"
e := StartServer(t, &app.Config{}, true, baseSeeder)

obj := e.POST("/api/models/{modelId}/fields", mId1).
obj := e.POST("/api/schemata/{schemaId}/fields", sid1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"key": "fKey1",
Expand Down Expand Up @@ -480,7 +480,7 @@ func TestIntegrationFieldDeleteWithProjectAPI(t *testing.T) {
Object().
HasValue("id", fId)

obj = e.POST("/api/models/{modelId}/fields", mId1).
obj = e.POST("/api/schemata/{schemaId}/fields", sid1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"key": "fKey1",
Expand All @@ -500,7 +500,7 @@ func TestIntegrationFieldDeleteWithProjectAPI(t *testing.T) {
Object().
HasValue("id", fId)

obj = e.POST("/api/models/{modelId}/fields", mId1).
obj = e.POST("/api/schemata/{schemaId}/fields", sid1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"key": "fKey1",
Expand Down
41 changes: 35 additions & 6 deletions server/internal/adapter/integration/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ func (s *Server) ModelFilter(ctx context.Context, request ModelFilterRequestObje

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, lastModified))
models = append(models, integrationapi.NewModel(m, sp, lastModified))
}

return ModelFilter200JSONResponse{
Expand Down Expand Up @@ -73,12 +77,17 @@ func (s *Server) ModelCreate(ctx context.Context, request ModelCreateRequestObje
return ModelCreate400Response{}, err
}

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
}

return ModelCreate200JSONResponse(integrationapi.NewModel(m, lastModified)), nil
return ModelCreate200JSONResponse(integrationapi.NewModel(m, sp, lastModified)), nil
}

func (s *Server) ModelGet(ctx context.Context, request ModelGetRequestObject) (ModelGetResponseObject, error) {
Expand All @@ -90,12 +99,17 @@ func (s *Server) ModelGet(ctx context.Context, request ModelGetRequestObject) (M
return nil, err
}

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

lastModified, err := uc.Item.LastModifiedByModel(ctx, request.ModelId, op)
if err != nil && !errors.Is(err, rerror.ErrNotFound) {
return nil, err
}

return ModelGet200JSONResponse(integrationapi.NewModel(m, lastModified)), nil
return ModelGet200JSONResponse(integrationapi.NewModel(m, sp, lastModified)), nil
}

func (s *Server) ModelGetWithProject(ctx context.Context, request ModelGetWithProjectRequestObject) (ModelGetWithProjectResponseObject, error) {
Expand All @@ -118,6 +132,11 @@ func (s *Server) ModelGetWithProject(ctx context.Context, request ModelGetWithPr
return ModelGetWithProject500Response{}, nil
}

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 {
if errors.Is(err, rerror.ErrNotFound) {
Expand All @@ -126,7 +145,7 @@ func (s *Server) ModelGetWithProject(ctx context.Context, request ModelGetWithPr
return ModelGetWithProject500Response{}, nil
}

return ModelGetWithProject200JSONResponse(integrationapi.NewModel(m, lastModified)), nil
return ModelGetWithProject200JSONResponse(integrationapi.NewModel(m, sp, lastModified)), nil
}

func (s *Server) ModelUpdate(ctx context.Context, request ModelUpdateRequestObject) (ModelUpdateResponseObject, error) {
Expand All @@ -148,12 +167,17 @@ func (s *Server) ModelUpdate(ctx context.Context, request ModelUpdateRequestObje
return ModelUpdate400Response{}, err
}

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

lastModified, err := uc.Item.LastModifiedByModel(ctx, request.ModelId, op)
if err != nil {
return nil, err
}

return ModelUpdate200JSONResponse(integrationapi.NewModel(m, lastModified)), nil
return ModelUpdate200JSONResponse(integrationapi.NewModel(m, sp, lastModified)), nil
}

func (s *Server) ModelUpdateWithProject(ctx context.Context, request ModelUpdateWithProjectRequestObject) (ModelUpdateWithProjectResponseObject, error) {
Expand Down Expand Up @@ -191,12 +215,17 @@ func (s *Server) ModelUpdateWithProject(ctx context.Context, request ModelUpdate
return ModelUpdateWithProject400Response{}, err
}

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 {
return nil, err
}

return ModelUpdateWithProject200JSONResponse(integrationapi.NewModel(m, lastModified)), nil
return ModelUpdateWithProject200JSONResponse(integrationapi.NewModel(m, sp, lastModified)), nil
}

func (s *Server) ModelDelete(ctx context.Context, request ModelDeleteRequestObject) (ModelDeleteResponseObject, error) {
Expand Down
26 changes: 18 additions & 8 deletions server/internal/adapter/integration/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,27 @@ func (s *Server) FieldCreate(ctx context.Context, request FieldCreateRequestObje
uc := adapter.Usecases(ctx)
op := adapter.Operator(ctx)

sch, err := uc.Schema.FindByModel(ctx, request.ModelId, op)
sch, err := uc.Schema.FindByID(ctx, request.SchemaId, op)
if err != nil {
if errors.Is(err, rerror.ErrNotFound) {
return FieldCreate400Response{}, err
}
return FieldCreate400Response{}, err
}

m, err := uc.Model.FindBySchema(ctx, sch.ID(), op)
if err != nil {
return FieldCreate400Response{}, err
}

tp, dv, err := FromSchemaTypeProperty(*request.Body.Type, *request.Body.Multiple)
if err != nil {
return nil, err
}

param := interfaces.CreateFieldParam{
ModelID: &request.ModelId,
SchemaID: sch.Schema().ID(),
ModelID: m.ID().Ref(),
SchemaID: sch.ID(),
Type: integrationapi.FromValueType(request.Body.Type),
Name: *request.Body.Key,
Description: nil,
Expand Down Expand Up @@ -130,14 +135,19 @@ func (s *Server) FieldUpdate(ctx context.Context, request FieldUpdateRequestObje
uc := adapter.Usecases(ctx)
op := adapter.Operator(ctx)

sch, err := uc.Schema.FindByModel(ctx, request.ModelId, op)
sch, err := uc.Schema.FindByID(ctx, request.SchemaId, op)
if err != nil {
if errors.Is(err, rerror.ErrNotFound) {
return FieldUpdate400Response{}, err
}
return FieldUpdate400Response{}, err
}

m, err := uc.Model.FindBySchema(ctx, sch.ID(), op)
if err != nil {
return FieldUpdate400Response{}, err
}

idOrKey := (*string)(&request.FieldIdOrKey)
f := sch.FieldByIDOrKey(id.FieldIDFromRef(idOrKey), id.NewKeyFromPtr(idOrKey))
if f == nil {
Expand All @@ -146,8 +156,8 @@ func (s *Server) FieldUpdate(ctx context.Context, request FieldUpdateRequestObje

param := interfaces.UpdateFieldParam{
FieldID: f.ID(),
ModelID: &request.ModelId,
SchemaID: sch.Schema().ID(),
ModelID: m.ID().Ref(),
SchemaID: sch.ID(),
Name: request.Body.Key,
Description: nil,
Key: request.Body.Key,
Expand Down Expand Up @@ -243,7 +253,7 @@ func (s *Server) FieldDelete(ctx context.Context, request FieldDeleteRequestObje
uc := adapter.Usecases(ctx)
op := adapter.Operator(ctx)

sch, err := uc.Schema.FindByModel(ctx, request.ModelId, op)
sch, err := uc.Schema.FindByID(ctx, request.SchemaId, op)
if err != nil {
if errors.Is(err, rerror.ErrNotFound) {
return FieldDelete400Response{}, err
Expand All @@ -257,7 +267,7 @@ func (s *Server) FieldDelete(ctx context.Context, request FieldDeleteRequestObje
return FieldDelete400Response{}, rerror.ErrNotFound
}

err = uc.Schema.DeleteField(ctx, sch.Schema().ID(), f.ID(), op)
err = uc.Schema.DeleteField(ctx, sch.ID(), f.ID(), op)
if err != nil {
return FieldDelete400Response{}, err
}
Expand Down
Loading

0 comments on commit 073667d

Please sign in to comment.