Commit f6e10b09 authored by nanahira's avatar nanahira

get /api/vote

parent 3f851a16
Pipeline #2454 passed with stages
in 39 seconds
......@@ -108,6 +108,17 @@ export class AppController {
return result;
}
@Get('vote')
async getRandomVote(@Query('user') userid: string) {
if (!userid) {
return {
data: 'null',
};
}
const result = await this.appService.getRandomVote(userid);
return result;
}
@Post('upload')
uploadFile(@Req() req: express.Request, @Res() res: express.Response) {
const form = new IncomingForm();
......
......@@ -1014,4 +1014,29 @@ export class AppService {
optionCountMap,
};
}
async getRandomVote(userid: string) {
const now = moment().toDate();
const allVotes = await this.mcdb
.getRepository(Votes)
.createQueryBuilder('vote')
.where("vote.status = 't'")
.andWhere('vote.start_time <= :now', { now })
.andWhere('vote.end_time >= :now', { now })
.getMany();
const votedIds = (
await this.mcdb.getRepository(VoteResult).find({
userid,
})
).map((voteResult) => parseInt(voteResult.voteId));
const validVotes = allVotes.filter((vote) => !votedIds.includes(vote.id));
if (validVotes.length) {
const index = Math.floor(Math.random() * validVotes.length);
return {
data: validVotes[index],
};
} else {
return { data: 'null' };
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment