Api additions/fixed

- fixed #47 userVote is always null
- changed field userVote to require a userId
pull/4/head
Trivernis 5 years ago
parent 9d7e475efb
commit e798e3f059

@ -274,7 +274,7 @@ type Post {
createdAt: String! createdAt: String!
"the type of vote the user performed on the post" "the type of vote the user performed on the post"
userVote: VoteType userVote(userId: ID!): VoteType
} }
"represents a request of any type" "represents a request of any type"

@ -1,5 +1,5 @@
import * as sqz from "sequelize"; import * as sqz from "sequelize";
import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table,} from "sequelize-typescript"; import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table} from "sequelize-typescript";
import markdown from "../markdown"; import markdown from "../markdown";
import {PostVote, VoteType} from "./PostVote"; import {PostVote, VoteType} from "./PostVote";
import {User} from "./User"; import {User} from "./User";
@ -44,6 +44,11 @@ export class Post extends Model<Post> {
return (await this.votes()).filter((v) => v.PostVote.voteType === VoteType.DOWNVOTE).length; return (await this.votes()).filter((v) => v.PostVote.voteType === VoteType.DOWNVOTE).length;
} }
/**
* Toggles the vote of the user.
* @param userId
* @param type
*/
public async vote(userId: number, type: VoteType): Promise<VoteType> { public async vote(userId: number, type: VoteType): Promise<VoteType> {
type = type ?? VoteType.UPVOTE; type = type ?? VoteType.UPVOTE;
let votes = await this.$get("rVotes", {where: {id: userId}}) as Array<User & {PostVote: PostVote}>; let votes = await this.$get("rVotes", {where: {id: userId}}) as Array<User & {PostVote: PostVote}>;
@ -67,4 +72,13 @@ export class Post extends Model<Post> {
return vote.PostVote.voteType; return vote.PostVote.voteType;
} }
/**
* Returns the type of vote that was performend on the post by the user specified by the user id.
* @param userId
*/
public async userVote({userId}: {userId: number}): Promise<VoteType> {
const votes = await this.$get("rVotes", {where: {id: userId}}) as Array<User & {PostVote: PostVote}>;
return votes[0]?.PostVote?.voteType;
}
} }

Loading…
Cancel
Save