51 lines
956 B
Go
51 lines
956 B
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"sort"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"qr-scanner/models"
|
|
"qr-scanner/services"
|
|
)
|
|
|
|
type ResultsHandler struct {
|
|
store *services.TaskStore
|
|
}
|
|
|
|
func NewResultsHandler(store *services.TaskStore) *ResultsHandler {
|
|
return &ResultsHandler{store: store}
|
|
}
|
|
|
|
func (h *ResultsHandler) GetResults(c *gin.Context) {
|
|
taskID := c.Param("taskID")
|
|
task, ok := h.store.Get(taskID)
|
|
if !ok {
|
|
fail(c, http.StatusNotFound, "任务不存在")
|
|
return
|
|
}
|
|
if task.Status == services.TaskExpired {
|
|
fail(c, http.StatusGone, "任务已过期")
|
|
return
|
|
}
|
|
|
|
resultsMap := task.Results()
|
|
indexes := make([]int, 0, len(resultsMap))
|
|
for i := range resultsMap {
|
|
indexes = append(indexes, i)
|
|
}
|
|
sort.Ints(indexes)
|
|
|
|
items := make([]models.ScanResult, 0, len(indexes))
|
|
for _, i := range indexes {
|
|
items = append(items, resultsMap[i])
|
|
}
|
|
|
|
respondOK(c, gin.H{
|
|
"taskID": task.ID,
|
|
"status": task.Status,
|
|
"items": items,
|
|
})
|
|
}
|