Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor : Added service to handle new upcoming commands #24

Merged
merged 5 commits into from
Nov 22, 2024
Merged
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
12 changes: 5 additions & 7 deletions controllers/baseHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/Real-Dev-Squad/discord-service/dtos"
service "github.com/Real-Dev-Squad/discord-service/service"
"github.com/Real-Dev-Squad/discord-service/utils"
"github.com/bwmarrin/discordgo"
"github.com/julienschmidt/httprouter"
Expand All @@ -24,19 +25,16 @@ func HomeHandler(response http.ResponseWriter, request *http.Request, params htt
return
}
switch message.Type {

case discordgo.InteractionPing:
payload := map[string]interface{}{"type": uint8(discordgo.InteractionResponsePong)}
utils.Success.NewDiscordResponse(response, "Pong", payload)
return

case discordgo.InteractionApplicationCommand:
messageResponse := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Hey there! Congratulations, you just executed your first slash command",
},
}
utils.Success.NewDiscordResponse(response, "Success", messageResponse)
service.MainService(&message)(response, request)
return

default:
response.WriteHeader(http.StatusOK)
}
Expand Down
6 changes: 2 additions & 4 deletions controllers/baseHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/Real-Dev-Squad/discord-service/controllers"
"github.com/Real-Dev-Squad/discord-service/dtos"
"github.com/Real-Dev-Squad/discord-service/fixtures"
_ "github.com/Real-Dev-Squad/discord-service/tests/helpers"
"github.com/bwmarrin/discordgo"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -47,8 +48,7 @@ func TestHomeHandler(t *testing.T) {
})
t.Run("Should return 200 when request body is valid for interaction command", func(t *testing.T) {
w := httptest.NewRecorder()
message := dtos.DiscordMessage{Type: discordgo.InteractionApplicationCommand}
jsonBytes, _ := json.Marshal(message)
jsonBytes, _ := json.Marshal(fixtures.HelloCommand)
r, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes))
controllers.HomeHandler(w, r, nil)

Expand All @@ -58,8 +58,6 @@ func TestHomeHandler(t *testing.T) {

assert.Nil(t, err)
assert.Equal(t, float64(discordgo.InteractionResponseChannelMessageWithSource), response["type"])
data := response["data"].(map[string]interface{})
assert.Equal(t, "Hey there! Congratulations, you just executed your first slash command", data["content"])
})

t.Run("Should return 200 when interaction type is unknown", func(t *testing.T) {
Expand Down
14 changes: 12 additions & 2 deletions dtos/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ package dtos

import "github.com/bwmarrin/discordgo"

type Data struct {
discordgo.ApplicationCommandInteractionData
GuildId string `json:"guild_id"`
}

type DiscordMessage struct {
Type discordgo.InteractionType `json:"type"`
User *discordgo.User `json:"user"`
AppPermissions string `json:"app_permissions"`
ApplicationId string `json:"application_id"`
Type discordgo.InteractionType `json:"type"`
Channel *discordgo.Channel `json:"channel"`
ChannelId string `json:"channel_id"`
Member *discordgo.Member `json:"member"`
Data *Data `json:"data"`
}
30 changes: 30 additions & 0 deletions fixtures/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fixtures

import (
"github.com/Real-Dev-Squad/discord-service/dtos"
"github.com/bwmarrin/discordgo"
)

var HelloCommand = &dtos.DiscordMessage{
AppPermissions: "0",
ApplicationId: "123456789012345678",
Type: discordgo.InteractionApplicationCommand,
Channel: &discordgo.Channel{
ID: "987654321098765432",
Name: "general",
},
ChannelId: "987654321098765432",
Member: &discordgo.Member{
User: &discordgo.User{
ID: "123456789012345678",
Username: "ExampleUser",
Discriminator: "1234",
},
},
Data: &dtos.Data{
GuildId: "876543210987654321",
ApplicationCommandInteractionData: discordgo.ApplicationCommandInteractionData{
Name: "hello",
},
},
}
18 changes: 18 additions & 0 deletions service/helloService.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package service

import (
"net/http"

"github.com/Real-Dev-Squad/discord-service/utils"
"github.com/bwmarrin/discordgo"
)

func (s *CommandService) HelloService(response http.ResponseWriter, request *http.Request) {
messageResponse := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: utils.ResponseGenerator.HelloResponse(s.discordMessage.Member.User.ID),
},
}
utils.Success.NewDiscordResponse(response, "Success", messageResponse)
}
35 changes: 35 additions & 0 deletions service/helloService_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package service

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/Real-Dev-Squad/discord-service/fixtures"
_ "github.com/Real-Dev-Squad/discord-service/tests/helpers"
"github.com/Real-Dev-Squad/discord-service/utils"

"github.com/bwmarrin/discordgo"
"github.com/stretchr/testify/assert"
)

func TestHelloService(t *testing.T) {

t.Run("should return a success response with a message", func(t *testing.T) {
w := httptest.NewRecorder()
jsonBytes, _ := json.Marshal(fixtures.HelloCommand)
r, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes))

CS.discordMessage = fixtures.HelloCommand
CS.HelloService(w, r)

assert.Equal(t, http.StatusOK, w.Code)
var response discordgo.InteractionResponse
err := json.NewDecoder(w.Body).Decode(&response)
assert.NoError(t, err)
assert.Equal(t, discordgo.InteractionResponseChannelMessageWithSource, response.Type)
assert.Equal(t, utils.ResponseGenerator.HelloResponse(fixtures.HelloCommand.Member.User.ID), response.Data.Content)
})
}
26 changes: 26 additions & 0 deletions service/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package service

import (
"net/http"

"github.com/Real-Dev-Squad/discord-service/dtos"
)

type CommandService struct {
discordMessage *dtos.DiscordMessage
}

var CS = CommandService{}

func MainService(discordMessage *dtos.DiscordMessage) func(response http.ResponseWriter, request *http.Request) {
CS.discordMessage = discordMessage
switch discordMessage.Data.Name {
case "hello":
return CS.HelloService

default:
return func(response http.ResponseWriter, request *http.Request) {
response.WriteHeader(http.StatusOK)
}
}
}
43 changes: 43 additions & 0 deletions service/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package service

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/Real-Dev-Squad/discord-service/dtos"
"github.com/Real-Dev-Squad/discord-service/fixtures"
_ "github.com/Real-Dev-Squad/discord-service/tests/helpers"
"github.com/bwmarrin/discordgo"
"github.com/stretchr/testify/assert"
)

func TestMainService(t *testing.T) {
t.Run("should return HelloService when command name is hello", func(t *testing.T) {
handler := MainService(fixtures.HelloCommand)
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
handler(w, r)

assert.Equal(t, http.StatusOK, w.Code)
})

t.Run("should return default handler when command name is not in record", func(t *testing.T) {
discordMessage := &dtos.DiscordMessage{
Data: &dtos.Data{
GuildId: "876543210987654321",
ApplicationCommandInteractionData: discordgo.ApplicationCommandInteractionData{
Name: "unknown",
},
},
}

handler := MainService(discordMessage)
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
handler(w, r)

assert.Equal(t, http.StatusOK, w.Code)
})

}
11 changes: 11 additions & 0 deletions utils/responseGenerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

import "fmt"

type responseGenerator struct{}

func (r *responseGenerator) HelloResponse(userId string) string {
return fmt.Sprintf("Hey there <@%s>! Congratulations, you just executed your first slash command", userId)
}

var ResponseGenerator = &responseGenerator{}
Loading