Commit 1c949e19 authored by nanahira's avatar nanahira

transaction

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