diff --git a/serve/src/api.ts b/serve/src/api.ts index 6a2626c..8b98a4f 100644 --- a/serve/src/api.ts +++ b/serve/src/api.ts @@ -13,7 +13,7 @@ app.use(bodyParser.json()); * @apiGroup Survey 问卷调查 * @apiVersion 0.1.1 */ -app.post('/survey', async(req: Request, res: Response) => { +app.post('/survey', async (req: Request, res: Response) => { const { name, age, gender, surveyResult } = req.body; if (!name || !age || !gender || !surveyResult) { // 如果请求参数不完整,则返回错误信息 @@ -31,7 +31,7 @@ app.post('/survey', async(req: Request, res: Response) => { * @apiGroup Survey 问卷调查 * @apiVersion 0.1.0 */ -app.get('/survey', async(_req: Request, res: Response) => { +app.get('/survey', async (_req: Request, res: Response) => { const data = await survey.list(); // 查询数据 console.log(`查询到 ${data.data.length} 条问卷调查数据`); // 控制台输出提示信息 res.json(data); @@ -43,7 +43,7 @@ app.get('/survey', async(_req: Request, res: Response) => { * @apiGroup Survey 问卷调查 * @apiVersion 0.1.0 */ -app.get('/survey/question/:id', async(req: Request, res: Response) => { +app.get('/survey/question/:id', async (req: Request, res: Response) => { const data = await survey.findById(req.params.id); // 根据 ID 查询数据 console.log(`查询问卷调查数据,id 为 ${req.params.id}`); // 控制台输出提示信息 res.status(data.code).json(data); @@ -55,7 +55,7 @@ app.get('/survey/question/:id', async(req: Request, res: Response) => { * @apiGroup Survey 问卷调查 * @apiVersion 0.2.0 */ -app.get('/survey/questions', async(req: Request, res: Response>) => { +app.get('/survey/questions', async (req: Request, res: Response>) => { const result = await survey.getQuestions(); res.json(result); }); @@ -70,7 +70,7 @@ app.get('/survey/questions', async(req: Request, res: Response>) = * "userName": "张三" *} */ -app.get('/survey/data', async(req: Request, res: Response>) => { +app.get('/survey/data', async (req: Request, res: Response>) => { const { surveyName, userName } = req.query; const result = await survey.querySurveyData(surveyName as string, userName as string); res.json(result); @@ -91,7 +91,7 @@ app.get('/survey/data', async(req: Request, res: Response>) => { * "userName": "张三" * } */ -app.post('/survey/questions', async(req: Request, res: Response) => { +app.post('/survey/questions', async (req: Request, res: Response) => { const { questions, surveyName, userName } = req.body; const result = await survey.setQuestions(questions, surveyName, userName); res.json(result); diff --git a/serve/src/store.ts b/serve/src/store.ts index d0b5a36..bab8a8a 100644 --- a/serve/src/store.ts +++ b/serve/src/store.ts @@ -150,9 +150,9 @@ class Store { public async saveQuestions(questions: string[], surveyName: string, userName: string): Promise { // 设置创建时间与id const newQuestion = { - id : uuidv4(), + id: uuidv4(), createTime: new Date().toISOString(), - questions + questions, }; // 根据用户名创建文件夹,文件夹里存放该用户创建的调查问卷 const userDir = path.resolve(__dirname, `data/${userName}`); @@ -163,9 +163,9 @@ class Store { console.log(`已将提问者 ${userName} 的问题列表${surveyName}_question.json保存至 ${filePath} 文件中`); return { - code : 0, + code: 0, message: '设置成功,问题列表已更新', - data : newQuestion + data: newQuestion, }; } } diff --git a/serve/src/survey.ts b/serve/src/survey.ts index 1220f19..eb784fb 100644 --- a/serve/src/survey.ts +++ b/serve/src/survey.ts @@ -41,15 +41,15 @@ class Survey { async save(data: Omit): Promise> { const newData: SurveyData = { ...data, - id : uuidv4(), - createdAt: Date.now() + id: uuidv4(), + createdAt: Date.now(), }; // 生成新数据并保存 await this.store.save(newData); console.log(`已将问卷调查数据 ${JSON.stringify(newData)} 保存至文件`); // 控制台输出提示信息 return { - code : 0, + code: 0, message: '提交成功', - data : newData + data: newData, }; } @@ -63,9 +63,9 @@ class Survey { const data = await this.store.read(); // 从数据存储对象中读取数据 console.log(`查询到 ${data.length} 条问卷调查数据`); // 控制台输出提示信息 return { - code : 0, + code: 0, message: '查询成功', - data : data.sort((a, b) => b.createdAt - a.createdAt) // 根据创建时间倒序排序 + data: data.sort((a, b) => b.createdAt - a.createdAt), // 根据创建时间倒序排序 }; } @@ -81,16 +81,16 @@ class Survey { if (result) { console.log(`查询问卷调查数据,id 为 ${id} 的数据存在`); // 控制台输出提示信息 return { - code : 0, + code: 0, message: '查询成功', - data : result + data: result, }; } else { console.log(`查询问卷调查数据,id 为 ${id} 的数据不存在`); // 控制台输出提示信息 return { - code : 404, + code: 404, message: '数据不存在', - data : undefined + data: undefined, }; } } @@ -103,9 +103,9 @@ class Survey { const questions = await this.store.readQuestions(); console.log(`查询到 ${questions.length} 条问题`); return { - code : 0, + code: 0, message: '查询成功', - data : questions + data: questions, }; } @@ -119,16 +119,16 @@ class Survey { const data = await this.store.querySurveyData(surveyName, userName); if (!data || !data.length) { return { - code : 404, + code: 404, message: '数据不存在,请检查调查问卷名称', - data : [] + data: [], }; } console.log(`查询${surveyName}调查问卷数据,共${data.length}条`); return { - code : 0, + code: 0, message: '查询成功', - data + data, }; } @@ -143,14 +143,14 @@ class Survey { async setQuestions(questions: any[], surveyName: string, userName: string): Promise { if (!questions || !Array.isArray(questions)) { return { - code : 400, + code: 400, message: '参数错误', - data : null + data: null, }; } const data = await this.store.saveQuestions(questions, surveyName, userName); return { - ...data + ...data, }; }