Skip to content

Commit

Permalink
Use insert for creating Note
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Jun 3, 2020
1 parent 3bfba98 commit 66776ce
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/services/note/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,30 +405,29 @@ async function insertNote(user: User, data: Option, tags: string[], emojis: stri

// 投稿を作成
try {
let note: Note;
if (insert.hasPoll) {
// Start transaction
await getConnection().transaction(async transactionalEntityManager => {
note = await transactionalEntityManager.save(insert);
await transactionalEntityManager.insert(Note, insert);

const poll = new Poll({
noteId: note.id,
noteId: insert.id,
choices: data.poll!.choices,
expiresAt: data.poll!.expiresAt,
multiple: data.poll!.multiple,
votes: new Array(data.poll!.choices.length).fill(0),
noteVisibility: note.visibility,
noteVisibility: insert.visibility,
userId: user.id,
userHost: user.host
});

await transactionalEntityManager.save(poll);
await transactionalEntityManager.insert(Poll, poll);
});
} else {
note = await Notes.save(insert);
await Notes.insert(insert);
}

return note!;
return await Notes.findOneOrFail(insert.id);
} catch (e) {
// duplicate key error
if (isDuplicateKeyValueError(e)) {
Expand All @@ -439,7 +438,7 @@ async function insertNote(user: User, data: Option, tags: string[], emojis: stri

console.error(e);

throw new Error('something happened');
throw e;
}
}

Expand Down

0 comments on commit 66776ce

Please sign in to comment.