diff --git a/internal/crowdactions/crowdaction.go b/internal/crowdactions/crowdaction.go index df7ca6c..3bc5b34 100644 --- a/internal/crowdactions/crowdaction.go +++ b/internal/crowdactions/crowdaction.go @@ -11,13 +11,13 @@ type Service interface { GetAllCrowdactions(ctx context.Context) ([]m.CrowdactionData, error) GetCrowdactionById(ctx context.Context, crowdactionId string) (*m.CrowdactionData, error) GetCrowdactionsByStatus(ctx context.Context, status string, startFrom *utils.PrimaryKey) ([]m.CrowdactionData, error) - RegisterCrowdaction(ctx context.Context, payload m.CrowdactionData) error + RegisterCrowdaction(ctx context.Context, payload m.CrowdactionData) (*m.CrowdactionData, error) } type CrowdactionManager interface { GetAll() ([]m.CrowdactionData, error) GetById(pk string, crowdactionId string) (*m.CrowdactionData, error) GetByStatus(filterCond string, startFrom *utils.PrimaryKey) ([]m.CrowdactionData, error) - Register(ctx context.Context, payload m.CrowdactionData) error + Register(ctx context.Context, payload m.CrowdactionData) (*m.CrowdactionData, error) } const ( @@ -46,9 +46,11 @@ func (e *crowdactionService) GetCrowdactionsByStatus(ctx context.Context, status return e.crowdactionRepository.GetByStatus(status, startFrom) } -func (e *crowdactionService) RegisterCrowdaction(ctx context.Context, payload m.CrowdactionData) error { - if err := e.crowdactionRepository.Register(ctx, payload); err != nil { - return err +func (e *crowdactionService) RegisterCrowdaction(ctx context.Context, payload m.CrowdactionData) (*m.CrowdactionData, error) { + res, err := e.crowdactionRepository.Register(ctx, payload) + + if err != nil { + return res, err } - return nil // should this be ultimately be nil? + return res, err } diff --git a/pkg/handler/aws/crowdaction/crowdaction.go b/pkg/handler/aws/crowdaction/crowdaction.go index 2bfd129..83e7deb 100644 --- a/pkg/handler/aws/crowdaction/crowdaction.go +++ b/pkg/handler/aws/crowdaction/crowdaction.go @@ -28,11 +28,20 @@ func (c *CrowdactionHandler) createCrowdaction(ctx context.Context, req events.A if err := json.Unmarshal([]byte(req.Body), &payload); err != nil { return utils.CreateMessageHttpResponse(http.StatusInternalServerError, err.Error()), nil } - if err := c.service.RegisterCrowdaction(ctx, payload); err != nil { + + res, err := c.service.RegisterCrowdaction(ctx, payload) + + if err != nil { return utils.CreateMessageHttpResponse(http.StatusInternalServerError, err.Error()), nil + } // return call to client - return utils.CreateMessageHttpResponse(http.StatusOK, "Crowdaction has been recorded"), nil + body, _ := json.Marshal(hnd.Response{Status: hnd.StatusSuccess, Data: res}) + + return events.APIGatewayV2HTTPResponse{ + Body: string(body), + StatusCode: http.StatusOK, + }, nil } /** diff --git a/pkg/mocks/repository/dynamoManager.go b/pkg/mocks/repository/dynamoManager.go index 134b778..e5a725a 100644 --- a/pkg/mocks/repository/dynamoManager.go +++ b/pkg/mocks/repository/dynamoManager.go @@ -27,7 +27,7 @@ func (d *Dynamo) GetByStatus(filterCond string, startFrom *utils.PrimaryKey) ([] return args.Get(0).([]m.CrowdactionData), args.Error(1) } -func (d *Dynamo) Register(ctx context.Context, payload m.CrowdactionData) error { +func (d *Dynamo) Register(ctx context.Context, payload m.CrowdactionData) (*m.CrowdactionData, error) { d.Mock.Called(payload) - return nil + return &payload, nil } diff --git a/pkg/repository/aws/crowdactionManager.go b/pkg/repository/aws/crowdactionManager.go index f09655e..3f69fc5 100644 --- a/pkg/repository/aws/crowdactionManager.go +++ b/pkg/repository/aws/crowdactionManager.go @@ -17,7 +17,8 @@ type Crowdaction interface { GetAll() ([]m.CrowdactionData, error) GetById(pk string, sk string) (*m.CrowdactionData, error) GetByStatus(status string, startFrom *utils.PrimaryKey) ([]m.CrowdactionData, error) - Register(ctx context.Context, payload m.CrowdactionData) error + Register(ctx context.Context, payload m.CrowdactionData) (*m.CrowdactionData, error) + // Register(ctx context.Context, payload m.CrowdactionData) error } const ( @@ -125,14 +126,21 @@ func (s *crowdaction) GetByStatus(status string, startFrom *utils.PrimaryKey) ([ return crowdactions, err } -func (s *crowdaction) Register(ctx context.Context, payload m.CrowdactionData) error { +func (s *crowdaction) Register(ctx context.Context, payload m.CrowdactionData) (*m.CrowdactionData, error) { + var response m.CrowdactionData generatedID := RandomIDPrefix(8) pk := utils.PKCrowdaction sk := payload.Category + "#" + payload.Subcategory + "#" + generatedID payload.CrowdactionID = sk + response = payload // should modify the payload here to include the crowdcationID // fmt.Println("payload", payload) fmt.Println("9. pkg/respository/aws/crowdactionManager.go", payload) - return s.dbClient.PutDBItem(constants.TableName, pk, sk, payload) + err := s.dbClient.PutDBItem(constants.TableName, pk, sk, payload) + + if err != nil { + return &response, nil + } + return &response, err } diff --git a/template.yaml b/template.yaml index 09782c2..9b76c4c 100644 --- a/template.yaml +++ b/template.yaml @@ -282,14 +282,14 @@ Resources: Runtime: go1.x Events: # TODO feature wip (cms) - # CreateCrowdaction: - # Type: HttpApi - # Properties: - # Path: /cms/crowdactions - # Method: post - # ApiId: !Ref HttpApi - # Auth: - # Authorizer: "NONE" + CreateCrowdaction: + Type: HttpApi + Properties: + Path: /cms/crowdactions + Method: post + ApiId: !Ref HttpApi + Auth: + Authorizer: "NONE" FetchCrowdaction: Type: HttpApi Properties: