feat: wire delivery result and infra data backends
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/1024XEngineer/xinfra/server/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type MachineHandler struct {
|
||||
machines *service.MachineService
|
||||
}
|
||||
|
||||
func NewMachineHandler(machines *service.MachineService) *MachineHandler {
|
||||
return &MachineHandler{machines: machines}
|
||||
}
|
||||
|
||||
func (h *MachineHandler) Overview(c *gin.Context) {
|
||||
overview, err := h.machines.Overview(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, overview)
|
||||
}
|
||||
|
||||
func (h *MachineHandler) List(c *gin.Context) {
|
||||
resources, err := h.machines.List(c.Request.Context(), service.MachineListQuery{
|
||||
Page: queryInt(c, "page", 1),
|
||||
Size: queryInt(c, "size", 20),
|
||||
Hostname: c.Query("hostname"),
|
||||
AssetNumber: c.Query("assetNumber"),
|
||||
Type: c.Query("type"),
|
||||
Location: c.Query("location"),
|
||||
IP: c.Query("ip"),
|
||||
Spec: c.Query("spec"),
|
||||
BusinessLine: c.Query("businessLine"),
|
||||
Source: c.Query("source"),
|
||||
Status: c.Query("status"),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, resources)
|
||||
}
|
||||
|
||||
func (h *MachineHandler) Sync(c *gin.Context) {
|
||||
state, err := h.machines.SyncNow(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error(), "state": state})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, state)
|
||||
}
|
||||
|
||||
func queryInt(c *gin.Context, key string, fallback int) int {
|
||||
value := c.Query(key)
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
n, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return n
|
||||
}
|
||||
Reference in New Issue
Block a user