Commit 3f851a16 authored by nanahira's avatar nanahira

get /api/votes

parent 30a316c9
Pipeline #2453 passed with stages
in 1 minute and 6 seconds
......@@ -102,6 +102,12 @@ export class AppController {
res.status(code).json({ code });
}
@Get('votes')
async getVotes(@Query() query: any) {
const result = await this.appService.getVotes(query);
return result;
}
@Post('upload')
uploadFile(@Req() req: express.Request, @Res() res: express.Response) {
const form = new IncomingForm();
......@@ -115,7 +121,9 @@ export class AppController {
return res.status(500).send('upload image fail!');
}
const response: any = {};
const response = {
code: 200,
};
if (err) {
response.code = 500;
} else {
......@@ -125,7 +133,7 @@ export class AppController {
response.path = files.file.path;
}
res.json(response);
res.status(response.code).json(response);
});
}
@Get('download/:id')
......
......@@ -75,6 +75,13 @@ interface PlayerDiff {
entertain_all: number;
}
interface VoteOption {
key: number;
name: string;
count: number;
percentage: number;
}
@Injectable()
export class AppService {
chineseDirtyFilter: Filter;
......@@ -924,4 +931,87 @@ export class AppService {
return 500;
}
}
private async fetchVoteOptionCount(
voteId: number,
optionId: number,
optionCountMap,
) {
const count = await this.mcdb.getRepository(VoteResult).count({
voteId: voteId.toString(),
optionId: optionId.toString(),
});
optionCountMap[optionId] = count;
}
private async fetchVoteInfo(
vote: Votes,
optionCountMap: any,
voteCountMap: any,
) {
const repo = this.mcdb.getRepository(VoteResult);
const voteId = vote.id;
const options: VoteOption[] = JSON.parse(vote.options);
await Promise.all(
options.map((option) =>
this.fetchVoteOptionCount(voteId, option.key, optionCountMap),
),
);
const optionIdStrings = options.map((option) => option.key.toString());
const voteCountResult = await repo
.createQueryBuilder()
.select('count(DISTINCT userid)', 'voteCount')
.where('vote_id = :voteId', { voteId: voteId.toString() })
.andWhere('option_id in (:...optionIdStrings)', { optionIdStrings })
.getRawOne();
const voteCount: number = parseInt(voteCountResult.voteCount);
voteCountMap[voteId] = voteCount;
}
async getVotes(query: any) {
const username = query.username;
const type = query.type;
let status: boolean = undefined;
if (type === '1') {
status = true;
}
if (type === '2') {
status = false;
}
const from_date = query.from_date;
const to_date = query.to_date;
// page_no 当前页数 page_num 每页展示数
// offset = (page_no - 1) * page_num
// select * from battle_history limit 5 offset 15;
const page_no: number = query.page || 1;
const page_num: number = query.page_num || 15;
const offset = (page_no - 1) * page_num;
const repo = this.mcdb.getRepository(Votes);
const countQuery = repo.createQueryBuilder();
if (status !== undefined) {
countQuery.where('status = :status', { status });
}
const total = await countQuery.getCount();
const voteQuery = repo.createQueryBuilder();
if (status !== undefined) {
voteQuery.where('status = :status', { status });
}
voteQuery.orderBy('create_time', 'DESC').limit(page_num).offset(offset);
const votes = await voteQuery.getMany();
const optionCountMap: any = {};
const voteCountMap: any = {};
await Promise.all(
votes.map((vote) =>
this.fetchVoteInfo(vote, optionCountMap, voteCountMap),
),
);
return {
total,
data: votes,
voteCountMap,
optionCountMap,
};
}
}
......@@ -24,7 +24,7 @@ export class Votes {
@Column('timestamp without time zone', { name: 'end_time', nullable: true })
endTime: Date;
@Column('boolean', { name: 'status', nullable: true, default: 'false' })
@Column('boolean', { name: 'status', nullable: true, default: false })
status: boolean;
@Column('boolean', {
......
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