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 Chat struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChat(db *gorm.DB) *Chat {
|
|
|
|
return &Chat{db: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chat) ByID(id uint64) (*model.Chat, error) {
|
2024-05-09 17:42:42 +03:00
|
|
|
var chat model.Chat
|
|
|
|
if err := c.db.Model(&chat).First(&chat, 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 &chat, nil
|
2024-05-07 21:49:09 +03:00
|
|
|
}
|
|
|
|
|
2024-05-09 20:48:59 +03:00
|
|
|
func (c *Chat) ByTelegramID(id int64) (*model.Chat, error) {
|
|
|
|
var chat model.Chat
|
2024-05-23 14:22:44 +03:00
|
|
|
if err := c.db.Model(&model.Chat{}).Where("telegram_id = ?", id).First(&chat).Error; err != nil {
|
2024-05-09 20:48:59 +03:00
|
|
|
return nil, util.HandleRecordNotFound(err)
|
|
|
|
}
|
|
|
|
return &chat, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chat) ByTelegramIDWithIntegrations(id int64) (*model.Chat, error) {
|
|
|
|
var chat model.Chat
|
2024-05-23 14:22:44 +03:00
|
|
|
if err := c.db.Model(&model.Chat{}).Where("telegram_id = ?", id).Preload("Integrations").First(&chat).Error; err != nil {
|
2024-05-09 20:48:59 +03:00
|
|
|
return nil, util.HandleRecordNotFound(err)
|
|
|
|
}
|
|
|
|
return &chat, nil
|
|
|
|
}
|
|
|
|
|
2024-05-07 21:49:09 +03:00
|
|
|
func (c *Chat) ByIDWithIntegrations(id uint64) (*model.Chat, error) {
|
2024-05-09 17:42:42 +03:00
|
|
|
var chat model.Chat
|
|
|
|
if err := c.db.Model(&chat).Preload("Integrations").First(&chat, 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 &chat, nil
|
2024-05-07 21:49:09 +03:00
|
|
|
}
|
2024-05-09 20:48:59 +03:00
|
|
|
|
|
|
|
func (c *Chat) Save(chat *model.Chat) error {
|
2024-05-23 13:55:05 +03:00
|
|
|
if chat.ID == 0 {
|
|
|
|
return c.db.Create(chat).Error
|
|
|
|
}
|
2024-05-09 20:48:59 +03:00
|
|
|
return c.db.Model(chat).Save(chat).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chat) Delete(chat *model.Chat) error {
|
|
|
|
return c.db.Model(&model.Chat{ID: chat.ID}).Delete(chat).Error
|
|
|
|
}
|