This repository has been archived by the owner on Mar 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
crowdaction.go
127 lines (116 loc) · 4.86 KB
/
crowdaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package models
import (
"fmt"
"github.com/CollActionteam/collaction_backend/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
)
const (
KeyDateStart = "date_start"
KeyDateEnd = "date_end"
KeyDateJoinBefore = "date_limit_join"
)
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"`
}
// TODO for later: Use different model between database and api (PasswordJoin vs. IsPasswordRequired)
type Crowdaction struct {
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"` // Must match KeyDateStart
DateEnd string `json:"date_end"` // Must match KeyDateEnd
DateLimitJoin string `json:"date_limit_join"` // Must match KeyDateLimitJoin
PasswordJoin string `json:"password_join"`
CommitmentOptions []CommitmentOption `json:"commitment_options"`
ParticipantCount int `json:"participant_count"`
TopParticipants []CrowdactionParticipant `json:"top_participants"`
Images CrowdactionImages `json:"images"`
}
func GetCrowdaction(crowdactionID string, tableName string) (*Crowdaction, error) {
var crowdaction Crowdaction
dbClient := utils.CreateDBClient()
item, err := utils.GetDBItem(dbClient, tableName, utils.PKCrowdaction, crowdactionID)
if err != nil {
return nil, err
}
if item == nil {
return nil, fmt.Errorf("crowdaction not found")
}
err = dynamodbattribute.UnmarshalMap(item, &crowdaction)
if err != nil {
return nil, err
}
return &crowdaction, nil
}
// TODO include parameter "skPrefix" to efficiently select category and subcategory
func listCrowdactions(tableName string, filterCond expression.ConditionBuilder, startFrom *utils.PrimaryKey) ([]Crowdaction, *utils.PrimaryKey, error) {
crowdactions := []Crowdaction{}
dbClient := utils.CreateDBClient()
keyCond := expression.Key(utils.PartitionKey).Equal(expression.Value(utils.PKCrowdaction))
expr, err := expression.NewBuilder().
WithKeyCondition(keyCond).
WithFilter(filterCond).
Build()
if err != nil {
return crowdactions, nil, err
}
var exclusiveStartKey utils.PrimaryKey
if startFrom != nil {
exclusiveStartKey = *startFrom
}
out, err := dbClient.Query(&dynamodb.QueryInput{
Limit: aws.Int64(utils.CrowdactionsPageLength),
ExclusiveStartKey: exclusiveStartKey,
TableName: aws.String(tableName),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
KeyConditionExpression: expr.KeyCondition(),
FilterExpression: expr.Filter(),
})
if err != nil {
return crowdactions, nil, err
} else if out == nil {
return crowdactions, nil, fmt.Errorf("query did not return result")
}
for _, item := range out.Items {
var crowdaction Crowdaction
itemErr := dynamodbattribute.UnmarshalMap(item, &crowdaction)
if itemErr == nil {
crowdactions = append(crowdactions, crowdaction)
}
}
if len(out.Items) != len(crowdactions) {
err = fmt.Errorf("error unmarshalling %d items", len(out.Items)-len(crowdactions))
}
startFrom = nil
if out.LastEvaluatedKey != nil && len(out.LastEvaluatedKey) > 0 {
lastPK := out.LastEvaluatedKey[utils.PartitionKey].S
lastSK := out.LastEvaluatedKey[utils.SortKey].S
exclusiveStartKey = utils.GetPrimaryKey(*lastPK, *lastSK)
startFrom = &exclusiveStartKey
}
return crowdactions, startFrom, err
}
func ListActiveCrowdactions(tableName string, startFrom *utils.PrimaryKey) ([]Crowdaction, *utils.PrimaryKey, error) {
filterCond := expression.Name(KeyDateStart).LessThanEqual(expression.Value(utils.GetDateStringNow()))
return listCrowdactions(tableName, filterCond, startFrom)
}
func ListJoinableCrowdactions(tableName string, startFrom *utils.PrimaryKey) ([]Crowdaction, *utils.PrimaryKey, error) {
filterCond := expression.Name(KeyDateJoinBefore).GreaterThan(expression.Value(utils.GetDateStringNow()))
return listCrowdactions(tableName, filterCond, startFrom)
}
func ListCompletedCrowdactions(tableName string, startFrom *utils.PrimaryKey) ([]Crowdaction, *utils.PrimaryKey, error) {
filterCond := expression.Name(KeyDateEnd).LessThanEqual(expression.Value(utils.GetDateStringNow()))
return listCrowdactions(tableName, filterCond, startFrom)
}