Skip to content

Commit

Permalink
Refactor : Simplified existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
joyguptaa committed Nov 19, 2024
1 parent 33cacfd commit 07f8b11
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
2 changes: 0 additions & 2 deletions controllers/baseHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion service/helloService.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (s *CommandService) HelloService(response http.ResponseWriter, request *htt
messageResponse := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Hey there! Congratulations, you just executed your first slash command",
Content: utils.ResponseGenerator.HelloResponse(s.discordMessage.Member.User.ID),
},
}
utils.Success.NewDiscordResponse(response, "Success", messageResponse)
Expand Down
10 changes: 8 additions & 2 deletions service/helloService_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +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()
r, _ := http.NewRequest("GET", "/", nil)
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, "Hey there! Congratulations, you just executed your first slash command", response.Data.Content)
assert.Equal(t, utils.ResponseGenerator.HelloResponse(fixtures.HelloCommand.Member.User.ID), response.Data.Content)
})
}
1 change: 0 additions & 1 deletion service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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

Expand Down
11 changes: 2 additions & 9 deletions service/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@ import (
"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) {
discordMessage := &dtos.DiscordMessage{
Data: &dtos.Data{
GuildId: "876543210987654321",
ApplicationCommandInteractionData: discordgo.ApplicationCommandInteractionData{
Name: "hello",
},
},
}
handler := MainService(discordMessage)
handler := MainService(fixtures.HelloCommand)
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
handler(w, r)
Expand Down
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{}

0 comments on commit 07f8b11

Please sign in to comment.