Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 20x 20x 20x 4x 4x 4x 8x 20x 5x 5x 10x 20x 4x 4x 4x 4x 20x 1x 1x 1x 1x 20x 1x 1x 1x 20x 5x 5x 5x 5x 20x 5x 5x 5x 5x 20x 1x 1x 1x 1x 20x 2x 2x 2x 2x 20x 15x 15x 30x 20x 9x 9x 18x 20x | /** * Services for the post vote table. * @packageDocumentation */ import { BaseService, getTime } from "./util"; /** * Post vote architecture. */ export interface PostVote { id: number; userID: string; postID: string; voteType: string; createTime: number; } /** * Post vote with only vote type architecture. */ interface PostVoteType { voteType: string; } /** * Post vote count architecture. */ interface PostVoteCount { count: number; } /** * Post vote services */ export class PostVoteService extends BaseService { /** * Vote on a post. * * @param userID A user's ID. * @param postID A post's ID. * @param voteType The type of vote. */ public async vote( userID: string, postID: string, voteType: string ): Promise<void> { this.unvote(userID, postID); const sql = ` INSERT INTO PostVote ( userID, postID, voteType, createTime ) VALUES ( ?, ?, ?, ? ); `; const params = [userID, postID, voteType, getTime()]; await this.dbm.execute(sql, params); } /** * Unvote a post. * * @param userID A user's ID. * @param postID A post's ID. */ public async unvote(userID: string, postID: string): Promise<void> { const sql = `DELETE FROM PostVote WHERE userID = ? AND postID = ?;`; const params = [userID, postID]; await this.dbm.execute(sql, params); } /** * Check if a post has been voted on by a user. * * @param userID A user's ID. * @param postID A post's ID. * @returns Whether or not the post has been voted on by a user. */ public async voted(userID: string, postID: string): Promise<boolean> { const sql = `SELECT id FROM PostVote WHERE userID = ? AND postID = ?;`; const params = [userID, postID]; const rows: PostVote[] = await this.dbm.execute(sql, params); return rows.length > 0; } /** * Get a post vote record. * * @param userID A user's ID. * @param postID A post's ID. * @returns The post vote record. */ public async getVote(userID: string, postID: string): Promise<PostVote> { const sql = `SELECT * FROM PostVote WHERE userID = ? AND postID = ?;`; const params = [userID, postID]; const rows: PostVote[] = await this.dbm.execute(sql, params); return rows[0]; } /** * Get all post vote records. * * @returns All post vote records. */ public async getVotes(): Promise<PostVote[]> { const sql = `SELECT * FROM PostVote;`; const rows: PostVote[] = await this.dbm.execute(sql); return rows; } /** * Get all of a user's post votes. * * @param userID A user's ID. * @returns All of a user's votes. */ public async getUserVotes(userID: string): Promise<PostVote[]> { const sql = `SELECT * FROM PostVote WHERE userID = ?;`; const params = [userID]; const rows: PostVote[] = await this.dbm.execute(sql, params); return rows; } /** * Get all of the votes on a post. * * @param postID A post's ID. * @returns All of the votes on a post. */ public async getPostVotes(postID: string): Promise<PostVote[]> { const sql = `SELECT * FROM PostVote WHERE postID = ?;`; const params = [postID]; const rows: PostVote[] = await this.dbm.execute(sql, params); return rows; } /** * Get the type of vote a user has made on a post. * * @param userID A user's ID. * @param postID A post's ID. * @returns The type of vote the user made on the post. */ public async getPostVoteType( userID: string, postID: string ): Promise<string> { const sql = `SELECT voteType FROM PostVote WHERE userID = ? AND postID = ?;`; const params = [userID, postID]; const rows: PostVoteType[] = await this.dbm.execute(sql, params); return rows[0].voteType; } /** * Get the number of votes a post has of a given vote type. * * @param postID A post's ID. * @param voteType The type of vote. * @returns The number of votes a post has of the given vote type. */ public async getNumPostVotes( postID: string, voteType: string ): Promise<number> { const sql = `SELECT COUNT(*) AS count FROM PostVote WHERE postID = ? AND voteType = ?;`; const params = [postID, voteType]; const rows: PostVoteCount[] = await this.dbm.execute(sql, params); return rows[0].count; } /** * Delete all of a user's votes. * * @param userID A user's ID. */ public async deleteUserVotes(userID: string): Promise<void> { const sql = `DELETE FROM PostVote WHERE userID = ?;`; const params = [userID]; await this.dbm.execute(sql, params); } /** * Delete all of a post's votes. * * @param postID A post's ID. */ public async deletePostVotes(postID: string): Promise<void> { const sql = `DELETE FROM PostVote WHERE postID = ?;`; const params = [postID]; await this.dbm.execute(sql, params); } } |