Skip to content

Commit

Permalink
Add 'date' to responses and sort by date before printing the who's in…
Browse files Browse the repository at this point in the history
… list

Also started adding some ability to test the dynamodb.DataSource
  • Loading branch information
col committed Jul 19, 2018
1 parent 6418b24 commit 42b45bc
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 87 deletions.
11 changes: 5 additions & 6 deletions cmd/telegram/http/main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package main

import (
"github.com/col/whosinbot/dynamodb"
"net/http"
"fmt"
"github.com/col/whosinbot/whosinbot"
"github.com/col/whosinbot/dynamodb"
whttp "github.com/col/whosinbot/http"
"github.com/col/whosinbot/whosinbot"
"net/http"
)

func main() {
dataStore := &dynamodb.DynamoDataStore{}
bot := &whosinbot.WhosInBot{ DataStore: dataStore }
dataStore := dynamodb.NewDynamoDataStore()
bot := &whosinbot.WhosInBot{DataStore: dataStore}

http.Handle("/webhook", &whttp.WebhookHandler{WhosInBot: bot})
port := 8080
serverConfig := fmt.Sprintf(":%d", port)
http.ListenAndServe(serverConfig, nil)
}

10 changes: 5 additions & 5 deletions cmd/telegram/lambda/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"github.com/aws/aws-lambda-go/lambda"
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/col/whosinbot/dynamodb"
"context"
"github.com/col/whosinbot/telegram"
"github.com/col/whosinbot/whosinbot"
"log"
Expand Down Expand Up @@ -34,8 +34,8 @@ func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
}

// Process Command
dataStore := &dynamodb.DynamoDataStore{}
bot := whosinbot.WhosInBot{ DataStore: dataStore }
dataStore := dynamodb.NewDynamoDataStore()
bot := whosinbot.WhosInBot{DataStore: dataStore}
response, err := bot.HandleCommand(command)
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: 400}, err
Expand All @@ -50,4 +50,4 @@ func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
}
}
return events.APIGatewayProxyResponse{StatusCode: 200}, nil
}
}
29 changes: 19 additions & 10 deletions domain/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package domain

import "strings"
import "time"

type Command struct {
ChatID int64
Expand Down Expand Up @@ -28,8 +29,8 @@ func (c Command) ParamsStringExceptFirst() string {
}

type User struct {
UserID int64
Name string
UserID int64
Name string
}

func EmptyCommand() Command {
Expand Down Expand Up @@ -68,7 +69,7 @@ type RollCall struct {
Maybe []RollCallResponse
}

func (r *RollCall)AddResponse(response RollCallResponse) {
func (r *RollCall) AddResponse(response RollCallResponse) {
switch response.Status {
case "in":
r.In = append(r.In, response)
Expand All @@ -80,19 +81,27 @@ func (r *RollCall)AddResponse(response RollCallResponse) {
}

type RollCallResponse struct {
ChatID int64
UserID int64
Name string
Status string
Reason string
ChatID int64
UserID int64
Name string
Status string
Reason string
Date time.Time
}

func NewRollCallResponse(command Command, name string, status string, reason string) RollCallResponse {
return RollCallResponse{
ChatID: command.ChatID,
UserID: command.From.UserID,
Name: name,
Name: name,
Status: status,
Reason: reason,
Date: time.Now(),
}
}
}

type Responses []RollCallResponse

func (r Responses) Len() int { return len(r) }
func (r Responses) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r Responses) Less(i, j int) bool { return r[i].Date.Before(r[j].Date) }
94 changes: 55 additions & 39 deletions dynamodb/data_store.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
package dynamodb

import (
"github.com/col/whosinbot/domain"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/col/whosinbot/domain"
"log"
"os"
"sort"
"strconv"
"log"
"time"
)

const EmptyString = " "

type DynamoDataStore struct {
svc DynamoService
}

type DynamoService interface {
UpdateItem(input *dynamodb.UpdateItemInput) (*dynamodb.UpdateItemOutput, error)
GetItem(input *dynamodb.GetItemInput) (*dynamodb.GetItemOutput, error)
Query(input *dynamodb.QueryInput) (*dynamodb.QueryOutput, error)
BatchWriteItem(input *dynamodb.BatchWriteItemInput) (*dynamodb.BatchWriteItemOutput, error)
}

func (d DynamoDataStore) StartRollCall(rollCall domain.RollCall) (error) {
svc := getService()
func NewDynamoDataStore() *DynamoDataStore {
return &DynamoDataStore{
svc: getService(),
}
}

func (d DynamoDataStore) StartRollCall(rollCall domain.RollCall) error {
// DEBUG
log.Printf("StartRollCall: %+v\n", rollCall)

Expand All @@ -42,7 +55,7 @@ func (d DynamoDataStore) StartRollCall(rollCall domain.RollCall) (error) {
UpdateExpression: aws.String("set title = :title, quiet = :quiet"),
}

_, err := svc.UpdateItem(input)
_, err := d.svc.UpdateItem(input)

if err != nil {
log.Println(err.Error())
Expand All @@ -53,17 +66,15 @@ func (d DynamoDataStore) StartRollCall(rollCall domain.RollCall) (error) {
}

func (d DynamoDataStore) GetRollCall(chatID int64) (*domain.RollCall, error) {
svc := getService()

// DEBUG
log.Printf("GetRollCall: %+d\n", chatID)

input := &dynamodb.GetItemInput{
TableName: aws.String(os.Getenv("ROLLCALL_TABLE")),
Key: getRollCallKey(chatID),
Key: getRollCallKey(chatID),
}

result, err := svc.GetItem(input)
result, err := d.svc.GetItem(input)
if err != nil {
log.Println(err.Error())
return nil, err
Expand All @@ -81,8 +92,8 @@ func (d DynamoDataStore) GetRollCall(chatID int64) (*domain.RollCall, error) {

rollCall := domain.RollCall{
ChatID: chatID,
Title: title,
Quiet: *result.Item["quiet"].BOOL,
Title: title,
Quiet: *result.Item["quiet"].BOOL,
}

return &rollCall, nil
Expand All @@ -103,8 +114,6 @@ func (d DynamoDataStore) LoadRollCallResponses(rollCall *domain.RollCall) error
}

func (d DynamoDataStore) getRollCallResponses(chatID int64) ([]domain.RollCallResponse, error) {
svc := getService()

// DEBUG
log.Printf("getRollCallResponses ChatID: %+d\n", chatID)

Expand All @@ -119,10 +128,10 @@ func (d DynamoDataStore) getRollCallResponses(chatID int64) ([]domain.RollCallRe
},
},
KeyConditionExpression: &keyConditionExpression,
TableName: aws.String(os.Getenv("ROLLCALL_RESPONSE_TABLE")),
TableName: aws.String(os.Getenv("ROLLCALL_RESPONSE_TABLE")),
}

result, err := svc.Query(input)
result, err := d.svc.Query(input)
if err != nil {
log.Println(err.Error())
return nil, err
Expand All @@ -141,22 +150,29 @@ func (d DynamoDataStore) getRollCallResponses(chatID int64) ([]domain.RollCallRe
reason = ""
}

date := time.Now()
err = date.UnmarshalText([]byte(*item["date"].S))
if err != nil {
log.Println(err.Error())
return nil, err
}

response := domain.RollCallResponse{
ChatID: chatID,
UserID: int64(userID),
Name: *item["name"].S,
Name: *item["name"].S,
Status: *item["status"].S,
Reason: reason,
Date: date,
}
responses = append(responses, response)
}
sort.Sort(domain.Responses(responses))

return responses, nil
}

func (d DynamoDataStore) SetResponse(rollCallResponse domain.RollCallResponse) error {
svc := getService()

// DEBUG
log.Printf("SetResponse: %+v\n", rollCallResponse)

Expand All @@ -174,22 +190,28 @@ func (d DynamoDataStore) SetResponse(rollCallResponse domain.RollCallResponse) e
values[":reason"] = &dynamodb.AttributeValue{
S: aws.String(reason),
}
dateString, _ := rollCallResponse.Date.MarshalText()
values[":date"] = &dynamodb.AttributeValue{
S: aws.String(string(dateString)),
}

nameField := "name"
statusField := "status"
dateField := "date"
input := &dynamodb.UpdateItemInput{
ExpressionAttributeNames: map[string]*string{
"#name": &nameField,
"#name": &nameField,
"#status": &statusField,
"#date": &dateField,
},
ExpressionAttributeValues: values,
TableName: aws.String(os.Getenv("ROLLCALL_RESPONSE_TABLE")),
Key: getResponseKey(rollCallResponse.ChatID, rollCallResponse.UserID),
ReturnValues: aws.String("UPDATED_NEW"),
UpdateExpression: aws.String("set #name = :name, #status = :status, reason = :reason"),
TableName: aws.String(os.Getenv("ROLLCALL_RESPONSE_TABLE")),
Key: getResponseKey(rollCallResponse.ChatID, rollCallResponse.UserID),
ReturnValues: aws.String("UPDATED_NEW"),
UpdateExpression: aws.String("set #name = :name, #status = :status, reason = :reason, #date = :date"),
}

_, err := svc.UpdateItem(input)
_, err := d.svc.UpdateItem(input)

if err != nil {
log.Println(err.Error())
Expand All @@ -200,8 +222,6 @@ func (d DynamoDataStore) SetResponse(rollCallResponse domain.RollCallResponse) e
}

func (d DynamoDataStore) EndRollCall(rollCall domain.RollCall) error {
svc := getService()

// DEBUG
log.Printf("EndRollCall: %+v\n", rollCall)

Expand All @@ -222,7 +242,7 @@ func (d DynamoDataStore) EndRollCall(rollCall domain.RollCall) error {
}

input := &dynamodb.BatchWriteItemInput{
RequestItems: map[string][]*dynamodb.WriteRequest {
RequestItems: map[string][]*dynamodb.WriteRequest{
os.Getenv("ROLLCALL_TABLE"): {
&dynamodb.WriteRequest{
DeleteRequest: &dynamodb.DeleteRequest{
Expand All @@ -234,7 +254,7 @@ func (d DynamoDataStore) EndRollCall(rollCall domain.RollCall) error {
},
}

_, err = svc.BatchWriteItem(input)
_, err = d.svc.BatchWriteItem(input)
if err != nil {
log.Println(err.Error())
return err
Expand All @@ -244,8 +264,6 @@ func (d DynamoDataStore) EndRollCall(rollCall domain.RollCall) error {
}

func (d DynamoDataStore) SetTitle(rollCall domain.RollCall, title string) error {
svc := getService()

// DEBUG
log.Printf("SetTitle: %+v\n", rollCall)

Expand All @@ -255,13 +273,13 @@ func (d DynamoDataStore) SetTitle(rollCall domain.RollCall, title string) error
S: aws.String(title),
},
},
TableName: aws.String(os.Getenv("ROLLCALL_TABLE")),
Key: getRollCallKey(rollCall.ChatID),
TableName: aws.String(os.Getenv("ROLLCALL_TABLE")),
Key: getRollCallKey(rollCall.ChatID),
ReturnValues: aws.String("UPDATED_NEW"),
UpdateExpression: aws.String("set title = :title"),
}

_, err := svc.UpdateItem(input)
_, err := d.svc.UpdateItem(input)
if err != nil {
log.Println(err.Error())
return err
Expand All @@ -271,8 +289,6 @@ func (d DynamoDataStore) SetTitle(rollCall domain.RollCall, title string) error
}

func (d DynamoDataStore) SetQuiet(rollCall domain.RollCall, quiet bool) error {
svc := getService()

// DEBUG
log.Printf("SetQuiet: %+v\n", rollCall)

Expand All @@ -282,13 +298,13 @@ func (d DynamoDataStore) SetQuiet(rollCall domain.RollCall, quiet bool) error {
BOOL: aws.Bool(quiet),
},
},
TableName: aws.String(os.Getenv("ROLLCALL_TABLE")),
Key: getRollCallKey(rollCall.ChatID),
TableName: aws.String(os.Getenv("ROLLCALL_TABLE")),
Key: getRollCallKey(rollCall.ChatID),
ReturnValues: aws.String("UPDATED_NEW"),
UpdateExpression: aws.String("set quiet = :quiet"),
}

_, err := svc.UpdateItem(input)
_, err := d.svc.UpdateItem(input)

if err != nil {
log.Println(err.Error())
Expand Down
Loading

0 comments on commit 42b45bc

Please sign in to comment.