-
Notifications
You must be signed in to change notification settings - Fork 1
/
locations.go
69 lines (57 loc) · 1.77 KB
/
locations.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
package main
import (
_ "github.com/mattn/go-sqlite3"
"github.com/gin-gonic/gin"
)
type (
Location struct {
Id int64 `db:"id" json:"-"`
Location string `db:"nombre_locacion" json:"location,omitempty"`
Total string `db:"count(nombre_locacion)" json:"total,omitempty"`
}
ResourceLocation struct {
Id int64 `json:"id"`
Type string `json:"type"`
Attributes Location `json:"attributes"`
}
ResourceLocations struct {
Data []ResourceLocation `json:"data"`
Links PaginationLinks `json:"links"`
Meta Meta `json:"meta"`
}
)
func newResourceLocation (attr Location) ResourceLocation {
return ResourceLocation {
Id: attr.Id,
Type: "locations",
Attributes: attr,
}
}
func newResourceLocations (locations []Location, endpoint string, pg string) ResourceLocations {
resources := make([]ResourceLocation, 0)
for l := range locations {
resources = append(resources, newResourceLocation(locations[l]))
}
links := PaginationLinks {
First: paginationLink(endpoint, firstPage, "first"),
Last: paginationLink(endpoint, lastPage, "last"),
Prev: paginationLink(endpoint, pg, "prev"),
Next: paginationLink(endpoint, pg, "next"),
}
return ResourceLocations{Data: resources, Links: links, Meta: Meta{TotalPages: "1"}}
}
// http http://localhost:8080/api/locations
func GetLocations(c *gin.Context) {
var locations []Location
page := c.DefaultQuery("page", "1")
query := PaginateQuery(groupByQuery("SELECT id, %s, count(%s) FROM exhibiciones", "location"), page)
_, err := dbmap.Select(&locations, query)
checkErr(err)
if err == nil {
content := newResourceLocations(locations, "locations", page)
c.JSON(200, content)
} else {
jsonErr := &Error{"not_found", 404, "Not Found Error", "No location found."}
c.JSON(404, jsonErr)
}
}