vegapokerbot/internal/db/repository/user.go

55 lines
1.3 KiB
Go
Raw Permalink Normal View History

2024-05-07 21:49:09 +03:00
package repository
import (
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/util"
"gorm.io/gorm"
)
type User struct {
db *gorm.DB
}
func NewUser(db *gorm.DB) *User {
return &User{db: db}
}
func (u *User) ByID(id uint64) (*model.User, error) {
2024-05-09 17:42:42 +03:00
var user model.User
if err := u.db.Model(&user).First(&user, id).Error; err != nil {
2024-05-07 21:49:09 +03:00
return nil, util.HandleRecordNotFound(err)
}
2024-05-09 17:42:42 +03:00
return &user, nil
}
func (u *User) ByTelegramID(id int64) (*model.User, error) {
var user model.User
2024-05-23 14:22:44 +03:00
if err := u.db.Model(&model.User{}).Where("telegram_id = ?", id).First(&user).Error; err != nil {
2024-05-09 17:42:42 +03:00
return nil, util.HandleRecordNotFound(err)
}
return &user, nil
2024-05-07 21:49:09 +03:00
}
func (u *User) ByTelegramIDs(ids []int64) ([]model.User, error) {
var users []model.User
2024-05-23 14:22:44 +03:00
if err := u.db.Model(&model.User{}).Where("telegram_id in (?)", ids).Find(&users).Error; err != nil {
return nil, util.HandleRecordNotFound(err)
}
return users, nil
}
2024-05-07 21:49:09 +03:00
func (u *User) ByIDWithChats(id uint64) (*model.User, error) {
2024-05-09 17:42:42 +03:00
var user model.User
if err := u.db.Model(&user).Preload("Chats").First(&user, id).Error; err != nil {
2024-05-07 21:49:09 +03:00
return nil, util.HandleRecordNotFound(err)
}
2024-05-09 17:42:42 +03:00
return &user, nil
}
func (u *User) Save(user *model.User) error {
2024-05-23 13:55:05 +03:00
if user.ID == 0 {
return u.db.Create(user).Error
}
return u.db.Model(user).Save(user).Error
2024-05-07 21:49:09 +03:00
}