Skip to content

Commit

Permalink
Change UserId to name when set_in_for another user
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemeng Qin committed Aug 1, 2018
1 parent 7a8bd31 commit c920501
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 62 deletions.
11 changes: 1 addition & 10 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions cmd/telegram/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

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

Expand Down
6 changes: 3 additions & 3 deletions cmd/telegram/lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/col/whosinbot/dynamodb"
"github.com/col/whosinbot/telegram"
"github.com/col/whosinbot/whosinbot"
"whosinbot/dynamodb"
"whosinbot/telegram"
"whosinbot/whosinbot"
"log"
"strings"
)
Expand Down
12 changes: 8 additions & 4 deletions domain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c Command) ParamsStringExceptFirst() string {
}

type User struct {
UserID int64
UserID string
Name string
}

Expand Down Expand Up @@ -82,17 +82,21 @@ func (r *RollCall) AddResponse(response RollCallResponse) {

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

func NewRollCallResponse(command Command, name string, status string, reason string) RollCallResponse {
func NewRollCallResponse(command Command, name string, status string, reason string, isSetStatusFor bool) RollCallResponse {
var userId = command.From.UserID
if isSetStatusFor {
userId = name
}
return RollCallResponse{
ChatID: command.ChatID,
UserID: command.From.UserID,
UserID: userId,
Name: name,
Status: status,
Reason: reason,
Expand Down
13 changes: 4 additions & 9 deletions dynamodb/data_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"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"
"whosinbot/domain"
"log"
"os"
"sort"
Expand Down Expand Up @@ -139,11 +139,6 @@ func (d DynamoDataStore) getRollCallResponses(chatID int64) ([]domain.RollCallRe

var responses = []domain.RollCallResponse{}
for _, item := range result.Items {
userID, err := strconv.Atoi(*item["user_id"].N)
if err != nil {
log.Println(err.Error())
return nil, err
}

reason := *item["reason"].S
if reason == EmptyString {
Expand All @@ -159,7 +154,7 @@ func (d DynamoDataStore) getRollCallResponses(chatID int64) ([]domain.RollCallRe

response := domain.RollCallResponse{
ChatID: chatID,
UserID: int64(userID),
UserID: *item["user_id"].S,
Name: *item["name"].S,
Status: *item["status"].S,
Reason: reason,
Expand Down Expand Up @@ -322,13 +317,13 @@ func getRollCallKey(chatID int64) map[string]*dynamodb.AttributeValue {
}
}

func getResponseKey(chatID int64, userID int64) map[string]*dynamodb.AttributeValue {
func getResponseKey(chatID int64, userID string) map[string]*dynamodb.AttributeValue {
return map[string]*dynamodb.AttributeValue{
"chat_id": {
N: aws.String(strconv.Itoa(int(chatID))),
},
"user_id": {
N: aws.String(strconv.Itoa(int(userID))),
N: aws.String(userID),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion dynamodb/data_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dynamodb

import (
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/col/whosinbot/domain"
"whosinbot/domain"
"github.com/stretchr/testify/assert"
"testing"
)
Expand Down
4 changes: 2 additions & 2 deletions http/webhook_handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package http

import (
"github.com/col/whosinbot/telegram"
"github.com/col/whosinbot/whosinbot"
"whosinbot/telegram"
"whosinbot/whosinbot"
"io/ioutil"
"net/http"
"os"
Expand Down
2 changes: 1 addition & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ resources:
AttributeType: N
-
AttributeName: user_id
AttributeType: N
AttributeType: S
KeySchema:
-
AttributeName: chat_id
Expand Down
5 changes: 3 additions & 2 deletions telegram/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package telegram

import (
"encoding/json"
"github.com/col/whosinbot/domain"
"whosinbot/domain"
"gopkg.in/telegram-bot-api.v4"
"strings"
"strconv"
)

func ParseUpdate(requestBody []byte) (domain.Command, error) {
Expand All @@ -19,7 +20,7 @@ func ParseUpdate(requestBody []byte) (domain.Command, error) {
Name: update.Message.Command(),
Params: strings.Fields(update.Message.CommandArguments()),
From: domain.User{
UserID: int64(update.Message.From.ID),
UserID: strconv.Itoa(update.Message.From.ID),
Name: update.Message.From.FirstName,
},
}
Expand Down
2 changes: 1 addition & 1 deletion telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package telegram
import (
"errors"
"fmt"
"github.com/col/whosinbot/domain"
"whosinbot/domain"
"gopkg.in/telegram-bot-api.v4"
"os"
)
Expand Down
10 changes: 5 additions & 5 deletions whosinbot/whos_in_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package whosinbot

import (
"fmt"
"github.com/col/whosinbot/domain"
"whosinbot/domain"
"log"
"strings"
)
Expand Down Expand Up @@ -130,17 +130,17 @@ func (b *WhosInBot) handleWhosIn(command domain.Command) (*domain.Response, erro
}

func (b *WhosInBot) handleResponse(command domain.Command, status string) (*domain.Response, error) {
return b.setAttendanceFor(command, command.From.Name, status, command.ParamsString())
return b.setAttendanceFor(command, command.From.Name, status, command.ParamsString(), false)
}

func (b *WhosInBot) handleResponseFor(command domain.Command, status string) (*domain.Response, error) {
if len(command.FirstParam()) == 0 {
return &domain.Response{ChatID: command.ChatID, Text: "Please provide the persons first name."}, nil
}
return b.setAttendanceFor(command, command.FirstParam(), status, command.ParamsStringExceptFirst())
return b.setAttendanceFor(command, command.FirstParam(), status, command.ParamsStringExceptFirst(), true)
}

func (b *WhosInBot) setAttendanceFor(command domain.Command, name string, status string, reason string) (*domain.Response, error) {
func (b *WhosInBot) setAttendanceFor(command domain.Command, name string, status string, reason string, isSetStatusFor bool) (*domain.Response, error) {
rollCall, err := b.DataStore.GetRollCall(command.ChatID)
if err != nil {
return nil, err
Expand All @@ -149,7 +149,7 @@ func (b *WhosInBot) setAttendanceFor(command domain.Command, name string, status
return &domain.Response{Text: "No roll call in progress", ChatID: command.ChatID}, nil
}

rollCallResponse := domain.NewRollCallResponse(command, name, status, reason)
rollCallResponse := domain.NewRollCallResponse(command, name, status, reason, isSetStatusFor)
b.DataStore.SetResponse(rollCallResponse)

err = b.DataStore.LoadRollCallResponses(rollCall)
Expand Down
Loading

0 comments on commit c920501

Please sign in to comment.