mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
Directly use folder name as _id in Quest
This commit is contained in:
1 parent
0d9b068678
commit
3e9e14399a
4 files changed
+17
-26
No files matched your search
@@ -4,7 +4,7 @@ export async function getQuests(req, res) {
|
|||||||
const quests = await Quest.find({}).sort({ order: 1 })
|
const quests = await Quest.find({}).sort({ order: 1 })
|
||||||
res.json(
|
res.json(
|
||||||
quests.map(({ name, title, order, _id }) => ({
|
quests.map(({ name, title, order, _id }) => ({
|
||||||
id: _id,
|
_id,
|
||||||
name,
|
name,
|
||||||
title,
|
title,
|
||||||
order,
|
order,
|
||||||
@@ -13,8 +13,8 @@ export async function getQuests(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getQuestDetail(req, res) {
|
export async function getQuestDetail(req, res) {
|
||||||
const { name } = req.params
|
const { id } = req.params
|
||||||
const quest = await Quest.findOne({ name })
|
const quest = await Quest.findById(id)
|
||||||
if (!quest) {
|
if (!quest) {
|
||||||
res.status(404).json({ message: 'Quest not found.' })
|
res.status(404).json({ message: 'Quest not found.' })
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -4,6 +4,6 @@ import * as questController from '../controllers/questController.js'
|
|||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
router.get('/', questController.getQuests)
|
router.get('/', questController.getQuests)
|
||||||
router.get('/:name', questController.getQuestDetail)
|
router.get('/:id', questController.getQuestDetail)
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
@@ -3,7 +3,7 @@ import { model, Schema } from 'mongoose'
|
|||||||
const Quest = model(
|
const Quest = model(
|
||||||
'Quest',
|
'Quest',
|
||||||
new Schema({
|
new Schema({
|
||||||
name: String,
|
_id: String,
|
||||||
title: String,
|
title: String,
|
||||||
order: Number,
|
order: Number,
|
||||||
content: String,
|
content: String,
|
||||||
|
|||||||
+12
-21
@@ -12,12 +12,12 @@ const marked = new Marked({
|
|||||||
|
|
||||||
const questDirectory = path.join(process.cwd(), 'quests')
|
const questDirectory = path.join(process.cwd(), 'quests')
|
||||||
|
|
||||||
async function parseQuests() {
|
export default async function loadAndUpdateQuests() {
|
||||||
const questNames = await fs.readdir(questDirectory)
|
const questNames = await fs.readdir(questDirectory)
|
||||||
|
|
||||||
const quests = Promise.all(
|
const quests = Promise.all(
|
||||||
questNames.map(async (name) => {
|
questNames.map(async (id) => {
|
||||||
const fullPath = path.join(questDirectory, name, 'README.md')
|
const fullPath = path.join(questDirectory, id, 'README.md')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Read and parse the README.md file.
|
// Read and parse the README.md file.
|
||||||
@@ -31,14 +31,20 @@ async function parseQuests() {
|
|||||||
|
|
||||||
const { title, order } = metadata
|
const { title, order } = metadata
|
||||||
|
|
||||||
return {
|
return Quest.findByIdAndUpdate(
|
||||||
name,
|
id,
|
||||||
|
{
|
||||||
|
_id: id,
|
||||||
title,
|
title,
|
||||||
order,
|
order,
|
||||||
content: htmlContent,
|
content: htmlContent,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
upsert: true,
|
||||||
}
|
}
|
||||||
|
)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error parsing quest ${name}:`, error)
|
console.error(`Error parsing quest ${id}:`, error)
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -46,18 +52,3 @@ async function parseQuests() {
|
|||||||
|
|
||||||
return quests
|
return quests
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateQuests(quests) {
|
|
||||||
Promise.all(
|
|
||||||
quests.map((quest) => {
|
|
||||||
return Quest.findOneAndUpdate({ name: quest.name }, quest, {
|
|
||||||
upsert: true,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export default async function loadAndUpdateQuests() {
|
|
||||||
const quests = await parseQuests()
|
|
||||||
await updateQuests(quests)
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user