83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"oidc-oauth2-server/services"
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AdminHandler struct {
|
|
adminService *services.AdminService
|
|
}
|
|
|
|
func NewAdminHandler(adminService *services.AdminService) *AdminHandler {
|
|
return &AdminHandler{adminService: adminService}
|
|
}
|
|
|
|
func (h *AdminHandler) ShowAdminLogin(c *gin.Context) {
|
|
c.HTML(http.StatusOK, "admin_login.html", gin.H{})
|
|
}
|
|
|
|
func (h *AdminHandler) HandleAdminLogin(c *gin.Context) {
|
|
username := c.PostForm("username")
|
|
password := c.PostForm("password")
|
|
|
|
admin, err := h.adminService.Authenticate(username, password)
|
|
if err != nil {
|
|
c.HTML(http.StatusBadRequest, "admin_login.html", gin.H{
|
|
"error": "Invalid credentials",
|
|
})
|
|
return
|
|
}
|
|
|
|
session := sessions.Default(c)
|
|
session.Set("admin_id", admin.ID)
|
|
session.Save()
|
|
|
|
c.Redirect(http.StatusFound, "/admin/dashboard")
|
|
}
|
|
|
|
func (h *AdminHandler) Dashboard(c *gin.Context) {
|
|
c.HTML(http.StatusOK, "admin_dashboard.html", gin.H{})
|
|
}
|
|
|
|
func (h *AdminHandler) ListUsers(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
|
|
|
users, total, err := h.adminService.ListUsers(page, pageSize)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "admin_users.html", gin.H{
|
|
"users": users,
|
|
"total": total,
|
|
"page": page,
|
|
"pageSize": pageSize,
|
|
})
|
|
}
|
|
|
|
func (h *AdminHandler) ListClients(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
|
|
|
clients, total, err := h.adminService.ListClients(page, pageSize)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "admin_clients.html", gin.H{
|
|
"clients": clients,
|
|
"total": total,
|
|
"page": page,
|
|
"pageSize": pageSize,
|
|
})
|
|
}
|