-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_accounts.go
50 lines (42 loc) · 1.06 KB
/
query_accounts.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
package main
import (
"log"
"testing-gin-api/mocks"
"github.com/gin-gonic/gin"
)
type QueryAccountsRequest struct {
Sort string `form:"sort"`
Desc bool `form:"desc"`
Limit int `form:"limit"`
Offset int `form:"offset"`
}
type QueryAccountsResponse struct {
Accounts []mocks.Account `json:"accounts"`
Sort string `form:"sort"`
Desc bool `form:"desc"`
Limit int `form:"limit"`
Offset int `form:"offset"`
Error string `json:"error"`
}
func GetAccounts(c *gin.Context) {
query := QueryAccountsRequest{}
if err := c.BindQuery(&query); err != nil {
log.Println("error binding query params", err)
c.JSON(400, gin.H{"error": err.Error()})
return
}
var accounts []mocks.Account
if query.Sort == "address" {
accounts = mocks.GetSortedAccounts()
} else {
accounts = mocks.GetAccounts()
}
response := QueryAccountsResponse{
Accounts: accounts,
Sort: query.Sort,
Desc: query.Desc,
Limit: query.Limit,
Offset: query.Offset,
}
c.JSON(200, response)
}