mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 19:40:11 +08:00
refactor: add getOrCreateSession to simplify process
This commit is contained in:
1 parent
610894e6d8
commit
801d1aa07b
6 files changed
+68
-28
No files matched your search
@@ -58,34 +58,14 @@ class SessionManager {
|
|||||||
this.setSession(data)
|
this.setSession(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSessionList() {
|
async getActiveSession() {
|
||||||
const res = await api.get('/sessions', {
|
const res = await api.post('/sessions/active', {
|
||||||
params: { quest_id: this.questId },
|
quest_id: this.questId,
|
||||||
})
|
})
|
||||||
const { data } = res
|
const session = res.data
|
||||||
return data
|
console.log(session)
|
||||||
}
|
|
||||||
|
|
||||||
async lastSession() {
|
|
||||||
const sessions = await this.getSessionList()
|
|
||||||
if (sessions.length === 0) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const { _id: sessionId } = sessions[sessions.length - 1]
|
|
||||||
const { data: session } = await api.get(`/sessions/${sessionId}`)
|
|
||||||
console.log(
|
|
||||||
`Last Session ID: ${session._id}, Quest: ${session.quest}, Created At: ${session.createdAt}`
|
|
||||||
)
|
|
||||||
this.setSession(session)
|
this.setSession(session)
|
||||||
}
|
}
|
||||||
|
|
||||||
async lastOrCreate() {
|
|
||||||
await this.lastSession()
|
|
||||||
if (!this.session.value) {
|
|
||||||
await this.createSession()
|
|
||||||
}
|
|
||||||
console.log(this.session.value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sessionManager = new SessionManager('helloworld')
|
const sessionManager = new SessionManager('helloworld')
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ const completed = computed(() => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await sessionManager.lastOrCreate()
|
await sessionManager.getActiveSession()
|
||||||
socket.connect(sessionManager.getSession())
|
socket.connect(sessionManager.getSession())
|
||||||
socket.on('connect', function open() {
|
socket.on('connect', function open() {
|
||||||
console.log('Connected to the server.')
|
console.log('Connected to the server.')
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import { Session } from '@linux-odyssey/models'
|
import { Session } from '@linux-odyssey/models'
|
||||||
import { createNewSession } from '../../game/sessionManager.js'
|
import {
|
||||||
|
createNewSession,
|
||||||
|
getOrCreateActiveSession,
|
||||||
|
} from '../../game/sessionManager.js'
|
||||||
|
|
||||||
function sessionSummary(session) {
|
function sessionSummary(session) {
|
||||||
return {
|
return {
|
||||||
@@ -82,3 +85,14 @@ export async function deleteSessionById(req, res) {
|
|||||||
res.status(500).json({ message: err.message })
|
res.status(500).json({ message: err.message })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getActiveSession(req, res) {
|
||||||
|
const { quest_id } = req.body
|
||||||
|
try {
|
||||||
|
const session = await getOrCreateActiveSession(req.user, quest_id)
|
||||||
|
res.json(sessionDetail(session))
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err)
|
||||||
|
res.status(500).json({ message: err.message })
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ const router = Router()
|
|||||||
|
|
||||||
router.get('/', sessionController.getSessionList)
|
router.get('/', sessionController.getSessionList)
|
||||||
router.post('/', sessionController.createSession)
|
router.post('/', sessionController.createSession)
|
||||||
|
router.post('/active', sessionController.getActiveSession)
|
||||||
router.get('/:id', sessionController.getSessionById)
|
router.get('/:id', sessionController.getSessionById)
|
||||||
router.delete('/:id', sessionController.deleteSessionById)
|
router.delete('/:id', sessionController.deleteSessionById)
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ async function deactivateSessions(user, quest) {
|
|||||||
})
|
})
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
sessions.map((session) => {
|
sessions.map((session) => {
|
||||||
return deleteContainer(session.containerId).then(() => {
|
return deleteContainer(session.containerId)
|
||||||
|
.catch(console.error)
|
||||||
|
.finally(() => {
|
||||||
session.status = 'inactive'
|
session.status = 'inactive'
|
||||||
session.containerId = null
|
session.containerId = null
|
||||||
return session.save()
|
return session.save()
|
||||||
@@ -48,3 +50,15 @@ export async function createNewSession(user, questId) {
|
|||||||
await session.save()
|
await session.save()
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getOrCreateActiveSession(user, questId) {
|
||||||
|
const session = await Session.findOne({
|
||||||
|
user,
|
||||||
|
quest: questId,
|
||||||
|
status: 'active',
|
||||||
|
})
|
||||||
|
if (session) {
|
||||||
|
return session
|
||||||
|
}
|
||||||
|
return createNewSession(user, questId)
|
||||||
|
}
|
||||||
@@ -80,6 +80,37 @@ paths:
|
|||||||
message:
|
message:
|
||||||
type: string
|
type: string
|
||||||
|
|
||||||
|
/sessions/active:
|
||||||
|
post:
|
||||||
|
summary: 'Get or create an active session.'
|
||||||
|
tags:
|
||||||
|
- signed-in
|
||||||
|
description: 'This endpoint returns the latest active session for the user, or creates a new one if no active session exists.'
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: 'object'
|
||||||
|
required:
|
||||||
|
- quest_id
|
||||||
|
properties:
|
||||||
|
quest_id:
|
||||||
|
type: 'string'
|
||||||
|
description: 'The ID of the quest.'
|
||||||
|
example: 'helloworld'
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: 'Return a active session.'
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/Session'
|
||||||
|
400:
|
||||||
|
description: 'Bad Request. The request is invalid or malformed.'
|
||||||
|
500:
|
||||||
|
description: 'Internal Server Error.'
|
||||||
|
|
||||||
/sessions/{session_id}:
|
/sessions/{session_id}:
|
||||||
get:
|
get:
|
||||||
tags:
|
tags:
|
||||||
|
|||||||
Reference in new issue
Block a user