Skip to content

Commit

Permalink
member endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
NoBypass committed Feb 17, 2024
1 parent 992ffda commit c41e837
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ well as store the Discord user.
---
</details>
<details>
<summary><code>POST</code> <code><b>/:id/daily</b></code> <code>Claim a daily reward for a Discord user by id</code></summary>
<summary><code>POST</code> <code><b>/daily/:id</b></code> <code>Claim a daily reward for a Discord user by id</code></summary>

##### Request Parameters

Expand Down Expand Up @@ -102,6 +102,31 @@ Discord API. No token is required for this endpoint.
Discord users. NOTE: The pagination uses zero-based indexing.
---
</details>
<details>
<summary><code>GET</code> <code><b>/member/:id</b></code> <code>Get the stats of a Discord member</code></summary>

##### Request Parameters

- `id` the Discord id of the user whose stats should be retrieved

##### Response Body (JSON)

``` go
type DiscordMemberResponse struct {
DiscordID string `json:"discord_id"`
Name string `json:"name"`
Nick string `json:"nick"`
XP float64 `json:"xp"`
LastDailyAt string `json:"last_daily_at"`
Level int `json:"level"`
Streak int `json:"streak"`
}
```

##### Method (on Client)
`Member(id string) (*DiscordMemberResponse, error)` Member is used to get the stats for a specific Discord user.
---
</details>

## Environment Variables
- `port` - The port to listen on
Expand Down
1 change: 1 addition & 0 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ________________________________________________
discord.Use(authService.DiscordAuthMiddleware())
discord.POST("/verify", discordController.Verify)
discord.PATCH("/daily/:id", discordController.Daily)
discord.GET("/member/:id", discordController.Member)
discord.GET("/leaderboard/:page", discordController.Leaderboard)
discord.POST("/bot-login", discordController.BotLogin)

Expand Down
16 changes: 16 additions & 0 deletions internal/app/controller/discord_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type DiscordController interface {
Verify(c echo.Context) error
Daily(c echo.Context) error
Member(c echo.Context) error
BotLogin(c echo.Context) error
Leaderboard(c echo.Context) error
}
Expand All @@ -26,6 +27,21 @@ func NewDiscordController(db *surreal_wrap.DB, config *conf.Config) DiscordContr
}
}

func (c discordController) Member(ctx echo.Context) error {
errCh := c.service.InjectErrorChan()

id := ctx.Param("id")

memberCh := c.service.GetMember(id)

select {
case err := <-errCh:
return err
case member := <-memberCh:
return ctx.JSON(http.StatusOK, member)
}
}

func (c discordController) Verify(ctx echo.Context) error {
cancel := c.service.InjectContext(ctx.Request().Context())
errCh := c.service.InjectErrorChan()
Expand Down
10 changes: 10 additions & 0 deletions pkg/api/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ func (c *Client) Leaderboard(page string) (*DiscordLeaderboardResponse, error) {

return do[DiscordLeaderboardResponse](req)
}

// Member is used to get the stats for a specific Discord user.
func (c *Client) Member(id string) (*DiscordMemberResponse, error) {
req, err := c.newJsonRequest(http.MethodGet, "/discord/member/"+id, nil)
if err != nil {
return nil, err
}

return do[DiscordMemberResponse](req)
}
6 changes: 6 additions & 0 deletions test/discord_signup.http
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ Content-Type: application/json
GET http://localhost:8080/discord/leaderboard/0
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjb3JlIiwic3ViIjoiYm90IiwiYXVkIjpbImJvdCJdLCJpYXQiOjE3MDQxNTgyOTd9.1p1Y1tAky8QOQhKe4GmLtwj0imWyS5qNM5D-fX-dGAk

### Test getting user info
GET http://localhost:8080/discord/member/672835870080106509
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjb3JlIiwic3ViIjoiYm90IiwiYXVkIjpbImJvdCJdLCJpYXQiOjE3MDQxNTgyOTd9.1p1Y1tAky8QOQhKe4GmLtwj0imWyS5qNM5D-fX-dGAk

0 comments on commit c41e837

Please sign in to comment.