Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions internal/crowdactions/crowdaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
13 changes: 11 additions & 2 deletions pkg/handler/aws/crowdaction/crowdaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
4 changes: 2 additions & 2 deletions pkg/mocks/repository/dynamoManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 11 additions & 3 deletions pkg/repository/aws/crowdactionManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
16 changes: 8 additions & 8 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down