-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from broemp/1-add-afk-command-to-discord-bot
1 add afk command to discord bot
- Loading branch information
Showing
22 changed files
with
274 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package api | ||
|
||
import ( | ||
"database/sql" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type afkRequest struct { | ||
User int64 `json:"user" binding:"required"` | ||
} | ||
|
||
func (server *Server) createAFK(ctx *gin.Context) { | ||
var req afkRequest | ||
if err := ctx.ShouldBindJSON(&req); err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
afk, err := server.store.CreateAFK(ctx, sql.NullInt64{Int64: req.User, Valid: true}) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, afk) | ||
} | ||
|
||
type listAFKRequest struct { | ||
User int64 `uri:"id" binding:"required"` | ||
} | ||
|
||
func (server *Server) listAFK(ctx *gin.Context) { | ||
var req listAFKRequest | ||
if err := ctx.ShouldBindUri(&req); err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
afk, err := server.store.ListAFK(ctx, sql.NullInt64{Int64: req.User, Valid: true}) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, afk) | ||
} | ||
|
||
func (server *Server) countAFK(ctx *gin.Context) { | ||
var req listAFKRequest | ||
if err := ctx.ShouldBindUri(&req); err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
afk, err := server.store.GetAFKCount(ctx, sql.NullInt64{Int64: req.User, Valid: true}) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, afk) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
DROP TABLE IF EXISTS group; | ||
DROP TABLE IF EXISTS afk; | ||
DROP TABLE IF EXISTS hoepperCount; | ||
DROP TABLE IF EXISTS invite; | ||
DROP TABLE IF EXISTS group; | ||
DROP TABLE IF EXISTS user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
-- name: CreateAFK :one | ||
INSERT INTO "afk" ( | ||
userid | ||
) VALUES ( | ||
$1 | ||
) RETURNING *; | ||
|
||
-- name: ListAFK :one | ||
SELECT * from "afk" | ||
WHERE userid = $1; | ||
|
||
-- name: GetAFKCount :one | ||
SELECT count(*) from "afk" | ||
WHERE userid = $1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreateAFK(t *testing.T) { | ||
user := createRandomUser(t) | ||
afk, err := testQueries.CreateAFK(context.Background(), sql.NullInt64{Int64: user.Discordid, Valid: true}) | ||
|
||
require.NoError(t, err) | ||
require.NotEmpty(t, afk) | ||
} | ||
|
||
func TestGetAFK(t *testing.T) { | ||
user := createRandomUser(t) | ||
i := sql.NullInt64{Int64: 0, Valid: true} | ||
for i.Int64 <= 5 { | ||
afk, err := testQueries.CreateAFK(context.Background(), sql.NullInt64{Int64: user.Discordid, Valid: true}) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, afk) | ||
i.Int64++ | ||
} | ||
num, err := testQueries.GetAFKCount(context.Background(), sql.NullInt64{Int64: user.Discordid, Valid: true}) | ||
require.NoError(t, err) | ||
require.Equal(t, i.Int64, num) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreateGroup(t *testing.T) { | ||
user := createRandomUser(t) | ||
|
||
group, err := testQueries.CreateGroup(context.Background(), user.Discordid) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, group) | ||
} | ||
|
||
func TestGetGroupByHost(t *testing.T) { | ||
user := createRandomUser(t) | ||
|
||
group, err := testQueries.CreateGroup(context.Background(), user.Discordid) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, group) | ||
|
||
group2, err := testQueries.GetGroupByHost(context.Background(), user.Discordid) | ||
require.NoError(t, err) | ||
require.Equal(t, group, group2) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.