mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: add sessionManagerto connect session
This commit is contained in:
1 parent
5326fb30b4
commit
e122800c81
5 files changed
+52
-85
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import api from './api'
|
||||
|
||||
class SessionManager {
|
||||
constructor(questId) {
|
||||
this.questId = questId
|
||||
}
|
||||
|
||||
async createdSession() {
|
||||
console.log('Creating a new session...')
|
||||
const res = await api.post('/sessions', { quest_id: this.questId })
|
||||
const { data } = res
|
||||
console.log('Created Session ID:', data._id)
|
||||
return data
|
||||
}
|
||||
|
||||
async getSessionList() {
|
||||
const res = await api.get('/sessions', {
|
||||
params: { quest_id: this.questId },
|
||||
})
|
||||
const { data } = res
|
||||
return data
|
||||
}
|
||||
|
||||
async lastSession() {
|
||||
const sessions = await this.getSessionList()
|
||||
if (sessions.length === 0) {
|
||||
return null
|
||||
}
|
||||
const session = sessions[sessions.length - 1]
|
||||
console.log(
|
||||
`Last Session ID: ${session._id}, Quest: ${session.quest}, Created At: ${session.createdAt}`
|
||||
)
|
||||
return session
|
||||
}
|
||||
|
||||
async latestOrCreate() {
|
||||
return (await this.lastSession()) || this.createdSession()
|
||||
}
|
||||
}
|
||||
|
||||
const sessionManager = new SessionManager('helloworld')
|
||||
|
||||
export default sessionManager
|
||||
Reference in new issue
Block a user