36 lines
863 B
Go
36 lines
863 B
Go
package red
|
|
|
|
import (
|
|
"fmt"
|
|
"ymgb/database"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type AiRepository struct {
|
|
client *redis.Client
|
|
}
|
|
|
|
func NewAiRepository(db *database.Context) AiRepository {
|
|
return AiRepository{client: db.Redis}
|
|
}
|
|
|
|
func (rep AiRepository) SetChatId(userId int, chatId string) error {
|
|
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
|
|
return rep.client.Set(ctx, key, chatId, 0).Err()
|
|
}
|
|
func (rep AiRepository) GetChatId(userId int) (string, error) {
|
|
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
|
|
return rep.client.Get(ctx, key).Result()
|
|
}
|
|
func (rep AiRepository) GetOrCreateChatId(userId int) (string, error) {
|
|
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
|
|
res := rep.client.Get(ctx, key)
|
|
if res.Err() != nil {
|
|
chatId := uuid.New().String()
|
|
return chatId, rep.SetChatId(userId, chatId)
|
|
}
|
|
return res.Result()
|
|
}
|