180 lines
4.7 KiB
Go
180 lines
4.7 KiB
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"lv8girl/internal/middleware"
|
|
"lv8girl/internal/services"
|
|
)
|
|
|
|
type AdminController struct {
|
|
adminSvc *services.AdminService
|
|
messageSvc *services.MessageService
|
|
}
|
|
|
|
func NewAdminController() *AdminController {
|
|
return &AdminController{
|
|
adminSvc: services.NewAdminService(),
|
|
messageSvc: services.NewMessageService(),
|
|
}
|
|
}
|
|
|
|
func (c *AdminController) Dashboard(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
stats, _ := c.adminSvc.GetStats()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_dashboard.html", gin.H{
|
|
"Username": username,
|
|
"Stats": stats,
|
|
"Page": "dashboard",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) PendingPosts(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
posts, _ := c.adminSvc.GetPendingPosts()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_pending_posts.html", gin.H{
|
|
"Username": username,
|
|
"Posts": posts,
|
|
"Page": "pending_posts",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) ApprovePost(ctx *gin.Context) {
|
|
postID := parseUint(ctx.Param("id"))
|
|
action := ctx.Param("action")
|
|
|
|
if action == "approve" {
|
|
c.adminSvc.ApprovePost(postID)
|
|
ctx.Redirect(http.StatusFound, "/admin/pending_posts?msg=帖子已通过审核")
|
|
} else if action == "reject" {
|
|
c.adminSvc.RejectPost(postID)
|
|
ctx.Redirect(http.StatusFound, "/admin/pending_posts?msg=帖子已拒绝")
|
|
} else {
|
|
ctx.Redirect(http.StatusFound, "/admin/pending_posts")
|
|
}
|
|
}
|
|
|
|
func (c *AdminController) PendingUsers(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
users, _ := c.adminSvc.GetPendingUsers()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_pending_users.html", gin.H{
|
|
"Username": username,
|
|
"Users": users,
|
|
"Page": "pending_users",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) ApproveUser(ctx *gin.Context) {
|
|
userID := parseUint(ctx.Param("id"))
|
|
action := ctx.Param("action")
|
|
adminID, _ := ctx.Get("user_id")
|
|
|
|
if action == "approve" {
|
|
c.adminSvc.ApproveUser(userID)
|
|
c.messageSvc.NotifyUserApproved(adminID.(uint), userID)
|
|
ctx.Redirect(http.StatusFound, "/admin/pending_users?msg=用户已通过审核")
|
|
} else if action == "reject" {
|
|
c.adminSvc.RejectUser(userID)
|
|
c.messageSvc.NotifyUserRejected(adminID.(uint), userID)
|
|
ctx.Redirect(http.StatusFound, "/admin/pending_users?msg=用户已拒绝")
|
|
} else {
|
|
ctx.Redirect(http.StatusFound, "/admin/pending_users")
|
|
}
|
|
}
|
|
|
|
func (c *AdminController) Posts(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
posts, _ := c.adminSvc.GetAllPosts()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_posts.html", gin.H{
|
|
"Username": username,
|
|
"Posts": posts,
|
|
"Page": "posts",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) DeletePost(ctx *gin.Context) {
|
|
postID := parseUint(ctx.Param("id"))
|
|
c.adminSvc.DeletePost(postID)
|
|
ctx.Redirect(http.StatusFound, "/admin/posts?msg=帖子已删除")
|
|
}
|
|
|
|
func (c *AdminController) Users(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
currentUserID, _ := ctx.Get("user_id")
|
|
users, _ := c.adminSvc.GetAllUsers()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_users.html", gin.H{
|
|
"Username": username,
|
|
"Users": users,
|
|
"CurrentUserID": currentUserID,
|
|
"Page": "users",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) UpdateUserRole(ctx *gin.Context) {
|
|
userIDStr := ctx.PostForm("user_id")
|
|
newRole := ctx.PostForm("new_role")
|
|
currentUserID, _ := ctx.Get("user_id")
|
|
|
|
userID, _ := strconv.ParseUint(userIDStr, 10, 32)
|
|
|
|
if uint(userID) == currentUserID.(uint) {
|
|
ctx.Redirect(http.StatusFound, "/admin/users?msg=不能修改自己的角色")
|
|
return
|
|
}
|
|
|
|
c.adminSvc.UpdateUserRole(uint(userID), newRole)
|
|
|
|
if newRole == "banned" {
|
|
c.messageSvc.NotifyUserBanned(currentUserID.(uint), uint(userID))
|
|
}
|
|
|
|
ctx.Redirect(http.StatusFound, "/admin/users?msg=用户角色已更新")
|
|
}
|
|
|
|
func (c *AdminController) DeleteUser(ctx *gin.Context) {
|
|
userID := parseUint(ctx.Param("id"))
|
|
currentUserID, _ := ctx.Get("user_id")
|
|
|
|
if userID == currentUserID.(uint) {
|
|
ctx.Redirect(http.StatusFound, "/admin/users?msg=不能删除自己")
|
|
return
|
|
}
|
|
|
|
c.adminSvc.DeleteUser(userID)
|
|
ctx.Redirect(http.StatusFound, "/admin/users?msg=用户已删除")
|
|
}
|
|
|
|
func (c *AdminController) Comments(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
comments, _ := c.adminSvc.GetAllComments()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_comments.html", gin.H{
|
|
"Username": username,
|
|
"Comments": comments,
|
|
"Page": "comments",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) DeleteComment(ctx *gin.Context) {
|
|
commentID := parseUint(ctx.Param("id"))
|
|
c.adminSvc.DeleteComment(commentID)
|
|
ctx.Redirect(http.StatusFound, "/admin/comments?msg=评论已删除")
|
|
}
|
|
|
|
func init() {
|
|
_ = middleware.GetCurrentUser
|
|
}
|