mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 11:10:11 +08:00
feat: delete "deleteSession" method, validate questId and sessionId
This commit is contained in:
1 parent
a7422853fa
commit
5df4967a85
12 files changed
+67
-99
No files matched your search
@@ -13,7 +13,7 @@ export async function newCommand(req, res) {
|
||||
return
|
||||
}
|
||||
|
||||
const sessionId = req.user.session_id
|
||||
const { sessionId } = req.user
|
||||
|
||||
const session = await Session.findById(sessionId)
|
||||
|
||||
@@ -64,7 +64,7 @@ export async function newCommand(req, res) {
|
||||
}
|
||||
|
||||
export async function completedCommand(req, res) {
|
||||
const sessionId = req.user.session_id
|
||||
const { sessionId } = req.user
|
||||
|
||||
const session = await Session.findById(sessionId)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Quest } from '@linux-odyssey/models'
|
||||
import { matchedData } from 'express-validator'
|
||||
|
||||
export async function getQuests(req, res) {
|
||||
const quests = await Quest.find({}).sort({ order: 1 })
|
||||
@@ -12,8 +13,8 @@ export async function getQuests(req, res) {
|
||||
}
|
||||
|
||||
export async function getQuestDetail(req, res) {
|
||||
const { id } = req.params
|
||||
const quest = await Quest.findById(id)
|
||||
const { questId } = matchedData(req)
|
||||
const quest = await Quest.findById(questId)
|
||||
if (!quest) {
|
||||
res.status(404).json({ message: 'Quest not found.' })
|
||||
return
|
||||
|
||||
@@ -28,10 +28,11 @@ function sessionDetail(session) {
|
||||
export async function getSessionList(req, res) {
|
||||
try {
|
||||
const query = { user: req.user._id }
|
||||
if (req.query.quest_id) {
|
||||
query.quest = req.query.quest_id
|
||||
const { questId, status } = matchedData(req)
|
||||
if (questId) {
|
||||
query.quest = questId
|
||||
}
|
||||
query.status = req.query.status || 'active'
|
||||
query.status = status || 'active'
|
||||
const sessions = await Session.find(query)
|
||||
res.json(sessions.map(sessionSummary))
|
||||
} catch (err) {
|
||||
@@ -41,9 +42,9 @@ export async function getSessionList(req, res) {
|
||||
}
|
||||
|
||||
export async function createSession(req, res) {
|
||||
const { quest_id } = matchedData(req)
|
||||
const { questId } = matchedData(req)
|
||||
try {
|
||||
const session = await createNewSession(req.user, quest_id)
|
||||
const session = await createNewSession(req.user, questId)
|
||||
res.status(201).json(sessionDetail(session))
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
@@ -53,8 +54,9 @@ export async function createSession(req, res) {
|
||||
|
||||
export async function getSessionById(req, res) {
|
||||
try {
|
||||
const { sessionId } = matchedData(req)
|
||||
const session = await Session.findOne({
|
||||
_id: req.params.id,
|
||||
_id: sessionId,
|
||||
user: req.user._id,
|
||||
})
|
||||
|
||||
@@ -69,28 +71,10 @@ export async function getSessionById(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteSessionById(req, res) {
|
||||
try {
|
||||
const session = await Session.findOneAndDelete({
|
||||
_id: req.params.id,
|
||||
user: req.user._id,
|
||||
})
|
||||
|
||||
if (!session) {
|
||||
res.status(404).json({ message: 'Session not found.' })
|
||||
return
|
||||
}
|
||||
|
||||
res.status(204).end()
|
||||
} catch (err) {
|
||||
res.status(500).json({ message: err.message })
|
||||
}
|
||||
}
|
||||
|
||||
export async function getOrCreateSession(req, res) {
|
||||
const { quest_id } = matchedData(req)
|
||||
const { questId } = matchedData(req)
|
||||
try {
|
||||
const session = await getOrCreateActiveSession(req.user, quest_id)
|
||||
const session = await getOrCreateActiveSession(req.user, questId)
|
||||
res.json(sessionDetail(session))
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
|
||||
Reference in new issue
Block a user