From 4fd0b247d285b1781165b1366698ac890a814cb4 Mon Sep 17 00:00:00 2001 From: "Laisky.Cai" Date: Tue, 25 Jun 2024 06:15:07 +0000 Subject: [PATCH] chore: decrease token quota in ConsumeToken function This commit modifies the ConsumeToken function in the token.go file to decrease the token quota by the specified amount. It replaces the previous code that directly updated the quota values with a call to the DecreaseTokenQuota function from the model package. This change improves code clarity and maintainability. Closes #123 --- controller/token.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/controller/token.go b/controller/token.go index ccfc1dec..ac8f12c1 100644 --- a/controller/token.go +++ b/controller/token.go @@ -279,8 +279,14 @@ func ConsumeToken(c *gin.Context) { return } - cleanToken.UsedQuota += int64(tokenPatch.AddUsedQuota) - cleanToken.RemainQuota -= int64(tokenPatch.AddUsedQuota) + if err = model.DecreaseTokenQuota(cleanToken.Id, int64(tokenPatch.AddUsedQuota)); err != nil { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": err.Error(), + }) + return + } + model.RecordConsumeLog(c.Request.Context(), userID, 0, 0, 0, tokenPatch.AddReason, cleanToken.Name, int64(tokenPatch.AddUsedQuota),