You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.5 KiB
60 lines
1.5 KiB
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"healthapi/internal/model"
|
|
"healthapi/internal/svc"
|
|
"healthapi/internal/types"
|
|
"healthapi/pkg/errorx"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetConversationLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewGetConversationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetConversationLogic {
|
|
return &GetConversationLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *GetConversationLogic) GetConversation(req *types.ConversationIdReq) (resp *types.ConversationDetailResp, err error) {
|
|
userID, err := GetUserIDFromCtx(l.ctx)
|
|
if err != nil {
|
|
return nil, errorx.ErrUnauthorized
|
|
}
|
|
|
|
var conversation model.Conversation
|
|
if err := l.svcCtx.DB.Where("id = ? AND user_id = ?", req.Id, userID).First(&conversation).Error; err != nil {
|
|
return nil, errorx.ErrNotFound
|
|
}
|
|
|
|
var messages []model.Message
|
|
l.svcCtx.DB.Where("conversation_id = ?", conversation.ID).Order("created_at ASC").Find(&messages)
|
|
|
|
resp = &types.ConversationDetailResp{
|
|
ID: uint(conversation.ID),
|
|
Title: conversation.Title,
|
|
Messages: make([]types.Message, 0, len(messages)),
|
|
CreatedAt: conversation.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
for _, m := range messages {
|
|
resp.Messages = append(resp.Messages, types.Message{
|
|
ID: uint(m.ID),
|
|
ConversationID: m.ConversationID,
|
|
Role: m.Role,
|
|
Content: m.Content,
|
|
CreatedAt: m.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
})
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|