Merge Go frontend (apiserver + xcecli) into meson build
This commit is contained in:
parent
43dc12a36c
commit
c9dc8c4676
205 changed files with 30793 additions and 328 deletions
70
src/frontend/apiserver/controllers/response.go
Normal file
70
src/frontend/apiserver/controllers/response.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type JsonStruct struct {
|
||||
Code int `json:"code"`
|
||||
Msg interface{} `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type JsonErrStruct struct {
|
||||
Code int `json:"code"`
|
||||
Msg interface{} `json:"msg"`
|
||||
Details interface{} `json:"details,omitempty"`
|
||||
}
|
||||
|
||||
func ReturnSuccess(ctx *gin.Context, code int, msg interface{}, data interface{}) {
|
||||
json := &JsonStruct{
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
Data: data,
|
||||
}
|
||||
ctx.JSON(200, json)
|
||||
}
|
||||
|
||||
func ReturnError(ctx *gin.Context, code int, msg interface{}, details interface{}) {
|
||||
json := &JsonErrStruct{
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
Details: details,
|
||||
}
|
||||
ctx.JSON(code, json)
|
||||
}
|
||||
|
||||
// BuildBatchOperationResponse creates a structured response map and summary message
|
||||
func BuildBatchOperationResponse(action string,
|
||||
resourceType string,
|
||||
succeededItems []map[string]interface{},
|
||||
failedItems []map[string]interface{},
|
||||
) (map[string]interface{}, string) {
|
||||
overallStatus := ""
|
||||
summaryMsg := ""
|
||||
totalRequested := len(succeededItems) + len(failedItems)
|
||||
|
||||
if len(failedItems) == 0 && totalRequested > 0 {
|
||||
overallStatus = "accepted"
|
||||
summaryMsg = fmt.Sprintf("%s control <%s> accepted for all %d requested %s.", resourceType, action, totalRequested, resourceType) // Making it more generic
|
||||
} else if len(succeededItems) == 0 && totalRequested > 0 {
|
||||
overallStatus = "failed"
|
||||
summaryMsg = fmt.Sprintf("%s control <%s> failed for all %d requested %s.", resourceType, action, totalRequested, resourceType)
|
||||
} else if totalRequested > 0 {
|
||||
overallStatus = "partially_accepted"
|
||||
summaryMsg = fmt.Sprintf("%s control <%s> partially accepted: %d received, %d failed.", resourceType, action, len(succeededItems), len(failedItems))
|
||||
} else { // Should not happen if input validation is correct, but handle defensively
|
||||
overallStatus = "no_items_processed"
|
||||
summaryMsg = fmt.Sprintf("No %s were provided for the '%s' action.", resourceType, action)
|
||||
}
|
||||
|
||||
response := map[string]interface{}{
|
||||
"status": overallStatus,
|
||||
"accepted": succeededItems,
|
||||
"failed": failedItems,
|
||||
}
|
||||
|
||||
return response, summaryMsg
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue