| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package store
- import (
- "spider/internal/model"
- "strings"
- "gorm.io/gorm"
- )
- // UpsertChannel inserts or updates a channel by username.
- func (s *Store) UpsertChannel(ch *model.Channel) error {
- ch.Username = strings.TrimPrefix(strings.TrimSpace(ch.Username), "@")
- if ch.Username == "" {
- return nil
- }
- var existing model.Channel
- err := s.DB.Where("username = ?", ch.Username).First(&existing).Error
- if err == gorm.ErrRecordNotFound {
- return s.DB.Create(ch).Error
- }
- if err != nil {
- return err
- }
- // already exists, skip
- return nil
- }
- // ListPendingChannels returns channels with status=pending, up to limit.
- func (s *Store) ListPendingChannels(limit int) ([]model.Channel, error) {
- var channels []model.Channel
- q := s.DB.Where("status = ?", "pending")
- if limit > 0 {
- q = q.Limit(limit)
- }
- err := q.Find(&channels).Error
- return channels, err
- }
- // UpdateChannelStatus sets the status and optionally last_message_id.
- func (s *Store) UpdateChannelStatus(id uint, status string, lastMessageID int) error {
- updates := map[string]any{"status": status}
- if lastMessageID > 0 {
- updates["last_message_id"] = lastMessageID
- }
- return s.DB.Model(&model.Channel{}).Where("id = ?", id).Updates(updates).Error
- }
- // UpdateChannelEntity caches the TG entity IDs for a channel.
- func (s *Store) UpdateChannelEntity(id uint, channelID, accessHash int64) error {
- return s.DB.Model(&model.Channel{}).Where("id = ?", id).
- Updates(map[string]any{"channel_id": channelID, "access_hash": accessHash}).Error
- }
- // IncrementMerchantsFound increments the merchants_found counter.
- func (s *Store) IncrementMerchantsFound(id uint) error {
- return s.DB.Model(&model.Channel{}).Where("id = ?", id).
- Update("merchants_found", gorm.Expr("merchants_found + 1")).Error
- }
|