- 新增应用管理页面,支持创建、查看、搜索和删除应用 - 重构API结构,将auth和app相关接口分离到独立模块 - 实现应用列表过滤和短ID生成功能 - 优化路由守卫和全局消息提示 - 更新登录页面样式和表单验证逻辑
88 lines
1.7 KiB
Go
88 lines
1.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"nebula/internal/api/response"
|
|
"nebula/internal/app"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AppHandler struct {
|
|
service *app.AppService
|
|
}
|
|
|
|
func NewAppHandler(service *app.AppService) *AppHandler {
|
|
return &AppHandler{service: service}
|
|
}
|
|
|
|
func (h *AppHandler) List(c *gin.Context) {
|
|
params := make(map[string]any)
|
|
|
|
// 从查询参数中获取过滤条件
|
|
if name := c.Query("name"); name != "" {
|
|
params["name"] = name
|
|
}
|
|
if description := c.Query("description"); description != "" {
|
|
params["description"] = description
|
|
}
|
|
|
|
apps, err := h.service.List(params)
|
|
if err != nil {
|
|
response.FailServer(c, err.Error())
|
|
return
|
|
}
|
|
response.Ok(c, apps)
|
|
}
|
|
|
|
func (h *AppHandler) Get(c *gin.Context) {
|
|
id := c.Param("id")
|
|
app, err := h.service.Get(id)
|
|
if err != nil {
|
|
response.FailServer(c, err.Error())
|
|
return
|
|
}
|
|
response.Ok(c, app)
|
|
}
|
|
|
|
func (h *AppHandler) Create(c *gin.Context) {
|
|
var app app.App
|
|
if err := c.ShouldBindJSON(&app); err != nil {
|
|
response.FailBadRequest(c, err.Error())
|
|
return
|
|
}
|
|
err := h.service.Create(app)
|
|
if err != nil {
|
|
response.FailServer(c, err.Error())
|
|
return
|
|
}
|
|
response.OkMsg(c, "created")
|
|
}
|
|
|
|
func (h *AppHandler) Update(c *gin.Context) {
|
|
id := c.Param("id")
|
|
var app app.App
|
|
if err := c.ShouldBindJSON(&app); err != nil {
|
|
response.FailBadRequest(c, err.Error())
|
|
return
|
|
}
|
|
err := h.service.Update(id, map[string]any{
|
|
"name": app.Name,
|
|
"description": app.Description,
|
|
})
|
|
if err != nil {
|
|
response.FailServer(c, err.Error())
|
|
return
|
|
}
|
|
response.OkMsg(c, "updated")
|
|
}
|
|
|
|
func (h *AppHandler) Delete(c *gin.Context) {
|
|
id := c.Param("id")
|
|
err := h.service.Delete(id)
|
|
if err != nil {
|
|
response.FailServer(c, err.Error())
|
|
return
|
|
}
|
|
response.OkMsg(c, "deleted")
|
|
}
|