Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Get participations for crowdactions/profiles #94

Draft
wants to merge 8 commits into
base: development
Choose a base branch
from
Draft
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
119 changes: 115 additions & 4 deletions docs/api2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,70 @@ paths:
type: string
example:
password: crowdaction does not exist
"/crowdactions/{crowdactionID}/participants":
get:
tags:
- Crowdaction
summary: Get details of a specific crowdaction
parameters:
- $ref: "#/components/parameters/ApiVersionParameter"
- name: crowdactionID
in: path
required: true
schema:
type: string
- in: query
name: password
description: Only include if crowdaction requires password
required: false
schema:
type: string
format: password
responses:
"200":
description: Crowdaction details
content:
application/json:
schema:
type: object
properties:
status:
type: string
default: success
data:
type: array
items:
$ref: "#/components/schemas/Participation"
"401":
description: Unauthorized (Invalid password)
content:
application/json:
schema:
type: object
properties:
status:
type: string
default: fail
data:
type: string
example:
password: Invalid or missing password
"403":
$ref: "#/components/responses/UnsupportedClientVersion"
"404":
description: Crowdaction not found
content:
application/json:
schema:
type: object
properties:
status:
type: string
default: fail
data:
type: string
example:
password: crowdaction does not exist
/crowdactions:
get:
tags:
Expand Down Expand Up @@ -169,8 +233,17 @@ paths:
summary: Stop participating in a particular crowdaction
responses:
"200":
$ref: "#/components/responses/EmptySuccess"

description: Success message
content:
application/json:
schema:
type: object
properties:
status:
type: string
default: success
data:
default: null
"403":
$ref: "#/components/responses/UnsupportedClientVersion"
"404":
Expand Down Expand Up @@ -217,8 +290,17 @@ paths:
- no-dairy
responses:
"201":
$ref: "#/components/responses/EmptySuccess"

description: Success message
content:
application/json:
schema:
type: object
properties:
status:
type: string
default: success
data:
default: null
"400":
description: Bad request (Invalid commitments)
content:
Expand Down Expand Up @@ -416,6 +498,35 @@ paths:
type: object
default:
userID: No such profile
/profiles/{userID}/participations:
parameters:
- $ref: "#/components/parameters/ApiVersionParameter"
- name: userID
in: path
required: true
schema:
type: string
get:
tags:
- Profile
summary: View the participations of a user
responses:
"200":
description: Profile was found
content:
application/json:
schema:
type: object
properties:
status:
type: string
default: success
data:
type: array
items:
$ref: "#/components/schemas/Participation"
"403":
$ref: "#/components/responses/UnsupportedClientVersion"
/upload-profile-picture:
parameters:
- $ref: "#/components/parameters/ApiVersionParameter"
Expand Down
11 changes: 11 additions & 0 deletions event_examples/crowdaction_participations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"pathParameters": {
"crowdactionID": "sustainability#food#303e67d6"
},
"requestContext": {
"http": {
"method": "GET",
"path": "/crowdactions/sustainability#food#303e67d6"
}
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/aws/aws-sdk-go v1.41.10
github.com/gin-gonic/gin v1.7.7
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 // indirect
github.com/go-playground/locales v0.14.0
github.com/go-playground/universal-translator v0.18.0
github.com/go-playground/validator/v10 v10.10.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/aws/aws-lambda-go v1.27.0 h1:aLzrJwdyHoF1A18YeVdJjX8Ixkd+bpogdxVInvHcWjM=
Expand All @@ -17,6 +18,8 @@ github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
Expand Down
1 change: 1 addition & 0 deletions internal/constants/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (

var (
TableName = os.Getenv("TABLE_NAME")
IndexName = os.Getenv("INDEX_NAME")
ProfileTablename = os.Getenv("PROFILE_TABLE")
ParticipationQueueName = os.Getenv("PARTICIPATION_QUEUE")
)
2 changes: 1 addition & 1 deletion internal/models/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package models

import (
"github.com/CollActionteam/collaction_backend/internal/constants"
validation "github.com/go-ozzo/ozzo-validation"
validation "github.com/go-ozzo/ozzo-validation/v4"
)

type Profile struct {
Expand Down
12 changes: 12 additions & 0 deletions internal/participation/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ type Service interface {
GetParticipation(ctx context.Context, userID string, crowdactionID string) (*m.ParticipationRecord, error)
RegisterParticipation(ctx context.Context, userID string, name string, crowdaction *models.Crowdaction, payload m.JoinPayload) error
CancelParticipation(ctx context.Context, userID string, crowdaction *models.Crowdaction) error
GetParticipationsUser(ctx context.Context, userID string) (*[]m.ParticipationRecord, error)
GetParticipationsCrowdaction(ctx context.Context, crowdactionID string) (*[]m.ParticipationRecord, error)
}

type ParticipationManager interface {
Get(ctx context.Context, userID string, crowdactionID string) (*m.ParticipationRecord, error)
Register(ctx context.Context, userID string, name string, crowdaction *models.Crowdaction, payload m.JoinPayload) error
Cancel(ctx context.Context, userID string, crowdaction *models.Crowdaction) error
ListByUser(ctx context.Context, userID string) (*[]m.ParticipationRecord, error)
ListByCrowdaction(ctx context.Context, crowdactionID string) (*[]m.ParticipationRecord, error)
}

type participationService struct {
Expand Down Expand Up @@ -75,3 +79,11 @@ func (s *participationService) CancelParticipation(ctx context.Context, userID s
}
return recordEvent(userID, crowdaction.CrowdactionID, part.Commitments, -1)
}

func (s *participationService) GetParticipationsUser(ctx context.Context, userID string) (*[]m.ParticipationRecord, error) {
return s.participationRepository.ListByUser(ctx, userID)
}

func (s *participationService) GetParticipationsCrowdaction(ctx context.Context, crowdactionID string) (*[]m.ParticipationRecord, error) {
return s.participationRepository.ListByCrowdaction(ctx, crowdactionID)
}
41 changes: 40 additions & 1 deletion internal/participation/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package participation_test

import (
"context"
"testing"

m "github.com/CollActionteam/collaction_backend/internal/models"
"github.com/CollActionteam/collaction_backend/internal/participation"
"github.com/CollActionteam/collaction_backend/models"
"github.com/CollActionteam/collaction_backend/pkg/mocks/repository"
"github.com/stretchr/testify/assert"
"testing"
)

func TestParticipation_RegisterParticipation(t *testing.T) {
Expand All @@ -21,3 +22,41 @@ func TestParticipation_RegisterParticipation(t *testing.T) {
assert.EqualError(t, err, "cannot change participation for this crowdaction anymore")
})
}

func Test_GetParticipationsUser(t *testing.T) {
ast := assert.New(t)
repository := &repository.Participation{}
cc := []m.ParticipationRecord{}

t.Run("testing GetParticipationsUser", func(t *testing.T) {
userID := "123"

repository.On("ListByUser", context.Background(), userID).Return(&cc, nil).Once()

service := participation.NewParticipationService(repository)
crowdactions, err := service.GetParticipationsUser(context.Background(), userID)
ast.NoError(err)
ast.Equal(&cc, crowdactions)

repository.AssertExpectations(t)
})
}

func Test_GetParticipationsCrowdaction(t *testing.T) {
ast := assert.New(t)
cc := []m.ParticipationRecord{}
repository := &repository.Participation{}

t.Run("testing GetParticipationsCrowdaction", func(t *testing.T) {
crowdactionID := "1"

repository.On("ListByCrowdaction", context.Background(), crowdactionID).Return(&cc, nil).Once()

service := participation.NewParticipationService(repository)
crowdactions, err := service.GetParticipationsCrowdaction(context.Background(), crowdactionID)
ast.NoError(err)
ast.Equal(&cc, crowdactions)

repository.AssertExpectations(t)
})
}
16 changes: 16 additions & 0 deletions pkg/handler/aws/participations/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"context"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
return NewParticipationsHandler().getParticipations(ctx, req)
}

func main() {
lambda.Start(handler)
}
56 changes: 56 additions & 0 deletions pkg/handler/aws/participations/participations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"context"
"fmt"
"net/http"
"strings"

m "github.com/CollActionteam/collaction_backend/internal/models"
"github.com/CollActionteam/collaction_backend/internal/participation"
"github.com/CollActionteam/collaction_backend/pkg/repository/aws"
"github.com/CollActionteam/collaction_backend/utils"
"github.com/aws/aws-lambda-go/events"
)

type participations *[]m.ParticipationRecord

type ParticipationsHandler struct {
service participation.Service
}

func NewParticipationsHandler() *ParticipationsHandler {
participationRepository := aws.NewParticipation(aws.NewDynamo())
return &ParticipationsHandler{service: participation.NewParticipationService(participationRepository)}
}

func (h *ParticipationsHandler) getParticipations(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
var err error = nil
var data *[]m.ParticipationRecord
path := req.RequestContext.HTTP.Path
// TODO pagination
fmt.Printf("Participations function called with path: %s\n", path) // TODO remove
if strings.HasPrefix(path, "/crowdactions") {
crowdactionID := req.PathParameters["crowdactionID"]
// TODO check password:
// 1. Fetch crowdaction
// 2. If the crowdaction is not found, return 404
// 3. If the crowdaction is password protected, check the request for the password
fmt.Printf("getParticipations: crowdactionID: %s\n", crowdactionID) // TODO remove
data, err = h.service.GetParticipationsCrowdaction(ctx, crowdactionID)
} else if strings.HasPrefix(path, "/profiles") {
userID := req.PathParameters["userID"]
data, err = h.service.GetParticipationsUser(ctx, userID)
} else {
err = fmt.Errorf("invalid path: %s", path)
}
if err != nil {
fmt.Printf("getParticipations: error: %s\n", err) // TODO remove
handlerError(err)
}
return utils.GetDataHttpResponse(http.StatusOK, "", data), nil
}

func handlerError(err error) events.APIGatewayV2HTTPResponse {
return utils.CreateMessageHttpResponse(http.StatusInternalServerError, err.Error())
}
11 changes: 11 additions & 0 deletions pkg/mocks/repository/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"context"

m "github.com/CollActionteam/collaction_backend/internal/models"
"github.com/CollActionteam/collaction_backend/models"
"github.com/stretchr/testify/mock"
Expand All @@ -25,3 +26,13 @@ func (s *Participation) Cancel(ctx context.Context, userID string, crowdaction *
args := s.Mock.Called(ctx, userID, crowdaction)
return args.Error(0)
}

func (s *Participation) ListByUser(ctx context.Context, userID string) (*[]m.ParticipationRecord, error) {
args := s.Mock.Called(ctx, userID)
return args.Get(0).(*[]m.ParticipationRecord), args.Error(1)
}

func (s *Participation) ListByCrowdaction(ctx context.Context, crowdactionID string) (*[]m.ParticipationRecord, error) {
args := s.Mock.Called(ctx, crowdactionID)
return args.Get(0).(*[]m.ParticipationRecord), args.Error(1)
}
Loading