/
author.go
47 lines (38 loc) · 951 Bytes
/
author.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
package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
"twreporter.org/go-api/models"
)
// GetAuthors receive HTTP GET method request, and return the authors.
// `limit`, `offset` and `sort` are the url query params,
// which define the rule we retrieve authors from storage.
func (nc *NewsController) GetAuthors(c *gin.Context) (int, gin.H, error) {
const defaultLimit = 20
const defaultSort = "updatedAt"
var authors []models.FullAuthor
var err error
var total int
_, limit, offset, sort, _ := nc.GetQueryParam(c)
if limit == 0 {
limit = defaultLimit
}
if sort == "" {
sort = defaultSort
}
authors, total, err = nc.Storage.GetFullAuthors(limit, offset, sort)
if err != nil {
return 0, gin.H{}, err
}
return http.StatusOK, gin.H{
"status": "success",
"data": gin.H{
"records": authors,
"meta": models.MetaOfResponse{
Total: total,
Offset: offset,
Limit: limit,
},
},
}, nil
}