Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format code with prettier #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions serve/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ app.use(bodyParser.json());
* @apiGroup Survey 问卷调查
* @apiVersion 0.1.1
*/
app.post('/survey', async(req: Request, res: Response<Res>) => {
app.post('/survey', async (req: Request, res: Response<Res>) => {
const { name, age, gender, surveyResult } = req.body;
if (!name || !age || !gender || !surveyResult) {
// 如果请求参数不完整,则返回错误信息
Expand All @@ -31,7 +31,7 @@ app.post('/survey', async(req: Request, res: Response<Res>) => {
* @apiGroup Survey 问卷调查
* @apiVersion 0.1.0
*/
app.get('/survey', async(_req: Request, res: Response<Res>) => {
app.get('/survey', async (_req: Request, res: Response<Res>) => {
const data = await survey.list(); // 查询数据
console.log(`查询到 ${data.data.length} 条问卷调查数据`); // 控制台输出提示信息
res.json(data);
Expand All @@ -43,7 +43,7 @@ app.get('/survey', async(_req: Request, res: Response<Res>) => {
* @apiGroup Survey 问卷调查
* @apiVersion 0.1.0
*/
app.get('/survey/question/:id', async(req: Request, res: Response<Res>) => {
app.get('/survey/question/:id', async (req: Request, res: Response<Res>) => {
const data = await survey.findById(req.params.id); // 根据 ID 查询数据
console.log(`查询问卷调查数据,id 为 ${req.params.id}`); // 控制台输出提示信息
res.status(data.code).json(data);
Expand All @@ -55,7 +55,7 @@ app.get('/survey/question/:id', async(req: Request, res: Response<Res>) => {
* @apiGroup Survey 问卷调查
* @apiVersion 0.2.0
*/
app.get('/survey/questions', async(req: Request, res: Response<Res<string[]>>) => {
app.get('/survey/questions', async (req: Request, res: Response<Res<string[]>>) => {
const result = await survey.getQuestions();
res.json(result);
});
Expand All @@ -70,7 +70,7 @@ app.get('/survey/questions', async(req: Request, res: Response<Res<string[]>>) =
* "userName": "张三"
*}
*/
app.get('/survey/data', async(req: Request, res: Response<Res<any[]>>) => {
app.get('/survey/data', async (req: Request, res: Response<Res<any[]>>) => {
const { surveyName, userName } = req.query;
const result = await survey.querySurveyData(surveyName as string, userName as string);
res.json(result);
Expand All @@ -91,7 +91,7 @@ app.get('/survey/data', async(req: Request, res: Response<Res<any[]>>) => {
* "userName": "张三"
* }
*/
app.post('/survey/questions', async(req: Request, res: Response<Res>) => {
app.post('/survey/questions', async (req: Request, res: Response<Res>) => {
const { questions, surveyName, userName } = req.body;
const result = await survey.setQuestions(questions, surveyName, userName);
res.json(result);
Expand Down
8 changes: 4 additions & 4 deletions serve/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ class Store {
public async saveQuestions(questions: string[], surveyName: string, userName: string): Promise<Response> {
// 设置创建时间与id
const newQuestion = {
id : uuidv4(),
id: uuidv4(),
createTime: new Date().toISOString(),
questions
questions,
};
// 根据用户名创建文件夹,文件夹里存放该用户创建的调查问卷
const userDir = path.resolve(__dirname, `data/${userName}`);
Expand All @@ -163,9 +163,9 @@ class Store {
console.log(`已将提问者 ${userName} 的问题列表${surveyName}_question.json保存至 ${filePath} 文件中`);

return {
code : 0,
code: 0,
message: '设置成功,问题列表已更新',
data : newQuestion
data: newQuestion,
};
}
}
Expand Down
38 changes: 19 additions & 19 deletions serve/src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class Survey {
async save(data: Omit<SurveyData, 'id' | 'createdAt'>): Promise<Response<SurveyData>> {
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,
};
}

Expand All @@ -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), // 根据创建时间倒序排序
};
}

Expand All @@ -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,
};
}
}
Expand All @@ -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,
};
}

Expand All @@ -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,
};
}

Expand All @@ -143,14 +143,14 @@ class Survey {
async setQuestions(questions: any[], surveyName: string, userName: string): Promise<Response> {
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,
};
}

Expand Down