feat!: (WIP) decouple storage, users, and configuration files to support multiple users

This commit is contained in:
krau
2025-02-18 17:17:02 +08:00
parent 9367419156
commit 968547b005
21 changed files with 474 additions and 372 deletions

View File

@@ -4,16 +4,25 @@ import (
"github.com/krau/SaveAny-Bot/types"
)
func CreateUser(userID int64) error {
if _, err := GetUserByUserID(userID); err == nil {
func CreateUser(chatID int64) error {
if _, err := GetUserByChatID(chatID); err == nil {
return nil
}
return db.Create(&types.User{UserID: userID}).Error
return db.Create(&types.User{ChatID: chatID}).Error
}
func GetUserByUserID(userID int64) (*types.User, error) {
// GetUserByUserID gets a user by their telegram user ID
//
// Return with active storages
func GetUserByChatID(chatID int64) (*types.User, error) {
var user types.User
err := db.Where("user_id = ?", userID).First(&user).Error
err := db.Preload("Storages", "active = ?", true).Where("chat_id = ?", chatID).First(&user).Error
return &user, err
}
func GetUserWithAllStoragesByChatID(chatID int64) (*types.User, error) {
var user types.User
err := db.Preload("Storages").Where("chat_id = ?", chatID).First(&user).Error
return &user, err
}