Commit 1c949e19 authored by nanahira's avatar nanahira

transaction

parent f6e10b09
Pipeline #2455 passed with stages
in 42 seconds
...@@ -718,35 +718,44 @@ export class AppService { ...@@ -718,35 +718,44 @@ export class AppService {
battleHistory.isfirstwin = firstWin; battleHistory.isfirstwin = firstWin;
battleHistory.decka = deckA; battleHistory.decka = deckA;
battleHistory.deckb = deckB; battleHistory.deckb = deckB;
await Promise.all([ await this.transaction(this.mcdb, async (db) => {
repo.save(battleHistory), const repo = db.getRepository(BattleHistory);
this.mcdb try {
.createQueryBuilder() await Promise.all([
.update(UserInfo) repo.save(battleHistory),
.set({ db
exp: expResult.expA, .createQueryBuilder()
pt: ptResult.ptA, .update(UserInfo)
athleticWin: () => `athletic_win + ${paramA.athletic_win}`, .set({
athleticLose: () => `athletic_lose + ${paramA.athletic_win}`, exp: expResult.expA,
athleticDraw: () => `athletic_draw + ${paramA.athletic_win}`, pt: ptResult.ptA,
athleticAll: () => `athletic_all + ${paramA.athletic_win}`, athleticWin: () => `athletic_win + ${paramA.athletic_win}`,
}) athleticLose: () => `athletic_lose + ${paramA.athletic_win}`,
.where('username = :username', { username: userA.username }) athleticDraw: () => `athletic_draw + ${paramA.athletic_win}`,
.execute(), athleticAll: () => `athletic_all + ${paramA.athletic_win}`,
this.mcdb })
.createQueryBuilder() .where('username = :username', { username: userA.username })
.update(UserInfo) .execute(),
.set({ db
exp: expResult.expB, .createQueryBuilder()
pt: ptResult.ptB, .update(UserInfo)
athleticWin: () => `athletic_win + ${paramB.athletic_win}`, .set({
athleticLose: () => `athletic_lose + ${paramB.athletic_win}`, exp: expResult.expB,
athleticDraw: () => `athletic_draw + ${paramB.athletic_win}`, pt: ptResult.ptB,
athleticAll: () => `athletic_all + ${paramB.athletic_win}`, athleticWin: () => `athletic_win + ${paramB.athletic_win}`,
}) athleticLose: () => `athletic_lose + ${paramB.athletic_win}`,
.where('username = :username', { username: userB.username }) athleticDraw: () => `athletic_draw + ${paramB.athletic_win}`,
.execute(), athleticAll: () => `athletic_all + ${paramB.athletic_win}`,
]); })
.where('username = :username', { username: userB.username })
.execute(),
]);
return true;
} catch (e) {
this.log.error(`Failed to report score: ${e.toString()}`);
return false;
}
});
} else { } else {
const expResult = EloUtility.getExpScore( const expResult = EloUtility.getExpScore(
userA.exp, userA.exp,
...@@ -788,33 +797,42 @@ export class AppService { ...@@ -788,33 +797,42 @@ export class AppService {
battleHistory.isfirstwin = firstWin; battleHistory.isfirstwin = firstWin;
battleHistory.decka = deckA; battleHistory.decka = deckA;
battleHistory.deckb = deckB; battleHistory.deckb = deckB;
await Promise.all([ await this.transaction(this.mcdb, async (db) => {
repo.save(battleHistory), const repo = db.getRepository(BattleHistory);
this.mcdb try {
.createQueryBuilder() await Promise.all([
.update(UserInfo) repo.save(battleHistory),
.set({ db
exp: expResult.expA, .createQueryBuilder()
athleticWin: () => `athletic_win + ${paramA.athletic_win}`, .update(UserInfo)
athleticLose: () => `athletic_lose + ${paramA.athletic_win}`, .set({
athleticDraw: () => `athletic_draw + ${paramA.athletic_win}`, exp: expResult.expA,
athleticAll: () => `athletic_all + ${paramA.athletic_win}`, entertainWin: () => `entertain_win + ${paramA.entertain_win}`,
}) entertainLose: () => `entertain_lose + ${paramA.entertain_win}`,
.where('username = :username', { username: userA.username }) entertainDraw: () => `entertain_draw + ${paramA.entertain_win}`,
.execute(), entertainAll: () => `entertain_all + ${paramA.entertain_win}`,
this.mcdb })
.createQueryBuilder() .where('username = :username', { username: userA.username })
.update(UserInfo) .execute(),
.set({ db
exp: expResult.expB, .createQueryBuilder()
athleticWin: () => `athletic_win + ${paramB.athletic_win}`, .update(UserInfo)
athleticLose: () => `athletic_lose + ${paramB.athletic_win}`, .set({
athleticDraw: () => `athletic_draw + ${paramB.athletic_win}`, exp: expResult.expB,
athleticAll: () => `athletic_all + ${paramB.athletic_win}`, entertainWin: () => `entertain_win + ${paramB.entertain_win}`,
}) entertainLose: () => `entertain_lose + ${paramB.entertain_win}`,
.where('username = :username', { username: userB.username }) entertainDraw: () => `entertain_draw + ${paramB.entertain_win}`,
.execute(), entertainAll: () => `entertain_all + ${paramB.entertain_win}`,
]); })
.where('username = :username', { username: userB.username })
.execute(),
]);
return true;
} catch (e) {
this.log.error(`Failed to report score: ${e.toString()}`);
return false;
}
});
} }
return null; return null;
...@@ -893,43 +911,47 @@ export class AppService { ...@@ -893,43 +911,47 @@ export class AppService {
const date_time = moment().format('YYYY-MM-DD'); const date_time = moment().format('YYYY-MM-DD');
const create_time = moment().toDate(); const create_time = moment().toDate();
const repo = this.mcdb.getRepository(VoteResult); let statusCode = 200;
try { await this.transaction(this.mcdb, async (db) => {
if (multiple) { const repo = db.getRepository(VoteResult);
if (!opids) { try {
return 400; if (multiple) {
} if (!opids) {
const voteResults = opids.map((opid) => { statusCode = 400;
return false;
}
const voteResults = opids.map((opid) => {
const voteResult = new VoteResult();
voteResult.voteId = voteid;
voteResult.optionId = opid;
voteResult.userid = user;
voteResult.dateTime = date_time;
voteResult.createTime = create_time;
return voteResult;
});
await repo.save(voteResults);
} else {
const voteResult = new VoteResult(); const voteResult = new VoteResult();
voteResult.voteId = voteid; voteResult.voteId = voteid;
voteResult.optionId = opid; voteResult.optionId = opid;
voteResult.userid = user; voteResult.userid = user;
voteResult.dateTime = date_time; voteResult.dateTime = date_time;
voteResult.createTime = create_time; voteResult.createTime = create_time;
return voteResult; await repo.save(voteResult);
}); }
await Promise.all( if (username) {
voteResults.map((voteResult) => repo.save(voteResult)), await db
); .getRepository(UserInfo)
} else { .update({ username }, { exp: () => 'exp + 1', id: parseInt(user) });
const voteResult = new VoteResult(); }
voteResult.voteId = voteid; return true;
voteResult.optionId = opid; } catch (e) {
voteResult.userid = user; this.log.error(`Failed updating vote result: ${e.toString()}`);
voteResult.dateTime = date_time; statusCode = 500;
voteResult.createTime = create_time; return false;
await repo.save(voteResult);
}
if (username) {
await this.mcdb
.getRepository(UserInfo)
.update({ username }, { exp: () => 'exp + 1', id: parseInt(user) });
} }
return 200; });
} catch (e) { return statusCode;
this.log.error(`Failed updating vote result: ${e.toString()}`);
return 500;
}
} }
private async fetchVoteOptionCount( private async fetchVoteOptionCount(
......
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