-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_handler.go
83 lines (67 loc) · 1.82 KB
/
account_handler.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
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/rralcala/martyn/models"
)
func getAccounts(c *gin.Context) {
source := models.AccountModel{}
count := source.TotalCount()
sort := parseJSONArray(c.Query("sort"))
itemRange := parseJSONArrayInt(c.Query("range"))
filter := parseJSONMap(c.Query("filter"))
c.Writer.Header().Set("X-Total-Count", strconv.FormatInt(count, 10))
c.IndentedJSON(
http.StatusOK,
source.GetList(sort, itemRange, filter),
)
}
func getAccountByID(c *gin.Context) {
source := new(models.AccountModel)
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "id is not int"})
}
item, err := source.GetSingleItem(id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"message": "transaction not found"})
return
}
c.IndentedJSON(http.StatusOK, item)
}
func updateAccounts(c *gin.Context) {
c.JSON(http.StatusNoContent, gin.H{"message": "success"})
}
func deleteAccounts(c *gin.Context) {
source := new(models.AccountModel)
if len(c.Param("id")) > 0 {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "id is not int"})
return
}
item := models.Account{
ID: id,
}
source.Delete([]*models.Account{&item})
} else if filter := c.Query("filter"); len(filter) > 0 {
var items []*models.Account
for _, id := range parseJSONArrayInt(filter) {
items = append(items, &models.Account{
ID: int64(id),
})
}
source.Delete(items)
}
c.JSON(http.StatusNoContent, gin.H{"message": "success"})
}
func postAccount(c *gin.Context) {
source := new(models.AccountModel)
var newItem models.Account
if err := c.BindJSON(&newItem); err != nil {
return
}
source.Create(&newItem)
c.IndentedJSON(http.StatusCreated, newItem)
}