-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoreshandler.go
98 lines (90 loc) · 2.43 KB
/
storeshandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"net/http"
"strings"
"github.com/asendia/salmonping/db"
"github.com/gin-gonic/gin"
)
// storesHandler godoc
//
// @Summary Get list of stores
// @Description get list of stores based on query string params
// @Tags ping
// @Accept json
// @Produce json
// @Param enable_ping query string false "Enable ping, true|false"
// @Param name query string false "Names (comma spearated)"
// @Param platform query string false "Platforms (comma spearated)"
// @Param status query string false "Statuses (comma spearated)"
// @Success 200 {object} StoresResponse
// @Failure 400 {object} DefaultErrorResponse
// @Failure 500 {object} DefaultErrorResponse
// @Router /stores [get]
func storesHandler(c *gin.Context) {
// Prepare db connection
ctx := c.Request.Context()
tx, conn, _, message, err := prepareDBConn(ctx)
if conn != nil {
defer conn.Close(ctx)
}
if tx != nil {
// Rollback everything
defer tx.Rollback(ctx)
}
if err != nil {
log := DefaultErrorResponse{
Error: err.Error(),
Level: "error",
Message: message,
}
logJson(log.JSON())
c.JSON(http.StatusInternalServerError, log)
return
}
var payload StoresPayload
if err := c.ShouldBindQuery(&payload); err != nil {
log := DefaultErrorResponse{
Error: err.Error(),
Level: "error",
Message: "Error binding payload",
Query: c.Request.URL.RawQuery,
}
logJson(log.JSON())
c.JSON(http.StatusBadRequest, log)
return
}
// Query string params
queries := db.New(tx)
var names = filterEmptyStrings(strings.Split(payload.Name, ","))
var platforms = filterEmptyStrings(strings.Split(payload.Platform, ","))
var statuses = filterEmptyStrings(strings.Split(payload.Status, ","))
stores, err := queries.SelectListings(ctx, db.SelectListingsParams{
EnablePing: []bool{true, false},
Names: names,
Platforms: platforms,
Statuses: statuses,
})
if err != nil {
log := DefaultErrorResponse{
Error: err.Error(),
Level: "error",
Message: "Error selecting listing pings",
}
logJson(log.JSON())
c.JSON(http.StatusInternalServerError, log)
return
}
c.JSON(http.StatusOK, StoresResponse{
Stores: stores,
})
}
type StoresPayload struct {
EnablePing string `form:"enable_ping"`
Name string `form:"name"`
Platform string `form:"platform"`
Status string `form:"status"`
URL string `form:"url"`
}
type StoresResponse struct {
Stores []db.SelectListingsRow `json:"stores"`
}