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