This commit is contained in:
2022-12-09 23:26:38 -06:00
parent 8df6231b71
commit d8a6c8acd1
15 changed files with 643 additions and 56 deletions

68
endpoints/tokens.go Normal file
View File

@@ -0,0 +1,68 @@
package endpoints
import (
"fmt"
"git.zomo.dev/zomo/discord-retokenizer/storage"
"github.com/gin-gonic/gin"
)
func getTokens(c *gin.Context) {
tokens := storage.GetTokens()
c.JSON(200, tokens)
}
func getToken(c *gin.Context) {
id := c.Param("id")
token := storage.GetToken(id)
c.JSON(200, token)
}
type TokenBody struct {
BotID string `json:"bot_id" binding:"required"`
}
func addToken(c *gin.Context) {
var tokenBody TokenBody
if err := c.BindJSON(&tokenBody); err != nil {
fmt.Println(err)
return
}
token := storage.GenerateToken(tokenBody.BotID)
if token == "" {
c.JSON(400, gin.H{
"message": "bot id not found",
})
return
}
c.JSON(200, gin.H{
"token": token,
})
}
func deleteToken(c *gin.Context) {
id := c.Param("id")
storage.DeleteToken(id)
c.JSON(200, gin.H{
"message": "success",
})
}
func updateToken(c *gin.Context) {
id := c.Param("id")
var tokenBody TokenBody
if err := c.BindJSON(&tokenBody); err != nil {
fmt.Println(err)
return
}
didSet := storage.UpdateToken(id, tokenBody.BotID)
if !didSet {
c.JSON(400, gin.H{
"message": "token or bot id not found",
})
return
}
c.JSON(200, gin.H{
"message": "success",
})
}