-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13c1295
commit d278f24
Showing
29 changed files
with
687 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package admin | ||
|
||
import ( | ||
"chat/utils" | ||
"database/sql" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func GetRedeemData(db *sql.DB) []RedeemData { | ||
var data []RedeemData | ||
|
||
rows, err := db.Query(` | ||
SELECT quota, COUNT(*) AS total, SUM(IF(used = 0, 0, 1)) AS used | ||
FROM redeem | ||
GROUP BY quota | ||
`) | ||
if err != nil { | ||
return data | ||
} | ||
|
||
for rows.Next() { | ||
var d RedeemData | ||
if err := rows.Scan(&d.Quota, &d.Total, &d.Used); err != nil { | ||
return data | ||
} | ||
data = append(data, d) | ||
} | ||
|
||
return data | ||
} | ||
|
||
func GenerateRedeemCodes(db *sql.DB, num int, quota float32) RedeemGenerateResponse { | ||
arr := make([]string, 0) | ||
idx := 0 | ||
for idx < num { | ||
code, err := CreateRedeemCode(db, quota) | ||
|
||
if err != nil { | ||
return RedeemGenerateResponse{ | ||
Status: false, | ||
Message: err.Error(), | ||
} | ||
} | ||
arr = append(arr, code) | ||
idx++ | ||
} | ||
|
||
return RedeemGenerateResponse{ | ||
Status: true, | ||
Data: arr, | ||
} | ||
} | ||
|
||
func CreateRedeemCode(db *sql.DB, quota float32) (string, error) { | ||
code := fmt.Sprintf("nio-%s", utils.GenerateChar(32)) | ||
_, err := db.Exec(` | ||
INSERT INTO redeem (code, quota) VALUES (?, ?) | ||
`, code, quota) | ||
|
||
if err != nil && strings.Contains(err.Error(), "Duplicate entry") { | ||
// code name is duplicate | ||
return CreateRedeemCode(db, quota) | ||
} | ||
|
||
return code, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import axios from "axios"; | ||
import { getErrorMessage } from "@/utils/base.ts"; | ||
|
||
export type RedeemResponse = { | ||
status: boolean; | ||
error: string; | ||
quota: number; | ||
}; | ||
|
||
export async function useRedeem(code: string): Promise<RedeemResponse> { | ||
try { | ||
const resp = await axios.get(`/redeem?code=${code}`); | ||
return resp.data as RedeemResponse; | ||
} catch (e) { | ||
console.debug(e); | ||
return { | ||
status: false, | ||
error: `network error: ${getErrorMessage(e)}`, | ||
quota: 0, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.