From ac8e9d1cfad26bd813351e24189c3b67dc8a5a37 Mon Sep 17 00:00:00 2001 From: wancat Date: Thu, 22 Feb 2024 20:16:08 +0800 Subject: [PATCH] refactor quest controller --- server/src/api/controllers/questController.js | 25 ----------------- server/src/api/controllers/questController.ts | 28 +++++++++++++++++++ 2 files changed, 28 insertions(+), 25 deletions(-) delete mode 100644 server/src/api/controllers/questController.js create mode 100644 server/src/api/controllers/questController.ts diff --git a/server/src/api/controllers/questController.js b/server/src/api/controllers/questController.js deleted file mode 100644 index b27ab1e..0000000 --- a/server/src/api/controllers/questController.js +++ /dev/null @@ -1,25 +0,0 @@ -import { Quest } from '@linux-odyssey/models' -import { matchedData } from 'express-validator' -import { asyncHandler } from '../../middleware/error.js' - -export const getQuests = asyncHandler(async (req, res) => { - const quests = await Quest.find({}) - res.json( - quests.map(({ title, _id, requirements }) => ({ - _id, - title, - requirements, - })) - ) -}) - -export const getQuestDetail = asyncHandler(async (req, res) => { - const { questId } = matchedData(req) - const quest = await Quest.findById(questId) - if (!quest) { - res.status(404).json({ message: 'Quest not found.' }) - return - } - const { _id, title, order, instruction } = quest - res.json({ _id, title, order, instruction }) -}) diff --git a/server/src/api/controllers/questController.ts b/server/src/api/controllers/questController.ts new file mode 100644 index 0000000..e9a7660 --- /dev/null +++ b/server/src/api/controllers/questController.ts @@ -0,0 +1,28 @@ +import { Quest } from '@linux-odyssey/models' +import { matchedData } from 'express-validator' +import type { Request, Response } from 'express' +import { asyncHandler } from '../../middleware/error.js' + +export const getQuests = asyncHandler(async (req: Request, res: Response) => { + const quests = await Quest.find({}) + res.json( + quests.map(({ title, _id, requirements }) => ({ + _id, + title, + requirements, + })) + ) +}) + +export const getQuestDetail = asyncHandler( + async (req: Request, res: Response) => { + const { questId } = matchedData(req) + const quest = await Quest.findById(questId) + if (!quest) { + res.status(404).json({ message: 'Quest not found.' }) + return + } + const { _id, title, instruction, requirements } = quest + res.json({ _id, title, instruction, requirements }) + } +)