feat: 新增Chat会话和群聊API
- 新增chat_session相关模型、仓库和服务 - 新增chat_group相关模型、仓库和服务 - 新增session_handler和chat_group_handler - 实现会话管理和群聊功能 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
56
server/internal/repository/chat_group_repo.go
Normal file
56
server/internal/repository/chat_group_repo.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"x-agents/server/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ChatGroupRepository 群聊仓储
|
||||
type ChatGroupRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewChatGroupRepository 创建群聊仓储
|
||||
func NewChatGroupRepository(db *gorm.DB) *ChatGroupRepository {
|
||||
return &ChatGroupRepository{db: db}
|
||||
}
|
||||
|
||||
// Create 创建群聊
|
||||
func (r *ChatGroupRepository) Create(group *model.ChatGroup) error {
|
||||
return r.db.Create(group).Error
|
||||
}
|
||||
|
||||
// FindByID 根据ID查询
|
||||
func (r *ChatGroupRepository) FindByID(id string) (*model.ChatGroup, error) {
|
||||
var group model.ChatGroup
|
||||
err := r.db.Where("id = ?", id).First(&group).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
// FindByUserID 根据用户ID查询群聊列表
|
||||
func (r *ChatGroupRepository) FindByUserID(userID string, limit, offset int) ([]model.ChatGroup, int64, error) {
|
||||
var groups []model.ChatGroup
|
||||
query := r.db.Model(&model.ChatGroup{}).Where("user_id = ?", userID)
|
||||
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err := query.Order("created_at DESC").Limit(limit).Offset(offset).Find(&groups).Error
|
||||
return groups, total, err
|
||||
}
|
||||
|
||||
// Update 更新群聊
|
||||
func (r *ChatGroupRepository) Update(group *model.ChatGroup) error {
|
||||
return r.db.Save(group).Error
|
||||
}
|
||||
|
||||
// Delete 删除群聊
|
||||
func (r *ChatGroupRepository) Delete(id string) error {
|
||||
return r.db.Delete(&model.ChatGroup{}, "id = ?", id).Error
|
||||
}
|
||||
86
server/internal/repository/chat_repository.go
Normal file
86
server/internal/repository/chat_repository.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"x-agents/server/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ChatRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewChatRepository(db *gorm.DB) *ChatRepository {
|
||||
return &ChatRepository{db: db}
|
||||
}
|
||||
|
||||
// Session CRUD
|
||||
|
||||
// CreateSession 创建会话
|
||||
func (r *ChatRepository) CreateSession(session *model.ChatSession) error {
|
||||
return r.db.Create(session).Error
|
||||
}
|
||||
|
||||
// GetSessionByID 根据ID获取会话
|
||||
func (r *ChatRepository) GetSessionByID(id string) (*model.ChatSession, error) {
|
||||
var session model.ChatSession
|
||||
err := r.db.Where("id = ?", id).First(&session).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &session, nil
|
||||
}
|
||||
|
||||
// GetSessionsByUserID 获取用户的所有会话
|
||||
func (r *ChatRepository) GetSessionsByUserID(userID string, limit, offset int) ([]model.ChatSession, int64, error) {
|
||||
var sessions []model.ChatSession
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&model.ChatSession{}).Where("user_id = ?", userID)
|
||||
query.Count(&total)
|
||||
|
||||
err := query.Order("updated_at DESC").Limit(limit).Offset(offset).Find(&sessions).Error
|
||||
return sessions, total, err
|
||||
}
|
||||
|
||||
// UpdateSession 更新会话
|
||||
func (r *ChatRepository) UpdateSession(session *model.ChatSession) error {
|
||||
return r.db.Save(session).Error
|
||||
}
|
||||
|
||||
// DeleteSession 删除会话
|
||||
func (r *ChatRepository) DeleteSession(id string) error {
|
||||
// 先删除会话下的所有消息
|
||||
r.db.Where("session_id = ?", id).Delete(&model.ChatMessage{})
|
||||
// 再删除会话
|
||||
return r.db.Delete(&model.ChatSession{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
// Message CRUD
|
||||
|
||||
// CreateMessage 创建消息
|
||||
func (r *ChatRepository) CreateMessage(message *model.ChatMessage) error {
|
||||
return r.db.Create(message).Error
|
||||
}
|
||||
|
||||
// GetMessagesBySessionID 获取会话的所有消息
|
||||
func (r *ChatRepository) GetMessagesBySessionID(sessionID string, limit, offset int) ([]model.ChatMessage, int64, error) {
|
||||
var messages []model.ChatMessage
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&model.ChatMessage{}).Where("session_id = ?", sessionID)
|
||||
query.Count(&total)
|
||||
|
||||
err := query.Order("created_at ASC").Limit(limit).Offset(offset).Find(&messages).Error
|
||||
return messages, total, err
|
||||
}
|
||||
|
||||
// DeleteMessage 删除消息
|
||||
func (r *ChatRepository) DeleteMessage(id string) error {
|
||||
return r.db.Delete(&model.ChatMessage{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
// DeleteMessagesBySessionID 删除会话的所有消息
|
||||
func (r *ChatRepository) DeleteMessagesBySessionID(sessionID string) error {
|
||||
return r.db.Where("session_id = ?", sessionID).Delete(&model.ChatMessage{}).Error
|
||||
}
|
||||
Reference in New Issue
Block a user