Files
SaveAny-Bot/database/user.go
Copilot 3e20dc2c5f feat: add custom parameter support to /ytdlp command (#185), close #184
* Initial plan

* Implement parameter support for /ytdlp command

Co-authored-by: krau <71133316+krau@users.noreply.github.com>

* Add comprehensive tests for ytdlp parameter parsing

Co-authored-by: krau <71133316+krau@users.noreply.github.com>

* Improve flag parsing logic and clarify argument order

Co-authored-by: krau <71133316+krau@users.noreply.github.com>

* Preserve critical defaults and improve comments

Co-authored-by: krau <71133316+krau@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: krau <71133316+krau@users.noreply.github.com>
2026-01-19 13:10:21 +08:00

53 lines
1.2 KiB
Go

package database
import (
"context"
"gorm.io/gorm/clause"
)
func CreateUser(ctx context.Context, chatID int64) error {
if _, err := GetUserByChatID(ctx, chatID); err == nil {
return nil
}
return db.Create(&User{ChatID: chatID}).Error
}
func GetAllUsers(ctx context.Context) ([]User, error) {
var users []User
err := db.WithContext(ctx).
Preload(clause.Associations).
Find(&users).Error
return users, err
}
func GetUserByChatID(ctx context.Context, chatID int64) (*User, error) {
var user User
err := db.WithContext(ctx).
Preload(clause.Associations).
Where("chat_id = ?", chatID).First(&user).Error
return &user, err
}
func UpdateUser(ctx context.Context, user *User) error {
if _, err := GetUserByChatID(ctx, user.ChatID); err != nil {
return err
}
return db.WithContext(ctx).Save(user).Error
}
func DeleteUser(ctx context.Context, user *User) error {
return db.WithContext(ctx).
Unscoped().
Select(clause.Associations).
Delete(user).Error
}
func GetUserByID(ctx context.Context, id uint) (*User, error) {
var user User
err := db.WithContext(ctx).
Preload(clause.Associations).
Where("id = ?", id).First(&user).Error
return &user, err
}