Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5b32f25f
authored
Jul 23, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: fix redemption can be used multiple times in some cases
parent
819056a8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
4 additions
and
3 deletions
+4
-3
model/redemption.go
+4
-3
No files found.
model/redemption.go
View file @
5b32f25f
...
@@ -51,20 +51,21 @@ func Redeem(key string, userId int) (quota int, err error) {
...
@@ -51,20 +51,21 @@ func Redeem(key string, userId int) (quota int, err error) {
redemption
:=
&
Redemption
{}
redemption
:=
&
Redemption
{}
err
=
DB
.
Transaction
(
func
(
tx
*
gorm
.
DB
)
error
{
err
=
DB
.
Transaction
(
func
(
tx
*
gorm
.
DB
)
error
{
err
:=
DB
.
Where
(
"`key` = ?"
,
key
)
.
First
(
redemption
)
.
Error
err
:=
tx
.
Set
(
"gorm:query_option"
,
"FOR UPDATE"
)
.
Where
(
"`key` = ?"
,
key
)
.
First
(
redemption
)
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
"无效的兑换码"
)
return
errors
.
New
(
"无效的兑换码"
)
}
}
if
redemption
.
Status
!=
common
.
RedemptionCodeStatusEnabled
{
if
redemption
.
Status
!=
common
.
RedemptionCodeStatusEnabled
{
return
errors
.
New
(
"该兑换码已被使用"
)
return
errors
.
New
(
"该兑换码已被使用"
)
}
}
err
=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
userId
)
.
Update
(
"quota"
,
gorm
.
Expr
(
"quota + ?"
,
redemption
.
Quota
))
.
Error
err
=
tx
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
userId
)
.
Update
(
"quota"
,
gorm
.
Expr
(
"quota + ?"
,
redemption
.
Quota
))
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
redemption
.
RedeemedTime
=
common
.
GetTimestamp
()
redemption
.
RedeemedTime
=
common
.
GetTimestamp
()
redemption
.
Status
=
common
.
RedemptionCodeStatusUsed
redemption
.
Status
=
common
.
RedemptionCodeStatusUsed
return
redemption
.
SelectUpdate
()
err
=
tx
.
Save
(
redemption
)
.
Error
return
err
})
})
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
errors
.
New
(
"兑换失败,"
+
err
.
Error
())
return
0
,
errors
.
New
(
"兑换失败,"
+
err
.
Error
())
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment