first commit
This commit is contained in:
60
pkg/api/handle_market.go
Normal file
60
pkg/api/handle_market.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package api
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
type Market struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Location string `json:"location"`
|
||||
}
|
||||
|
||||
func handleGetMarket(c *gin.Context) {
|
||||
markets := make([]Market, 0)
|
||||
|
||||
rows, err := db.Query(`select id, name, description, location from market`)
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var market Market
|
||||
err = rows.Scan(&market.Id, &market.Name, &market.Description, &market.Location)
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
markets = append(markets, market)
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"markets": markets,
|
||||
})
|
||||
}
|
||||
|
||||
func handleGetMarketByDistance(c *gin.Context) {
|
||||
markets := make([]Market, 0)
|
||||
point := c.Query("point")
|
||||
|
||||
rows, err := db.Query(`select id, name, description, location, location<->$1 as dist from market order by dist`, point)
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
var x string
|
||||
for rows.Next() {
|
||||
var market Market
|
||||
err = rows.Scan(&market.Id, &market.Name, &market.Description, &market.Location, &x)
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
markets = append(markets, market)
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"markets": markets,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user