feat(delivery): refine base service integrations
This commit is contained in:
@@ -18,7 +18,17 @@ func NewMachineHandler(machines *service.MachineService) *MachineHandler {
|
||||
}
|
||||
|
||||
func (h *MachineHandler) Overview(c *gin.Context) {
|
||||
overview, err := h.machines.Overview(c.Request.Context())
|
||||
claims, ok := CurrentClaims(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "missing current user"})
|
||||
return
|
||||
}
|
||||
businessLineID, ok := queryUint(c, "business_line_id")
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "business_line_id is required"})
|
||||
return
|
||||
}
|
||||
overview, err := h.machines.Overview(c.Request.Context(), claims.UserID, claims.IsAdmin, businessLineID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -27,18 +37,28 @@ func (h *MachineHandler) Overview(c *gin.Context) {
|
||||
}
|
||||
|
||||
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"),
|
||||
claims, ok := CurrentClaims(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "missing current user"})
|
||||
return
|
||||
}
|
||||
businessLineID, ok := queryUint(c, "business_line_id")
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "business_line_id is required"})
|
||||
return
|
||||
}
|
||||
resources, err := h.machines.List(c.Request.Context(), claims.UserID, claims.IsAdmin, service.MachineListQuery{
|
||||
BusinessLineID: businessLineID,
|
||||
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"),
|
||||
Source: c.Query("source"),
|
||||
Status: c.Query("status"),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
@@ -67,3 +87,15 @@ func queryInt(c *gin.Context, key string, fallback int) int {
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func queryUint(c *gin.Context, key string) (uint64, bool) {
|
||||
value := c.Query(key)
|
||||
if value == "" {
|
||||
return 0, false
|
||||
}
|
||||
n, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil || n == 0 {
|
||||
return 0, false
|
||||
}
|
||||
return n, true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user