-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.go
179 lines (169 loc) · 4.61 KB
/
controllers.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"fmt"
"log"
"github.com/gin-gonic/gin"
"github.com/lancatlin/lazy-finance/ledger"
"github.com/lancatlin/lazy-finance/model"
)
// @Summary Get Templates
// @Description get templates for a user
// @Tags templates
// @Accept json
// @Produce json
// @Success 200 {array} string "Returns user templates"
// @Failure 500 {object} Error "Internal Server Error"
// @Router /templates [get]
func getTemplates(c *gin.Context) {
user := getUser(c)
templates, err := user.templates()
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(200, templates)
}
// @Summary New Transaction
// @Description create a new transaction
// @Tags transactions
// @Accept json
// @Produce json
// @Param data body model.Transaction true "Transaction Data"
// @Param save query bool false "Save as template"
// @Success 200 {object} string "Returns new transaction"
// @Failure 400 {object} Error "Bad Request"
// @Failure 500 {object} Error "Internal Server Error"
// @Router /txs [post]
func newTx(c *gin.Context) {
var payload model.Transaction
if err := c.ShouldBind(&payload); err != nil {
c.AbortWithError(400, err)
return
}
user := getUser(c)
err := user.newTx(payload)
if err != nil {
c.AbortWithError(400, err)
return
}
save := c.Query("save")
log.Println(save)
if save == "true" {
template := model.FromTransaction(payload)
err := user.saveTemplate(template)
if err != nil {
c.AbortWithError(500, err)
return
}
}
c.JSON(200, payload)
}
// @Summary Get Transactions
// @Description get transactions for a user
// @Tags transactions
// @Accept json
// @Produce json
// @Param query query ledger.Query false "Query"
// @Success 200 {array} model.Transaction "Returns user transactions"
// @Failure 500 {object} Error "Internal Server Error"
// @Router /txs [get]
func getTxs(c *gin.Context) {
user := getUser(c)
query := ledger.Query{}
if err := c.Bind(&query); err != nil {
c.AbortWithError(400, err)
return
}
txs, err := user.txs(query)
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(200, txs)
}
// @Summary Get Balances
// @Description get balances for a user
// @Tags balances
// @Accept json
// @Produce json
// @Param query query ledger.Query false "Query"
// @Success 200 {array} ledger.Balance "Returns user balances"
// @Failure 500 {object} Error "Internal Server Error"
// @Router /balances [get]
func getBalances(c *gin.Context) {
user := getUser(c)
query := ledger.Query{}
if err := c.Bind(&query); err != nil {
c.AbortWithError(400, err)
return
}
balances, err := user.getBalances(query)
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(200, balances)
}
// @Summary Get File List
// @Description get file list for a user
// @Tags files
// @Accept json
// @Produce json
// @Success 200 {array} File "Returns user file list"
// @Failure 500 {object} Error "Internal Server Error"
// @Router /files [get]
func getFileList(c *gin.Context) {
user := getUser(c)
files, err := user.ListFiles()
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(200, files)
}
// @Summary Get File
// @Description get file for a user
// @Tags files
// @Accept json
// @Produce json
// @Param path path string true "File Path"
// @Success 200 {file} string "Returns user file"
// @Failure 500 {object} Error "Internal Server Error"
// @Router /files/{path} [get]
func getFile(c *gin.Context) {
user := getUser(c)
path := c.Param("path")
c.File(user.FilePath(path))
}
// @Summary Upload File
// @Description upload file for a user
// @Tags files
// @Accept json
// @Produce json
// @Param path path string true "File Path"
// @Param data body string true "File Data"
// @Success 200 {object} string "Returns success"
// @Failure 500 {object} Error "Internal Server Error"
// @Router /files/{path} [post]
func uploadFile(c *gin.Context) {
user := getUser(c)
path := c.Param("path")
var payload map[string]string
if err := c.BindJSON(&payload); err != nil {
c.AbortWithError(400, err)
return
}
data, ok := payload["data"]
if !ok {
c.AbortWithError(400, fmt.Errorf("missing data"))
return
}
err := user.overwriteFile(path, data)
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(200, gin.H{
"message": "success",
})
}