Commit 5db9ca15 authored by nanahira's avatar nanahira

add createdAtBefore and createdAtAfter

parent b320dae1
Pipeline #35526 failed with stages
in 2 minutes and 17 seconds
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
"class-validator": "^0.14.1", "class-validator": "^0.14.1",
"crypto-random-string": "3.3.1", "crypto-random-string": "3.3.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"moment": "^2.30.1",
"nesties": "^1.1.1", "nesties": "^1.1.1",
"nestjs-mycard": "^4.0.2", "nestjs-mycard": "^4.0.2",
"nicot": "^1.1.11", "nicot": "^1.1.11",
...@@ -8929,15 +8928,6 @@ ...@@ -8929,15 +8928,6 @@
"mkdirp": "bin/cmd.js" "mkdirp": "bin/cmd.js"
} }
}, },
"node_modules/moment": {
"version": "2.30.1",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.30.1.tgz",
"integrity": "sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==",
"license": "MIT",
"engines": {
"node": "*"
}
},
"node_modules/ms": { "node_modules/ms": {
"version": "2.1.3", "version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
......
...@@ -26,13 +26,20 @@ import { ...@@ -26,13 +26,20 @@ import {
Participant, Participant,
ParticipantScore, ParticipantScore,
} from '../../participant/entities/participant.entity'; } from '../../participant/entities/participant.entity';
import { IsArray, IsInt, IsOptional, IsPositive } from 'class-validator'; import {
IsArray,
IsDate,
IsInt,
IsOptional,
IsPositive,
} from 'class-validator';
import { ApiProperty, PartialType } from '@nestjs/swagger'; import { ApiProperty, PartialType } from '@nestjs/swagger';
import { Match } from '../../match/entities/match.entity'; import { Match } from '../../match/entities/match.entity';
import { TournamentRules } from '../../tournament-rules/rule-map'; import { TournamentRules } from '../../tournament-rules/rule-map';
import _ from 'lodash'; import _ from 'lodash';
import { RenameClass } from 'nicot/dist/src/utility/rename-class'; import { RenameClass } from 'nicot/dist/src/utility/rename-class';
import { QuerySinceDaysAgo } from '../../utility/query-since-days-ago'; import { QuerySinceDaysAgo } from '../../utility/query-since-days-ago';
import { QueryOperator } from '../../utility/query-operator';
export enum TournamentRule { export enum TournamentRule {
SingleElimination = 'SingleElimination', SingleElimination = 'SingleElimination',
...@@ -130,14 +137,23 @@ export class Tournament extends DescBase { ...@@ -130,14 +137,23 @@ export class Tournament extends DescBase {
@NotWritable() @NotWritable()
@NotInResult() @NotInResult()
@IsInt() @IsDate()
@IsPositive() @QueryOperator('createdAt', '<=')
@QuerySinceDaysAgo('createdAt') @ApiProperty({
description: '查询创建时间在什么时候之前的比赛',
required: false,
})
createdAtBefore: Date;
@NotWritable()
@NotInResult()
@IsDate()
@QueryOperator('createdAt', '>=')
@ApiProperty({ @ApiProperty({
description: '查询最多创建时间在多少天前的比赛', description: '查询创建时间在什么时候之后的比赛',
required: false, required: false,
}) })
createdAtSinceDaysAgo: number; createAtAfter: Date;
@NotColumn() @NotColumn()
@OneToMany(() => Participant, (participant) => participant.tournament) @OneToMany(() => Participant, (participant) => participant.tournament)
......
import { QueryCondition } from 'nicot';
export const QueryOperator = (dateField: string, operator: string) =>
QueryCondition((obj, qb, entityName, key) => {
if (!obj[key]) return;
const typeormKey = `operator_${entityName}_${dateField}_${key}`;
qb.andWhere(`${entityName}.${dateField} ${operator} :${typeormKey}`, {
[typeormKey]: obj[key],
});
});
import { QueryCondition } from 'nicot';
import moment from 'moment';
export const QuerySinceDaysAgo = (dateField: string) =>
QueryCondition((obj, qb, entityName, key) => {
if (!obj[key]) return;
const since = moment().subtract(obj[key], 'days').toDate();
const typeormKey = `since_${entityName}_${dateField}_${key}`;
qb.andWhere(`${entityName}.${dateField} > :${typeormKey}`, {
[typeormKey]: since,
});
});
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