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
7bd4c0e4
authored
May 04, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: return quota to user when delete token (close #37)
parent
28e05cde
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
22 additions
and
6 deletions
+22
-6
controller/relay.go
+1
-1
model/redemption.go
+1
-1
model/token.go
+15
-4
model/user.go
+5
-0
No files found.
controller/relay.go
View file @
7bd4c0e4
...
@@ -137,7 +137,7 @@ func relayHelper(c *gin.Context) error {
...
@@ -137,7 +137,7 @@ func relayHelper(c *gin.Context) error {
ratio
=
common
.
RatioGPT3dot5
ratio
=
common
.
RatioGPT3dot5
}
}
quota
=
int
(
float64
(
quota
)
*
ratio
)
quota
=
int
(
float64
(
quota
)
*
ratio
)
err
:=
model
.
Consum
eTokenQuota
(
tokenId
,
quota
)
err
:=
model
.
Decreas
eTokenQuota
(
tokenId
,
quota
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"Error consuming token remain quota: "
+
err
.
Error
())
common
.
SysError
(
"Error consuming token remain quota: "
+
err
.
Error
())
}
}
...
...
model/redemption.go
View file @
7bd4c0e4
...
@@ -55,7 +55,7 @@ func Redeem(key string, tokenId int) (quota int, err error) {
...
@@ -55,7 +55,7 @@ func Redeem(key string, tokenId int) (quota int, err error) {
if
redemption
.
Status
!=
common
.
RedemptionCodeStatusEnabled
{
if
redemption
.
Status
!=
common
.
RedemptionCodeStatusEnabled
{
return
0
,
errors
.
New
(
"该兑换码已被使用"
)
return
0
,
errors
.
New
(
"该兑换码已被使用"
)
}
}
err
=
TopUp
TokenQuota
(
tokenId
,
redemption
.
Quota
)
err
=
Increase
TokenQuota
(
tokenId
,
redemption
.
Quota
)
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
err
return
0
,
err
}
}
...
...
model/token.go
View file @
7bd4c0e4
...
@@ -116,15 +116,26 @@ func DeleteTokenById(id int, userId int) (err error) {
...
@@ -116,15 +116,26 @@ func DeleteTokenById(id int, userId int) (err error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
quota
:=
token
.
RemainQuota
if
quota
!=
0
{
if
quota
>
0
{
err
=
IncreaseUserQuota
(
userId
,
quota
)
}
else
{
err
=
DecreaseUserQuota
(
userId
,
quota
)
}
}
if
err
!=
nil
{
return
err
}
return
token
.
Delete
()
return
token
.
Delete
()
}
}
func
Consum
eTokenQuota
(
id
int
,
quota
int
)
(
err
error
)
{
func
Increas
eTokenQuota
(
id
int
,
quota
int
)
(
err
error
)
{
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"remain_quota"
,
gorm
.
Expr
(
"remain_quota
-
?"
,
quota
))
.
Error
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"remain_quota"
,
gorm
.
Expr
(
"remain_quota
+
?"
,
quota
))
.
Error
return
err
return
err
}
}
func
TopUp
TokenQuota
(
id
int
,
quota
int
)
(
err
error
)
{
func
Decrease
TokenQuota
(
id
int
,
quota
int
)
(
err
error
)
{
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"remain_quota"
,
gorm
.
Expr
(
"remain_quota
+
?"
,
quota
))
.
Error
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"remain_quota"
,
gorm
.
Expr
(
"remain_quota
-
?"
,
quota
))
.
Error
return
err
return
err
}
}
model/user.go
View file @
7bd4c0e4
...
@@ -225,6 +225,11 @@ func GetUserQuota(id int) (quota int, err error) {
...
@@ -225,6 +225,11 @@ func GetUserQuota(id int) (quota int, err error) {
return
quota
,
err
return
quota
,
err
}
}
func
IncreaseUserQuota
(
id
int
,
quota
int
)
(
err
error
)
{
err
=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"quota"
,
gorm
.
Expr
(
"quota + ?"
,
quota
))
.
Error
return
err
}
func
DecreaseUserQuota
(
id
int
,
quota
int
)
(
err
error
)
{
func
DecreaseUserQuota
(
id
int
,
quota
int
)
(
err
error
)
{
err
=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"quota"
,
gorm
.
Expr
(
"quota - ?"
,
quota
))
.
Error
err
=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"quota"
,
gorm
.
Expr
(
"quota - ?"
,
quota
))
.
Error
return
err
return
err
...
...
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