-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
147 lines (112 loc) · 3.05 KB
/
wallet.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
package handler
import (
"database/sql"
"log"
"net/http"
"github.com/1995parham-teaching/song_cloud/request"
"github.com/labstack/echo/v4"
)
type Wallet struct {
Store *sql.DB
}
// nolint: wrapcheck
func (w *Wallet) Update(c echo.Context) error {
ctx := c.Request().Context()
var body request.Wallet
if err := c.Bind(&body); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
stmt, err := w.Store.PrepareContext(
ctx,
"UPDATE wallet SET credit = credit + $1 WHERE username = $2",
)
if err != nil {
log.Printf("stmt preparation failed %s", err)
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
defer stmt.Close()
result, err := stmt.ExecContext(ctx, body.Credit, body.Username)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
n, err := result.RowsAffected()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if n == 0 {
return echo.ErrNotFound
}
return c.NoContent(http.StatusOK)
}
// nolint: wrapcheck, funlen, cyclop
func (w *Wallet) Transfer(c echo.Context) error {
ctx := c.Request().Context()
var body request.Transfer
if err := c.Bind(&body); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
tx, err := w.Store.Begin()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
{
stmt, err := w.Store.PrepareContext(
ctx,
"UPDATE wallet SET credit = credit - $1 WHERE username = $2",
)
if err != nil {
log.Printf("stmt preparation failed %s", err)
_ = tx.Rollback()
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
defer stmt.Close()
result, err := stmt.ExecContext(ctx, body.Credit, body.Username)
if err != nil {
_ = tx.Rollback()
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
n, err := result.RowsAffected()
if err != nil {
_ = tx.Rollback()
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if n == 0 {
_ = tx.Rollback()
return echo.ErrNotFound
}
}
{
stmt, err := w.Store.PrepareContext(
ctx,
"UPDATE wallet SET credit = credit + $1 WHERE username = $2",
)
if err != nil {
log.Printf("stmt preparation failed %s", err)
_ = tx.Rollback()
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
defer stmt.Close()
result, err := stmt.ExecContext(ctx, body.Credit, body.EndUser)
if err != nil {
_ = tx.Rollback()
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
n, err := result.RowsAffected()
if err != nil {
_ = tx.Rollback()
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if n == 0 {
_ = tx.Rollback()
return echo.ErrNotFound
}
}
if err := tx.Commit(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.NoContent(http.StatusOK)
}
func (w *Wallet) Register(g *echo.Group) {
g.POST("/wallet", w.Update)
g.POST("/transfer", w.Transfer)
}