From e798e3f0593d105a6d2b9fa9bf32d9debba79e08 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Wed, 18 Dec 2019 19:49:12 +0100 Subject: [PATCH] Api additions/fixed - fixed #47 userVote is always null - changed field userVote to require a userId --- src/graphql/schema.graphql | 2 +- src/lib/models/Post.ts | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/graphql/schema.graphql b/src/graphql/schema.graphql index 9246ad2..1782d5d 100644 --- a/src/graphql/schema.graphql +++ b/src/graphql/schema.graphql @@ -274,7 +274,7 @@ type Post { createdAt: String! "the type of vote the user performed on the post" - userVote: VoteType + userVote(userId: ID!): VoteType } "represents a request of any type" diff --git a/src/lib/models/Post.ts b/src/lib/models/Post.ts index 1421d14..b7412a5 100644 --- a/src/lib/models/Post.ts +++ b/src/lib/models/Post.ts @@ -1,5 +1,5 @@ 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 {PostVote, VoteType} from "./PostVote"; import {User} from "./User"; @@ -44,6 +44,11 @@ export class Post extends Model { 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 { type = type ?? VoteType.UPVOTE; let votes = await this.$get("rVotes", {where: {id: userId}}) as Array; @@ -67,4 +72,13 @@ export class Post extends Model { 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 { + const votes = await this.$get("rVotes", {where: {id: userId}}) as Array; + return votes[0]?.PostVote?.voteType; + } }