|
|
@ -74,10 +74,22 @@ export class Post {
|
|
|
|
* @param type
|
|
|
|
* @param type
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public async vote(userId: number, type: dataaccess.VoteType): Promise<dataaccess.VoteType> {
|
|
|
|
public async vote(userId: number, type: dataaccess.VoteType): Promise<dataaccess.VoteType> {
|
|
|
|
const [vote, _] = await SqPostVotes
|
|
|
|
type = type || dataaccess.VoteType.UPVOTE;
|
|
|
|
.findOrCreate({where: {userId}, defaults: {voteType: type, postId: this.post.id}});
|
|
|
|
let vote = await SqPostVotes.findOne({where: {user_id: userId, post_id: this.id}});
|
|
|
|
|
|
|
|
if (!vote) {
|
|
|
|
|
|
|
|
await this.post.addVote(userId);
|
|
|
|
|
|
|
|
vote = await SqPostVotes.findOne({where: {user_id: userId, post_id: this.id}});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vote) {
|
|
|
|
|
|
|
|
if (vote.voteType === type) {
|
|
|
|
|
|
|
|
await vote.destroy();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
vote.voteType = type;
|
|
|
|
vote.voteType = type;
|
|
|
|
await vote.save();
|
|
|
|
await vote.save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return vote.voteType;
|
|
|
|
return vote.voteType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|