Files
ragflow/internal/dao/chat_channel.go
Hz_ b48f03d0f5 feat(go/dao): migrate chat channel database entity and DAO to Go (#16055)
## Changes
1. **Entity (`internal/entity/chat_channel.go`)**:
- Implemented `ChatChannel` struct mapping the `chat_channel` database
table.
- Declared `ChatChannelListResponse` as a DTO to filter out sensitive
credentials (`config` field) and fetch the associated `dialog_name` via
left join.
2. **GORM Migration (`internal/dao/database.go`)**:
- Registered `&entity.ChatChannel{}` in the `dataModels` array inside
`InitDB()` to enable safe GORM schema synchronization.
3. **DAO (`internal/dao/chat_channel.go`)**:
- Implemented `ChatChannelDAO` wrapping GORM CRUD methods (`Create`,
`GetByID`, `UpdateByID`, `DeleteByID`).
- Implemented `ListByTenantID` performing a `LEFT JOIN` on the `dialog`
table to retrieve `dialog_name` while excluding `config` values to avoid
credential leaks.
4. **Test (`internal/dao/chat_channel_test.go`)**:
- Added integration unit tests testing the full CRUD lifecycle and GORM
left-join mapping list querying.
2026-06-17 11:26:13 +08:00

44 lines
1.4 KiB
Go

package dao
import "ragflow/internal/entity"
type ChatChannelDAO struct{}
func NewChatChannel() *ChatChannelDAO {
return &ChatChannelDAO{}
}
func (dao *ChatChannelDAO) Create(channel *entity.ChatChannel) error {
return DB.Create(channel).Error
}
func (dao *ChatChannelDAO) GetByID(id string, tenantID string) (*entity.ChatChannel, error) {
var channel entity.ChatChannel
err := DB.Where("id = ? AND tenant_id = ?", id, tenantID).First(&channel).Error
if err != nil {
return nil, err
}
return &channel, err
}
func (dao *ChatChannelDAO) UpdateByID(id string, tenantID string, updates map[string]any) error {
return DB.Model(&entity.ChatChannel{}).Where("id = ? AND tenant_id = ?", id, tenantID).Updates(updates).Error
}
func (dao *ChatChannelDAO) DeleteByID(id string, tenantID string) error {
return DB.Where("id = ? AND tenant_id = ?", id, tenantID).Delete(&entity.ChatChannel{}).Error
}
func (dao *ChatChannelDAO) ListByTenantID(tenantID string) ([]*entity.ChatChannelListResponse, error) {
results := make([]*entity.ChatChannelListResponse, 0)
err := DB.Table("chat_channel").
Select("chat_channel.id, chat_channel.name, chat_channel.channel, chat_channel.chat_id, chat_channel.status, dialog.name as dialog_name").
Joins("LEFT JOIN dialog ON dialog.id = chat_channel.chat_id").
Where("chat_channel.tenant_id = ?", tenantID).
Order("chat_channel.create_time DESC").
Scan(&results).Error
return results, err
}