mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-11 17:42:48 +00:00
feat: add user admin page - various dependency updates
This commit is contained in:
@@ -41,3 +41,14 @@ func UserGet(conn *gorm.DB, id string) (*User, error) {
|
||||
err := conn.First(user).Error
|
||||
return user, err
|
||||
}
|
||||
|
||||
func UserGetAll(conn *gorm.DB) (*[]User, error) {
|
||||
users := &[]User{}
|
||||
err := conn.Find(users).Error
|
||||
return users, err
|
||||
}
|
||||
|
||||
func UserUpdate(conn *gorm.DB, user *User) (*User, error) {
|
||||
err := conn.Save(user).Error
|
||||
return user, err
|
||||
}
|
||||
|
||||
42
server/webserver/routes/user.go
Normal file
42
server/webserver/routes/user.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/db"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/model"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/response"
|
||||
)
|
||||
|
||||
func AddUserRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/user", middleware.AuthorizedJWT(), middleware.AuthPermissions(middleware.PermAdmin), userHandler)
|
||||
group.PUT("/user", middleware.AuthorizedJWT(), middleware.AuthPermissions(middleware.PermAdmin), userUpdateHandler)
|
||||
}
|
||||
|
||||
func userHandler(c *gin.Context) {
|
||||
users, err := model.UserGetAll(db.GetConn())
|
||||
|
||||
if err != nil {
|
||||
response.InternalError(c, err)
|
||||
} else {
|
||||
response.Success(c, users)
|
||||
}
|
||||
}
|
||||
|
||||
func userUpdateHandler(c *gin.Context) {
|
||||
params := struct {
|
||||
Users []model.User `json:"users"`
|
||||
}{}
|
||||
c.BindJSON(¶ms)
|
||||
|
||||
for _, user := range params.Users {
|
||||
_, err := model.UserUpdate(db.GetConn(), &user)
|
||||
|
||||
if err != nil {
|
||||
response.InternalError(c, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
response.Success(c, params.Users)
|
||||
}
|
||||
@@ -4,12 +4,12 @@ import (
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/kkdai/youtube/v2"
|
||||
"github.com/mgerb/go-discord-bot/server/bot"
|
||||
"github.com/mgerb/go-discord-bot/server/db"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/model"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/response"
|
||||
"github.com/rylio/ytdl"
|
||||
)
|
||||
|
||||
// AddVideoArchiveRoutes -
|
||||
@@ -70,7 +70,9 @@ func postVideoArchiveHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
info, err := ytdl.GetVideoInfo(params.URL)
|
||||
client := youtube.Client{}
|
||||
|
||||
info, err := client.GetVideo(params.URL)
|
||||
|
||||
if err != nil {
|
||||
response.InternalError(c, err)
|
||||
@@ -85,7 +87,7 @@ func postVideoArchiveHandler(c *gin.Context) {
|
||||
|
||||
videoArchive := model.VideoArchive{
|
||||
Author: info.Author,
|
||||
DatePublished: info.DatePublished,
|
||||
DatePublished: info.PublishDate,
|
||||
Description: info.Description,
|
||||
Duration: int(info.Duration.Seconds()),
|
||||
Title: info.Title,
|
||||
|
||||
@@ -3,7 +3,6 @@ package webserver
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gobuffalo/packr"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -14,9 +13,7 @@ import (
|
||||
func getRouter() *gin.Engine {
|
||||
router := gin.Default()
|
||||
|
||||
box := packr.NewBox("../../dist/static")
|
||||
|
||||
router.StaticFS("/static", box)
|
||||
router.Static("/static", "./dist/static")
|
||||
router.Static("/public/sounds", config.Config.SoundsPath)
|
||||
router.Static("/public/youtube", config.Config.YoutubePath)
|
||||
router.Static("/public/clips", config.Config.ClipsPath)
|
||||
@@ -32,12 +29,13 @@ func getRouter() *gin.Engine {
|
||||
routes.AddSoundRoutes(api)
|
||||
routes.AddVideoArchiveRoutes(api)
|
||||
routes.AddUserEventLogRoutes(api)
|
||||
routes.AddUserRoutes(api)
|
||||
|
||||
router.NoRoute(func(c *gin.Context) {
|
||||
if strings.HasPrefix(c.Request.URL.String(), "/api/") {
|
||||
response.BadRequest(c, "404 Not Found")
|
||||
} else {
|
||||
c.Data(200, "text/html", box.Bytes("index.html"))
|
||||
c.File("./dist/static/index.html")
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user