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
Show all changes
43 commits
Select commit Hold shift + click to select a range
ae8e84d
🚀 crowdaction hex refactoring init
edreinoso Feb 5, 2022
ac8387f
🚧 dynamodb manager repository, pt1: init
edreinoso Feb 5, 2022
7ae52c7
⚡️ launching own sam config
edreinoso Feb 25, 2022
f0656c4
Merge branch 'development' into feat/gh-73/crowdactions
edreinoso Feb 25, 2022
1eae809
Merge branch 'development' into feat/gh-73/crowdactions
edreinoso Feb 25, 2022
9963992
🚧 [crowdaction] deployed first function on to aws
edreinoso Feb 26, 2022
605e564
🚧 [crowdaction] removing extra copy of dynamo repository
edreinoso Feb 26, 2022
0423b32
🚧 [crowdaction] init model and validation on request
edreinoso Feb 26, 2022
d7daae2
🚧 [crowdaction] changed the model function names
edreinoso Mar 4, 2022
e684acf
🚧 [crowdaction] working on internal and handler
edreinoso Mar 9, 2022
32666f8
🚧 [crowdaction] simple version of GET request by ID
edreinoso Mar 10, 2022
c25eec9
🚧 [crowdaction] testing GET request by ID
edreinoso Mar 11, 2022
bae64a5
🚧 [crowdaction] creating external function to GET crowdaction by ID
edreinoso Mar 11, 2022
3d5c1a5
🚧 [crowdaction] including query function in ddb repository
edreinoso Mar 19, 2022
33a7ab1
🚧 [crowdaction] initial code base for GET crowdactions by status
edreinoso Mar 19, 2022
b555b09
🚧 [crowdaction] cleaning up, commitment options
edreinoso Mar 19, 2022
841b968
🚧 [crowdaction] including the mock repository for dynamo and crowdaction
edreinoso Mar 20, 2022
483e006
🚧 [crowdaction] modifying the mock repository to include GetCrowdacti…
edreinoso Mar 20, 2022
9ce295a
🚧 [crowdaction] removed unnecessary comments, per the feedback on pul…
edreinoso Mar 20, 2022
8c5c280
[pull request] removing samconfig file from history
edreinoso Mar 23, 2022
99f52ef
[pull request] ignoring samconfig file as per the pull request
edreinoso Mar 23, 2022
4dc7789
[pull request] modified naming for the internal interface
edreinoso Mar 23, 2022
18aa963
[pull request] getting rid of useless else statement
edreinoso Mar 23, 2022
8fb01c0
[pull request] removing dynamodb dependencies in logic
edreinoso Mar 24, 2022
a47a23b
[pull request] including mock repository and crowdaction test
edreinoso Mar 24, 2022
39c7417
[pull request] cleaning up some unnecessary lines
edreinoso Mar 24, 2022
525b2b4
[pull request] separating repository concerns
edreinoso Mar 28, 2022
595b737
[pull request] modifying test cases with new repository structure
edreinoso Mar 28, 2022
98f45af
[pull request] changing dynamo repository call from crowdaction main …
edreinoso Mar 28, 2022
45cb593
[pull request] changing parameters to pass unit test case
edreinoso Mar 28, 2022
93138e2
[pull request] changing crowdactionID from test case
edreinoso Mar 28, 2022
f03c63a
[pull request] changing method call for crowdaction test
edreinoso Mar 28, 2022
8449139
[pull request] fixing parameter issue in crowdaction_test
edreinoso Mar 29, 2022
14f671a
[pull request] testing issue with the args.get(x) call in mock/dynamo…
edreinoso Mar 29, 2022
09ed55a
[pull request] including Mock in each of the function calls from the …
edreinoso Mar 29, 2022
589a25c
[pull request] modifying test cases for crowdaction
edreinoso Mar 29, 2022
72c469f
[pull request] changes done based on feedback
edreinoso Mar 23, 2022
11d5e7f
Merge branch 'feat/gh-73/crowdactions' of github.com:CollActionteam/c…
edreinoso Mar 29, 2022
4c3e35e
[pull request] removing unnecessary hello world
edreinoso Apr 2, 2022
4c44370
[pull request] removing some unnecessary comments
edreinoso Apr 2, 2022
77bed7e
pointing system for the commitments in crowdactions
edreinoso Apr 8, 2022
9858c8b
Merge branch 'development' of github.com:CollActionteam/collaction_ba…
edreinoso Apr 8, 2022
4459c00
Merge branch 'feat/gh-73/crowdactions' of github.com:CollActionteam/c…
edreinoso Apr 8, 2022
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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ _ignored/
.vscode/launch.json
.vscode/extensions.json
*.code-workspace

*.gitconfig
# VisualStudioCode Patch
# Ignore all local history of files
.history
.ionide

# MacOS
.DS_Store
.idea
.idea

samconfig.yaml
4 changes: 4 additions & 0 deletions docs/api2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,9 @@ components:
label:
type: string
example: Becoming vegetarian
points:
type: int
example: 30
description:
type: string
example: I will not eat any meat from any animal (including fish).
Expand All @@ -1157,6 +1160,7 @@ components:
id: no-beef
label: Not eating beef
description: I will avoid eating beef (Goodbye stake).
points: 30
Date:
type: string
pattern: '\d{4}-\d{2}-\d{2}'
Expand Down
42 changes: 42 additions & 0 deletions internal/crowdactions/crowdaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package crowdaction

import (
"context"
"fmt"

m "github.com/CollActionteam/collaction_backend/internal/models"
"github.com/CollActionteam/collaction_backend/utils"
)

type Service interface {
GetCrowdactionById(ctx context.Context, crowdactionId string) (*m.CrowdactionData, error)
GetCrowdactionsByStatus(ctx context.Context, status string, startFrom *utils.PrimaryKey) ([]m.CrowdactionData, error)
}
type CrowdactionManager interface {
GetById(pk string, crowdactionId string) (*m.CrowdactionData, error)
GetByStatus(filterCond string, startFrom *utils.PrimaryKey) ([]m.CrowdactionData, error)
}

const (
KeyDateStart = "date_start"
KeyDateEnd = "date_end"
KeyDateJoinBefore = "date_limit_join"
)

type crowdactionService struct {
crowdactionRepository CrowdactionManager
}

func NewCrowdactionService(crowdactionRepository CrowdactionManager) Service {
return &crowdactionService{crowdactionRepository: crowdactionRepository}
}

func (e *crowdactionService) GetCrowdactionById(ctx context.Context, crowdactionID string) (*m.CrowdactionData, error) {
fmt.Println("GetCrowdactionById", crowdactionID)
return e.crowdactionRepository.GetById(utils.PKCrowdaction, crowdactionID)
}

func (e *crowdactionService) GetCrowdactionsByStatus(ctx context.Context, status string, startFrom *utils.PrimaryKey) ([]m.CrowdactionData, error) {
fmt.Println("GetCrowdactionsByStatus", status, startFrom)
return e.crowdactionRepository.GetByStatus(status, startFrom)
}
36 changes: 36 additions & 0 deletions internal/crowdactions/crowdaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package crowdaction_test

import (
"context"
"fmt"
"testing"

cwd "github.com/CollActionteam/collaction_backend/internal/crowdactions"
m "github.com/CollActionteam/collaction_backend/internal/models"
"github.com/CollActionteam/collaction_backend/pkg/mocks/repository"

"github.com/CollActionteam/collaction_backend/utils"
"github.com/stretchr/testify/assert"
)

func TestCrowdaction_GetCrowdactionById(t *testing.T) {
as := assert.New(t)
dynamoRepository := &repository.Dynamo{}
var ctx context.Context
var crowdactions *m.CrowdactionData
crowdactionID := "sustainability#food#185f66fd"

t.Run("dev stage", func(t *testing.T) {
dynamoRepository.On("GetById", utils.PKCrowdaction, crowdactionID).Return(crowdactions, nil).Once()

service := cwd.NewCrowdactionService(dynamoRepository)

crowdaction, err := service.GetCrowdactionById(ctx, crowdactionID)

fmt.Println("Hello world", crowdaction)

as.NoError(err)

dynamoRepository.AssertExpectations(t)
})
}
9 changes: 9 additions & 0 deletions internal/models/commitment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package models

type CommitmentOption struct {
Id string `json:"id"`
Label string `json:"label"`
Description string `json:"description"`
Requires []CommitmentOption `json:"requires,omitempty"`
Points int `json:"points"`
}
32 changes: 32 additions & 0 deletions internal/models/crowdaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package models

type CrowdactionRequest struct {
Data CrowdactionData `json:"data" validate:"required"`
}

type CrowdactionParticipant struct {
Name string `json:"name,omitempty"`
UserID string `json:"userID,omitempty"`
}

type CrowdactionImages struct {
Card string `json:"card,omitempty"`
Banner string `json:"banner,omitempty"`
}

type CrowdactionData struct {
Copy link
Copy Markdown
Collaborator

@rubenhorn rubenhorn Apr 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CrowdactionData structure should have a field badge_thresholds of type []int.

CrowdactionID string `json:"crowdactionID"`
Title string `json:"title"`
Description string `json:"description"`
Category string `json:"category"`
Subcategory string `json:"subcategory"`
Location string `json:"location"`
DateStart string `json:"date_start"`
DateEnd string `json:"date_end"`
DateLimitJoin string `json:"date_limit_join"`
PasswordJoin string `json:"password_join"`
ParticipationCount int `json:"participant_count"`
TopParticipants []CrowdactionParticipant `json:"top_participants"`
Images CrowdactionImages `json:"images"`
CommitmentOptions []CommitmentOption `json:"commitment_options"`
}
76 changes: 76 additions & 0 deletions pkg/handler/aws/crowdaction/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"context"
"encoding/json"
"net/http"

cwd "github.com/CollActionteam/collaction_backend/internal/crowdactions"
"github.com/CollActionteam/collaction_backend/internal/models"
hnd "github.com/CollActionteam/collaction_backend/pkg/handler"
awsRepository "github.com/CollActionteam/collaction_backend/pkg/repository/aws"
"github.com/CollActionteam/collaction_backend/utils"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/go-playground/validator/v10"
)

func getCrowdactionByID(ctx context.Context, crowdactionID string) (events.APIGatewayV2HTTPResponse, error) {
dynamoRepository := awsRepository.NewCrowdaction(awsRepository.NewDynamo())
getCrowdaction, err := cwd.NewCrowdactionService(dynamoRepository).GetCrowdactionById(ctx, crowdactionID)

if err != nil {
return utils.CreateMessageHttpResponse(http.StatusInternalServerError, err.Error()), nil
}
if getCrowdaction == nil {
return utils.CreateMessageHttpResponse(http.StatusNotFound, "not participating"), nil
}

jsonPayload, _ := json.Marshal(getCrowdaction)
return events.APIGatewayV2HTTPResponse{
Body: string(jsonPayload),
StatusCode: http.StatusOK,
}, nil
}

func getCrowdactionsByStatus(ctx context.Context, status string) (events.APIGatewayV2HTTPResponse, error) {
dynamoRepository := awsRepository.NewCrowdaction(awsRepository.NewDynamo())
getCrowdactions, err := cwd.NewCrowdactionService(dynamoRepository).GetCrowdactionsByStatus(ctx, status, nil)

if err != nil {
return utils.CreateMessageHttpResponse(http.StatusInternalServerError, err.Error()), nil
}
jsonPayload, _ := json.Marshal(getCrowdactions)

return events.APIGatewayV2HTTPResponse{
Body: string(jsonPayload),
StatusCode: http.StatusOK,
}, nil
}

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
crowdactionID := req.PathParameters["crowdactionID"]
var request models.CrowdactionRequest

validate := validator.New()
if err := validate.StructCtx(ctx, request); err != nil {
body, _ := json.Marshal(hnd.Response{Status: hnd.StatusFail, Data: map[string]interface{}{"error": utils.ValidationResponse(err, validate)}})
return events.APIGatewayV2HTTPResponse{Body: string(body), StatusCode: http.StatusBadRequest}, nil
}

if crowdactionID == "" {
status := req.QueryStringParameters["status"]
return getCrowdactionsByStatus(ctx, status)
}

return getCrowdactionByID(ctx, crowdactionID)
}

func main() {
lambda.Start(handler)
}

func errToResponse(err error, code int) events.APIGatewayV2HTTPResponse {
msg, _ := json.Marshal(map[string]string{"message": err.Error()})
return events.APIGatewayV2HTTPResponse{Body: string(msg), StatusCode: code}
}
3 changes: 2 additions & 1 deletion pkg/handler/aws/emailContact/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"context"
"encoding/json"
"net/http"

"github.com/CollActionteam/collaction_backend/internal/contact"
"github.com/CollActionteam/collaction_backend/internal/models"
hnd "github.com/CollActionteam/collaction_backend/pkg/handler"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/go-playground/validator/v10"
"net/http"
)

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
Expand Down
21 changes: 21 additions & 0 deletions pkg/mocks/repository/dynamoManager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package repository

import (
m "github.com/CollActionteam/collaction_backend/internal/models"
"github.com/CollActionteam/collaction_backend/utils"
"github.com/stretchr/testify/mock"
)

type Dynamo struct {
mock.Mock
}

func (d *Dynamo) GetById(pk string, sk string) (*m.CrowdactionData, error) {
args := d.Mock.Called(pk, sk)
return args.Get(0).(*m.CrowdactionData), args.Error(1)
}

func (d *Dynamo) GetByStatus(filterCond string, startFrom *utils.PrimaryKey) ([]m.CrowdactionData, error) {
args := d.Mock.Called(filterCond, startFrom)
return args.Get(0).([]m.CrowdactionData), args.Error(1)
}
89 changes: 89 additions & 0 deletions pkg/repository/aws/crowdactionManager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package aws

import (
"fmt"

"github.com/CollActionteam/collaction_backend/internal/constants"
m "github.com/CollActionteam/collaction_backend/internal/models"
"github.com/CollActionteam/collaction_backend/utils"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
)

type Crowdaction interface {
GetById(pk string, sk string) (*m.CrowdactionData, error)
GetByStatus(status string, startFrom *utils.PrimaryKey) ([]m.CrowdactionData, error)
}

const (
KeyDateStart = "date_start"
KeyDateEnd = "date_end"
KeyDateJoinBefore = "date_limit_join"
)

type crowdaction struct {
dbClient *Dynamo
}

func NewCrowdaction(dynamo *Dynamo) Crowdaction {
return &crowdaction{dbClient: dynamo}
}

/**
GET Crowdaction by Id
**/
func (s *crowdaction) GetById(pk string, sk string) (*m.CrowdactionData, error) {
item, err := s.dbClient.GetDBItem(constants.TableName, pk, sk)

if item == nil || err != nil {
return nil, err
}

var c m.CrowdactionData
err = dynamodbattribute.UnmarshalMap(item, &c)

return &c, err
}

/**
GET Crowdaction by Status
**/
func (s *crowdaction) GetByStatus(status string, startFrom *utils.PrimaryKey) ([]m.CrowdactionData, error) {
crowdactions := []m.CrowdactionData{}
var filterCond expression.ConditionBuilder

switch status {
case "joinable":
filterCond = expression.Name(KeyDateJoinBefore).GreaterThan(expression.Value(utils.GetDateStringNow()))
fmt.Println("GetByStatus: joinable", filterCond)
case "active":
filterCond = expression.Name(KeyDateStart).LessThanEqual(expression.Value(utils.GetDateStringNow()))
fmt.Println("GetByStatus: active", filterCond)
case "ended":
filterCond = expression.Name(KeyDateEnd).LessThanEqual(expression.Value(utils.GetDateStringNow()))
fmt.Println("GetByStatus: ended", filterCond)
default:
fmt.Println("None of the edge cases matched")
}

items, err := s.dbClient.Query(constants.TableName, filterCond, startFrom)

if items == nil || err != nil {
return nil, err
}

for _, foo := range items {
var crowdaction m.CrowdactionData
err := dynamodbattribute.UnmarshalMap(foo, &crowdaction)

if err == nil {
crowdactions = append(crowdactions, crowdaction)
}
}

if len(items) != len(crowdactions) {
err = fmt.Errorf("error unmarshelling %d items", len(items)-len(crowdactions))
}

return crowdactions, err
}
Loading