Files
discord-retokenizer/endpoints/tokens.go
2022-12-09 23:26:38 -06:00

68 lines
1.2 KiB
Go

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",
})
}