Commit 9acd774c authored by nanahira's avatar nanahira

some new apis and rename entities

parent 5ca9b2c4
Pipeline #2465 passed with stages
in 39 seconds
...@@ -80,8 +80,42 @@ export class AppController { ...@@ -80,8 +80,42 @@ export class AppController {
} }
@Post('activity') @Post('activity')
async updateActivity(@Body() body: any) { async updateActivity(@Body() body: any, @Res() res: express.Response) {
return await this.appService.updateActivity(body); const code = await this.appService.updateActivity(body);
res.status(code).json({ code });
}
@Get('label')
async getLabel(@Res() res: express.Response) {
const value = await this.appService.getSiteConfig('label');
if (value) {
res.status(200).json({
code: 200,
text: value,
});
} else {
res.status(500).json({
code: 500,
});
}
}
@Post('label')
async updateLabel(
@Body('labelone') value: string,
@Res() res: express.Response,
) {
const code = await this.appService.updateSiteConfig('label', value);
res.status(code).json({ code });
}
@Post('adSwitchChange')
async updateAdvertisementSetting(
@Body('status') value: string,
@Res() res: express.Response,
) {
const code = await this.appService.updateSiteConfig('auto_close_ad', value);
res.status(code).json({ code });
} }
@Post('votes') @Post('votes')
...@@ -177,4 +211,14 @@ export class AppController { ...@@ -177,4 +211,14 @@ export class AppController {
} }
res.json({ deck }); res.json({ deck });
} }
@Post('deckdemo')
async submitDeckDemo(@Body() body: any, @Res() res: express.Response) {
const code = await this.appService.submitDeckDemo(body);
res.status(code).json({ code });
}
@Post('deckinfo')
async submitDeckInfo(@Body() body: any, @Res() res: express.Response) {
const code = await this.appService.submitDeckInfo(body);
res.status(code).json({ code });
}
} }
...@@ -30,6 +30,7 @@ import { scheduleJob } from 'node-schedule'; ...@@ -30,6 +30,7 @@ import { scheduleJob } from 'node-schedule';
import { DeckInfo } from './entities/mycard/DeckInfo'; import { DeckInfo } from './entities/mycard/DeckInfo';
import { DeckInfoHistory } from './entities/mycard/DeckInfoHistory'; import { DeckInfoHistory } from './entities/mycard/DeckInfoHistory';
import { DeckDemo } from './entities/mycard/DeckDemo'; import { DeckDemo } from './entities/mycard/DeckDemo';
import { Deck } from './entities/mycard/Deck';
const attrOffset = 1010; const attrOffset = 1010;
const raceOffset = 1020; const raceOffset = 1020;
...@@ -188,7 +189,7 @@ export class AppService { ...@@ -188,7 +189,7 @@ export class AppService {
.createQueryBuilder('userInfo') .createQueryBuilder('userInfo')
.orderBy(orderByWhat, 'DESC') .orderBy(orderByWhat, 'DESC')
.limit(100) .limit(100)
.getRawMany(); .getMany();
} }
async getCardInfo(id: number, lang: string) { async getCardInfo(id: number, lang: string) {
...@@ -463,35 +464,40 @@ export class AppService { ...@@ -463,35 +464,40 @@ export class AppService {
return null; return null;
} }
} }
private async getSiteConfig(configKey: string) { async getSiteConfig(configKey: string) {
const configObject = await this.mcdb try {
.getRepository(SiteConfig) const configObject = await this.mcdb
.findOneOrFail({ .getRepository(SiteConfig)
select: ['configValue'], .findOneOrFail({
where: [configKey], select: ['configValue'],
}); where: [configKey],
return configObject.configValue; });
} return configObject.configValue;
private async updateSiteConfig(configKey: string, configValue: string) { } catch (e) {
await this.mcdb this.log.warn(`Failed to get config ${configKey}: ${e.toString()}`);
.getRepository(SiteConfig) return null;
.update({ configKey }, { configValue }); }
} }
async updateSiteConfig(configKey: string, configValue: string) {
async updateActivity(body: any) {
const { start, end, max, name } = body;
const activityStr = JSON.stringify({ start, end, max, name });
try { try {
await this.updateSiteConfig('activity', activityStr); await this.mcdb
return { code: 200 }; .getRepository(SiteConfig)
.update({ configKey }, { configValue });
return 200;
} catch (e) { } catch (e) {
this.log.warn( this.log.warn(
`Failed to update activity to ${activityStr}: ${e.toString()}`, `Failed to update config ${configKey} to ${configValue}: ${e.toString()}`,
); );
return { code: 500 }; return 500;
} }
} }
async updateActivity(body: any) {
const { start, end, max, name } = body;
const activityStr = JSON.stringify({ start, end, max, name });
return this.updateSiteConfig('activity', activityStr);
}
private async findOrCreateUser(username: string) { private async findOrCreateUser(username: string) {
const repo = this.mcdb.getRepository(UserInfo); const repo = this.mcdb.getRepository(UserInfo);
let user = await repo.findOne({ let user = await repo.findOne({
...@@ -779,10 +785,10 @@ export class AppService { ...@@ -779,10 +785,10 @@ export class AppService {
.set({ .set({
exp: expResult.expA, exp: expResult.expA,
pt: ptResult.ptA, pt: ptResult.ptA,
athleticWin: () => `athletic_win + ${paramA.athletic_win}`, athletic_win: () => `athletic_win + ${paramA.athletic_win}`,
athleticLose: () => `athletic_lose + ${paramA.athletic_win}`, athletic_lose: () => `athletic_lose + ${paramA.athletic_lose}`,
athleticDraw: () => `athletic_draw + ${paramA.athletic_win}`, athletic_draw: () => `athletic_draw + ${paramA.athletic_draw}`,
athleticAll: () => `athletic_all + ${paramA.athletic_win}`, athletic_all: () => `athletic_all + ${paramA.athletic_all}`,
}) })
.where('username = :username', { username: userA.username }) .where('username = :username', { username: userA.username })
.execute(), .execute(),
...@@ -792,10 +798,10 @@ export class AppService { ...@@ -792,10 +798,10 @@ export class AppService {
.set({ .set({
exp: expResult.expB, exp: expResult.expB,
pt: ptResult.ptB, pt: ptResult.ptB,
athleticWin: () => `athletic_win + ${paramB.athletic_win}`, athletic_win: () => `athletic_win + ${paramB.athletic_win}`,
athleticLose: () => `athletic_lose + ${paramB.athletic_win}`, athletic_lose: () => `athletic_lose + ${paramB.athletic_lose}`,
athleticDraw: () => `athletic_draw + ${paramB.athletic_win}`, athletic_draw: () => `athletic_draw + ${paramB.athletic_draw}`,
athleticAll: () => `athletic_all + ${paramB.athletic_win}`, athletic_all: () => `athletic_all + ${paramB.athletic_all}`,
}) })
.where('username = :username', { username: userB.username }) .where('username = :username', { username: userB.username })
.execute(), .execute(),
...@@ -857,10 +863,12 @@ export class AppService { ...@@ -857,10 +863,12 @@ export class AppService {
.update(UserInfo) .update(UserInfo)
.set({ .set({
exp: expResult.expA, exp: expResult.expA,
entertainWin: () => `entertain_win + ${paramA.entertain_win}`, entertain_win: () => `entertain_win + ${paramA.entertain_win}`,
entertainLose: () => `entertain_lose + ${paramA.entertain_win}`, entertain_lose: () =>
entertainDraw: () => `entertain_draw + ${paramA.entertain_win}`, `entertain_lose + ${paramA.entertain_lose}`,
entertainAll: () => `entertain_all + ${paramA.entertain_win}`, entertain_draw: () =>
`entertain_draw + ${paramA.entertain_draw}`,
entertain_all: () => `entertain_all + ${paramA.entertain_all}`,
}) })
.where('username = :username', { username: userA.username }) .where('username = :username', { username: userA.username })
.execute(), .execute(),
...@@ -869,10 +877,12 @@ export class AppService { ...@@ -869,10 +877,12 @@ export class AppService {
.update(UserInfo) .update(UserInfo)
.set({ .set({
exp: expResult.expB, exp: expResult.expB,
entertainWin: () => `entertain_win + ${paramB.entertain_win}`, entertain_win: () => `entertain_win + ${paramB.entertain_win}`,
entertainLose: () => `entertain_lose + ${paramB.entertain_win}`, entertain_lose: () =>
entertainDraw: () => `entertain_draw + ${paramB.entertain_win}`, `entertain_lose + ${paramB.entertain_lose}`,
entertainAll: () => `entertain_all + ${paramB.entertain_win}`, entertain_draw: () =>
`entertain_draw + ${paramB.entertain_draw}`,
entertain_all: () => `entertain_all + ${paramB.entertain_all}`,
}) })
.where('username = :username', { username: userB.username }) .where('username = :username', { username: userB.username })
.execute(), .execute(),
...@@ -1060,16 +1070,13 @@ export class AppService { ...@@ -1060,16 +1070,13 @@ export class AppService {
const page_num: number = query.page_num || 15; const page_num: number = query.page_num || 15;
const offset = (page_no - 1) * page_num; const offset = (page_no - 1) * page_num;
const repo = this.mcdb.getRepository(Votes); 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(); const voteQuery = repo.createQueryBuilder();
if (status !== undefined) { if (status !== undefined) {
voteQuery.where('status = :status', { status }); voteQuery.where('status = :status', { status });
} }
voteQuery.orderBy('create_time', 'DESC').limit(page_num).offset(offset); const total = await voteQuery.getCount();
voteQuery.orderBy('create_time', 'DESC');
voteQuery.limit(page_num).offset(offset);
const votes = await voteQuery.getMany(); const votes = await voteQuery.getMany();
const optionCountMap: any = {}; const optionCountMap: any = {};
const voteCountMap: any = {}; const voteCountMap: any = {};
...@@ -1090,8 +1097,8 @@ export class AppService { ...@@ -1090,8 +1097,8 @@ export class AppService {
const now = moment().toDate(); const now = moment().toDate();
const allVotes = await this.mcdb.getRepository(Votes).find({ const allVotes = await this.mcdb.getRepository(Votes).find({
status: true, status: true,
startTime: LessThanOrEqual(now), start_time: LessThanOrEqual(now),
endTime: MoreThanOrEqual(now), end_time: MoreThanOrEqual(now),
}); });
const votedIds = ( const votedIds = (
await this.mcdb.getRepository(VoteResult).find({ await this.mcdb.getRepository(VoteResult).find({
...@@ -1123,14 +1130,14 @@ export class AppService { ...@@ -1123,14 +1130,14 @@ export class AppService {
.where('name = :name', { name }) .where('name = :name', { name })
.andWhere('id = :id', { id: parseInt(version) }) .andWhere('id = :id', { id: parseInt(version) })
.orderBy('start_time', 'DESC') .orderBy('start_time', 'DESC')
.getRawOne(); .getOne();
} else { } else {
deck = await this.mcdb deck = await this.mcdb
.getRepository(DeckInfo) .getRepository(DeckInfo)
.createQueryBuilder() .createQueryBuilder()
.where('name like :name', { name: `%${name}%` }) .where('name like :name', { name: `%${name}%` })
.orderBy('start_time', 'DESC') .orderBy('start_time', 'DESC')
.getRawOne(); .getOne();
} }
if (!deck) { if (!deck) {
return { return {
...@@ -1143,15 +1150,17 @@ export class AppService { ...@@ -1143,15 +1150,17 @@ export class AppService {
.createQueryBuilder() .createQueryBuilder()
.where('name = :name', { name: resName }) .where('name = :name', { name: resName })
.orderBy('start_time', 'DESC') .orderBy('start_time', 'DESC')
.getRawMany(); .getMany();
const demo = ( const demo = (
await this.mcdb await this.mcdb
.getRepository(DeckDemo) .getRepository(DeckDemo)
.createQueryBuilder() .createQueryBuilder()
.where('name = :name', { name: resName }) .where('name = :name', { name: resName })
.orderBy('create_time', 'DESC') .orderBy('create_time', 'DESC')
.getRawMany() .getMany()
).map((row) => { ).map((row) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
row.create_time = moment(row.create_time).format('YYYY-MM-DD'); row.create_time = moment(row.create_time).format('YYYY-MM-DD');
return row; return row;
}); });
...@@ -1287,4 +1296,80 @@ export class AppService { ...@@ -1287,4 +1296,80 @@ export class AppService {
side: sideCardArr, side: sideCardArr,
}; };
} }
async submitDeckDemo(body: any) {
const author: string = body.user;
const title: string = body.title;
const name: string = body.name;
const img_url: string = body.url;
const file: string = body.file || '';
const now = moment().toDate();
const deckDemo = new DeckDemo();
deckDemo.author = author;
deckDemo.title = title;
deckDemo.url = img_url;
deckDemo.name = name;
deckDemo.file = file;
deckDemo.create_time = now;
try {
await this.mcdb.getRepository(DeckDemo).save(deckDemo);
return 200;
} catch (e) {
this.log.error(`Failed submitting deck demo: ${e.toString()}`);
return 500;
}
}
async submitDeckInfo(body: any) {
const author = body.user;
const title = body.title;
const name = body.name;
const desc = body.desc;
const strategy = body.strategy;
const reference = body.reference;
const img_url = body.url;
const isNew = body.isNew;
const now = moment().toDate();
const content = {
author: this.chineseDirtyFilter.clean(author),
title: this.chineseDirtyFilter.clean(title),
desc: this.chineseDirtyFilter.clean(desc),
strategy: this.chineseDirtyFilter.clean(strategy),
reference: this.chineseDirtyFilter.clean(reference),
url: img_url,
};
const contentStr = JSON.stringify(content);
let code = 200;
await this.transaction(this.mcdb, async (db) => {
try {
if (isNew === 'true') {
const deckInfo = new DeckInfo();
deckInfo.name = name;
deckInfo.content = contentStr;
deckInfo.start_time = now;
await db.getRepository(DeckInfo).save(deckInfo);
} else {
await db
.getRepository(DeckInfo)
.update({ name }, { content: contentStr, end_time: now });
}
const deckInfoHistory = new DeckInfoHistory();
deckInfoHistory.name = name;
deckInfoHistory.content = contentStr;
deckInfoHistory.end_time = now;
await db.getRepository(DeckInfoHistory).save(deckInfoHistory);
} catch (e) {
this.log.error(`Failed to submit deck info ${name}: ${e.toString()}`);
code = 500;
return false;
}
return true;
});
return code;
}
} }
...@@ -24,5 +24,5 @@ export class DeckDemo { ...@@ -24,5 +24,5 @@ export class DeckDemo {
name: 'create_time', name: 'create_time',
nullable: true, nullable: true,
}) })
createTime: Date; create_time: Date;
} }
...@@ -12,8 +12,8 @@ export class DeckInfo { ...@@ -12,8 +12,8 @@ export class DeckInfo {
content: string; content: string;
@Column('timestamp without time zone', { name: 'start_time', nullable: true }) @Column('timestamp without time zone', { name: 'start_time', nullable: true })
startTime: Date; start_time: Date;
@Column('timestamp without time zone', { name: 'end_time', nullable: true }) @Column('timestamp without time zone', { name: 'end_time', nullable: true })
endTime: Date; end_time: Date;
} }
...@@ -12,8 +12,8 @@ export class DeckInfoHistory { ...@@ -12,8 +12,8 @@ export class DeckInfoHistory {
content: string; content: string;
@Column('timestamp without time zone', { name: 'start_time', nullable: true }) @Column('timestamp without time zone', { name: 'start_time', nullable: true })
startTime: Date; start_time: Date;
@Column('timestamp without time zone', { name: 'end_time', nullable: true }) @Column('timestamp without time zone', { name: 'end_time', nullable: true })
endTime: Date; end_time: Date;
} }
...@@ -20,28 +20,28 @@ export class UserInfo { ...@@ -20,28 +20,28 @@ export class UserInfo {
pt: number; pt: number;
@Column('integer', { name: 'entertain_win', default: 0 }) @Column('integer', { name: 'entertain_win', default: 0 })
entertainWin: number; entertain_win: number;
@Column('integer', { name: 'entertain_lose', default: 0 }) @Column('integer', { name: 'entertain_lose', default: 0 })
entertainLose: number; entertain_lose: number;
@Column('integer', { name: 'entertain_draw', default: 0 }) @Column('integer', { name: 'entertain_draw', default: 0 })
entertainDraw: number; entertain_draw: number;
@Column('integer', { name: 'entertain_all', default: 0 }) @Column('integer', { name: 'entertain_all', default: 0 })
entertainAll: number; entertain_all: number;
@Column('integer', { name: 'athletic_win', default: 0 }) @Column('integer', { name: 'athletic_win', default: 0 })
athleticWin: number; athletic_win: number;
@Column('integer', { name: 'athletic_lose', default: 0 }) @Column('integer', { name: 'athletic_lose', default: 0 })
athleticLose: number; athletic_lose: number;
@Column('integer', { name: 'athletic_draw', default: 0 }) @Column('integer', { name: 'athletic_draw', default: 0 })
athleticDraw: number; athletic_draw: number;
@Column('integer', { name: 'athletic_all', default: 0 }) @Column('integer', { name: 'athletic_all', default: 0 })
athleticAll: number; athletic_all: number;
@Column('integer', { name: 'id', nullable: true }) @Column('integer', { name: 'id', nullable: true })
id: number; id: number;
......
...@@ -16,13 +16,13 @@ export class Votes { ...@@ -16,13 +16,13 @@ export class Votes {
name: 'create_time', name: 'create_time',
nullable: true, nullable: true,
}) })
createTime: Date; create_time: Date;
@Column('timestamp without time zone', { name: 'start_time', nullable: true }) @Column('timestamp without time zone', { name: 'start_time', nullable: true })
startTime: Date; start_time: Date;
@Column('timestamp without time zone', { name: 'end_time', nullable: true }) @Column('timestamp without time zone', { name: 'end_time', nullable: true })
endTime: Date; end_time: Date;
@Column('boolean', { name: 'status', nullable: true, default: false }) @Column('boolean', { name: 'status', nullable: true, default: false })
status: boolean; status: boolean;
...@@ -30,7 +30,7 @@ export class Votes { ...@@ -30,7 +30,7 @@ export class Votes {
@Column('boolean', { @Column('boolean', {
name: 'multiple', name: 'multiple',
nullable: true, nullable: true,
default: 'false', default: false,
}) })
multiple: boolean; multiple: 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