Skip to content

Commit

Permalink
feat alpha: support sqlite and connection stack restruct
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Mar 1, 2024
1 parent d1573df commit 67cb512
Show file tree
Hide file tree
Showing 34 changed files with 429 additions and 140 deletions.
8 changes: 4 additions & 4 deletions admin/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func getFormat(t time.Time) string {

func GetSubscriptionUsers(db *sql.DB) int64 {
var count int64
err := db.QueryRow(`
err := globals.QueryRowDb(db, `
SELECT COUNT(*) FROM subscription WHERE expired_at > NOW()
`).Scan(&count)
if err != nil {
Expand Down Expand Up @@ -117,15 +117,15 @@ func GetUserTypeData(db *sql.DB) (UserTypeForm, error) {
var form UserTypeForm

// get total users
if err := db.QueryRow(`
if err := globals.QueryRowDb(db, `
SELECT COUNT(*) FROM auth
`).Scan(&form.Total); err != nil {
return form, err
}

// get subscription users count (current subscription)
// level 1: basic plan, level 2: standard plan, level 3: pro plan
if err := db.QueryRow(`
if err := globals.QueryRowDb(db, `
SELECT
(SELECT COUNT(*) FROM subscription WHERE level = 1 AND expired_at > NOW()),
(SELECT COUNT(*) FROM subscription WHERE level = 2 AND expired_at > NOW()),
Expand All @@ -136,7 +136,7 @@ func GetUserTypeData(db *sql.DB) (UserTypeForm, error) {

// get normal users count (no subscription in `subscription` table and `quota` + `used` < initial quota in `quota` table)
initialQuota := channel.SystemInstance.GetInitialQuota()
if err := db.QueryRow(`
if err := globals.QueryRowDb(db, `
SELECT COUNT(*) FROM auth
WHERE id NOT IN (SELECT user_id FROM subscription WHERE total_month > 0)
AND id IN (SELECT user_id FROM quota WHERE quota + used <= ?)
Expand Down
7 changes: 4 additions & 3 deletions admin/invitation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package admin

import (
"chat/globals"
"chat/utils"
"database/sql"
"errors"
Expand All @@ -12,7 +13,7 @@ import (
func GetInvitationPagination(db *sql.DB, page int64) PaginationForm {
var invitations []interface{}
var total int64
if err := db.QueryRow(`
if err := globals.QueryRowDb(db, `
SELECT COUNT(*) FROM invitation
`).Scan(&total); err != nil {
return PaginationForm{
Expand All @@ -21,7 +22,7 @@ func GetInvitationPagination(db *sql.DB, page int64) PaginationForm {
}
}

rows, err := db.Query(`
rows, err := globals.QueryDb(db, `
SELECT code, quota, type, used, updated_at FROM invitation
ORDER BY id DESC LIMIT ? OFFSET ?
`, pagination, page*pagination)
Expand Down Expand Up @@ -53,7 +54,7 @@ func GetInvitationPagination(db *sql.DB, page int64) PaginationForm {
}

func NewInvitationCode(db *sql.DB, code string, quota float32, t string) error {
_, err := db.Exec(`
_, err := globals.ExecDb(db, `
INSERT INTO invitation (code, quota, type)
VALUES (?, ?, ?)
`, code, quota, t)
Expand Down
5 changes: 3 additions & 2 deletions admin/redeem.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package admin

import (
"chat/globals"
"chat/utils"
"database/sql"
"fmt"
Expand All @@ -10,7 +11,7 @@ import (
func GetRedeemData(db *sql.DB) []RedeemData {
var data []RedeemData

rows, err := db.Query(`
rows, err := globals.QueryDb(db, `
SELECT quota, COUNT(*) AS total, SUM(IF(used = 0, 0, 1)) AS used
FROM redeem
GROUP BY quota
Expand Down Expand Up @@ -54,7 +55,7 @@ func GenerateRedeemCodes(db *sql.DB, num int, quota float32) RedeemGenerateRespo

func CreateRedeemCode(db *sql.DB, quota float32) (string, error) {
code := fmt.Sprintf("nio-%s", utils.GenerateChar(32))
_, err := db.Exec(`
_, err := globals.ExecDb(db, `
INSERT INTO redeem (code, quota) VALUES (?, ?)
`, code, quota)

Expand Down
25 changes: 13 additions & 12 deletions admin/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package admin

import (
"chat/channel"
"chat/globals"
"chat/utils"
"context"
"database/sql"
Expand Down Expand Up @@ -31,7 +32,7 @@ func getUsersForm(db *sql.DB, page int64, search string) PaginationForm {
var users []interface{}
var total int64

if err := db.QueryRow(`
if err := globals.QueryRowDb(db, `
SELECT COUNT(*) FROM auth
WHERE username LIKE ?
`, "%"+search+"%").Scan(&total); err != nil {
Expand All @@ -41,7 +42,7 @@ func getUsersForm(db *sql.DB, page int64, search string) PaginationForm {
}
}

rows, err := db.Query(`
rows, err := globals.QueryDb(db, `
SELECT
auth.id, auth.username, auth.email, auth.is_admin,
quota.quota, quota.used,
Expand Down Expand Up @@ -116,7 +117,7 @@ func passwordMigration(db *sql.DB, cache *redis.Client, id int64, password strin
return fmt.Errorf("password length must be between 6 and 36")
}

_, err := db.Exec(`
_, err := globals.ExecDb(db, `
UPDATE auth SET password = ? WHERE id = ?
`, utils.Sha2Encrypt(password), id)

Expand All @@ -126,23 +127,23 @@ func passwordMigration(db *sql.DB, cache *redis.Client, id int64, password strin
}

func emailMigration(db *sql.DB, id int64, email string) error {
_, err := db.Exec(`
_, err := globals.ExecDb(db, `
UPDATE auth SET email = ? WHERE id = ?
`, email, id)

return err
}

func setAdmin(db *sql.DB, id int64, isAdmin bool) error {
_, err := db.Exec(`
_, err := globals.ExecDb(db, `
UPDATE auth SET is_admin = ? WHERE id = ?
`, isAdmin, id)

return err
}

func banUser(db *sql.DB, id int64, isBanned bool) error {
_, err := db.Exec(`
_, err := globals.ExecDb(db, `
UPDATE auth SET is_banned = ? WHERE id = ?
`, isBanned, id)

Expand All @@ -154,15 +155,15 @@ func quotaMigration(db *sql.DB, id int64, quota float32, override bool) error {
// if quota is positive, then increase quota

if override {
_, err := db.Exec(`
_, err := globals.ExecDb(db, `
INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE quota = ?
`, id, quota, 0., quota)

return err
}

_, err := db.Exec(`
_, err := globals.ExecDb(db, `
INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE quota = quota + ?
`, id, quota, 0., quota)
Expand All @@ -176,7 +177,7 @@ func subscriptionMigration(db *sql.DB, id int64, month int64) error {

expireAt := time.Now().AddDate(0, int(month), 0)

_, err := db.Exec(`
_, err := globals.ExecDb(db, `
INSERT INTO subscription (user_id, total_month, expired_at) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE total_month = total_month + ?, expired_at = DATE_ADD(expired_at, INTERVAL ? MONTH)
`, id, month, expireAt, month, month)
Expand All @@ -189,7 +190,7 @@ func subscriptionLevelMigration(db *sql.DB, id int64, level int64) error {
return fmt.Errorf("invalid subscription level")
}

_, err := db.Exec(`
_, err := globals.ExecDb(db, `
INSERT INTO subscription (user_id, level) VALUES (?, ?)
ON DUPLICATE KEY UPDATE level = ?
`, id, level, level)
Expand All @@ -199,7 +200,7 @@ func subscriptionLevelMigration(db *sql.DB, id int64, level int64) error {

func releaseUsage(db *sql.DB, cache *redis.Client, id int64) error {
var level sql.NullInt64
if err := db.QueryRow(`
if err := globals.QueryRowDb(db, `
SELECT level FROM subscription WHERE user_id = ?
`, id).Scan(&level); err != nil {
return err
Expand All @@ -225,7 +226,7 @@ func UpdateRootPassword(db *sql.DB, cache *redis.Client, password string) error
return fmt.Errorf("password length must be between 6 and 36")
}

if _, err := db.Exec(`
if _, err := globals.ExecDb(db, `
UPDATE auth SET password = ? WHERE username = 'root'
`, utils.Sha2Encrypt(password)); err != nil {
return err
Expand Down
Loading

0 comments on commit 67cb512

Please sign in to comment.