70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import { RecordModel } from "pocketbase"
|
|
|
|
import { managePbError } from "../../utils/pbTools"
|
|
import pbClient from "../pbClient"
|
|
|
|
const DB_NAME = "soupGame"
|
|
|
|
export interface SoupGame {
|
|
chatId: string
|
|
title: string
|
|
query: string
|
|
answer: string
|
|
history: string[]
|
|
active: boolean
|
|
}
|
|
|
|
export type SoupGameModel = SoupGame & RecordModel
|
|
|
|
/**
|
|
* 创建一个新的SoupGame记录
|
|
* @param {SoupGame} soupGame - SoupGame对象
|
|
* @returns {Promise<SoupGameModel>} - 创建的SoupGame记录
|
|
*/
|
|
const create = (soupGame: SoupGame) =>
|
|
managePbError<SoupGameModel>(() =>
|
|
pbClient.collection(DB_NAME).create(soupGame)
|
|
)
|
|
|
|
/**
|
|
* 根据chatId获取一个活跃的SoupGame记录
|
|
* @param {string} chatId - 聊天ID
|
|
* @returns {Promise<SoupGameModel>} - 获取的SoupGame记录
|
|
*/
|
|
const getActiveOneByChatId = (chatId: string) =>
|
|
managePbError<SoupGameModel>(() =>
|
|
pbClient
|
|
.collection(DB_NAME)
|
|
.getFirstListItem(`chatId = "${chatId}" && active = true`)
|
|
)
|
|
|
|
/**
|
|
* 根据chatId关闭一个SoupGame记录
|
|
* @param {string} chatId - 聊天ID
|
|
* @returns {Promise<SoupGameModel>} - 更新的SoupGame记录
|
|
*/
|
|
const close = (id: string) =>
|
|
managePbError<SoupGameModel>(() =>
|
|
pbClient.collection(DB_NAME).update(id, { active: false })
|
|
)
|
|
|
|
/**
|
|
* 根据chatId插入历史记录
|
|
* @param {string} chatId - 聊天ID
|
|
* @param {string} history - 历史记录
|
|
* @returns {Promise<SoupGameModel>} - 更新的SoupGame记录
|
|
*/
|
|
const insertHistory = (id: string, history: string[]) =>
|
|
managePbError<SoupGameModel>(() =>
|
|
pbClient.collection(DB_NAME).update(id, { history })
|
|
)
|
|
|
|
const soupGame = {
|
|
create,
|
|
getActiveOneByChatId,
|
|
close,
|
|
insertHistory,
|
|
}
|
|
|
|
export default soupGame
|