All files adminFavorites.ts

100% Statements 56/56
100% Branches 4/4
100% Functions 9/9
100% Lines 46/46

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          20x                             20x             20x 2x 2x 2x   2x                 20x 2x 2x 2x   2x                 20x 3x   4x 2x   2x             2x 4x   2x   1x                 20x 10x 10x 20x                 20x 9x 9x 9x   9x               20x 2x 2x 2x   2x               20x 1x             1x 1x   1x                 20x 2x               2x 2x   2x   20x  
/**
 * Services for the admin favorites table.
 * @packageDocumentation
 */
 
import { BaseService, getTime, newUniqueID } from "./util";
import { Post } from "./post";
 
/**
 * Admin favorites architecture.
 */
export interface AdminFavorites {
  id: string;
  postID: string;
  createTime: number;
}
 
/**
 * Admin favorites services.
 */
export class AdminFavoritesService extends BaseService {
  /**
   * Get an admin favorite record.
   *
   * @param favoriteID A favorite's ID.
   * @returns The admin favorite record.
   */
  public async getFavorite(favoriteID: string): Promise<AdminFavorites> {
    const sql = `SELECT * FROM AdminFavorites WHERE id = ?;`;
    const params = [favoriteID];
    const rows: AdminFavorites[] = await this.dbm.execute(sql, params);
 
    return rows[0];
  }
 
  /**
   * Get an admin favorite record by post ID.
   *
   * @param postID A post's ID.
   * @returns The admin favorite record.
   */
  public async getFavoriteByPostID(postID: string): Promise<AdminFavorites> {
    const sql = `SELECT * FROM AdminFavorites WHERE postID = ?;`;
    const params = [postID];
    const rows: AdminFavorites[] = await this.dbm.execute(sql, params);
 
    return rows[0];
  }
 
  /**
   * Favorite a post.
   *
   * @param postID A post's ID.
   * @returns The new favorite's ID.
   */
  public async favorite(postID: string): Promise<string> {
    const favorited = await this.isFavorite(postID);
 
    if (!favorited) {
      const favoriteID = await newUniqueID(this.dbm, "AdminFavorites");
 
      const sql = `
        INSERT INTO AdminFavorites (
          id, postID, createTime
        ) VALUES (
          ?, ?, ?
        );
      `;
      const params = [favoriteID, postID, getTime()];
      await this.dbm.execute(sql, params);
 
      return favoriteID;
    } else {
      return null;
    }
  }
 
  /**
   * Unfavorite a post.
   *
   * @param postID A post's ID.
   */
  public async unfavorite(postID: string): Promise<void> {
    const sql = `DELETE FROM AdminFavorites WHERE postID = ?;`;
    const params = [postID];
    await this.dbm.execute(sql, params);
  }
 
  /**
   * Check if a post is an admin favorite.
   *
   * @param postID A post's ID.
   * @returns Whether or not the post is an admin favorite.
   */
  public async isFavorite(postID: string): Promise<boolean> {
    const sql = `SELECT id FROM AdminFavorites WHERE postID = ?;`;
    const params = [postID];
    const rows: AdminFavorites[] = await this.dbm.execute(sql, params);
 
    return rows.length > 0;
  }
 
  /**
   * Get all admin favorites.
   *
   * @returns All admin favorited items.
   */
  public async getFavorites(): Promise<AdminFavorites[]> {
    const sql = `SELECT * FROM AdminFavorites ORDER BY createTime`;
    const params = [];
    const rows: AdminFavorites[] = await this.dbm.execute(sql, params);
 
    return rows;
  }
 
  /**
   * Get all admin favorite posts.
   *
   * @returns All admin favorited posts.
   */
  public async getFavoritePosts(): Promise<Post[]> {
    const sql = `
      SELECT Post.*
        FROM Post
        JOIN AdminFavorites
          ON Post.id = AdminFavorites.postID
      ORDER BY AdminFavorites.createTime;
    `;
    const params = [];
    const rows: Post[] = await this.dbm.execute(sql, params);
 
    return rows;
  }
 
  /**
   * Get the n most recent favorited posts.
   *
   * @param num The number of favorited posts to return.
   * @returns Recently made favorited posts.
   */
  public async getRecentFavorites(num: number): Promise<Post[]> {
    const sql = `
      SELECT Post.*
        FROM Post
        JOIN AdminFavorites
          ON Post.id = AdminFavorites.postID
      ORDER BY Post.createTime DESC
      LIMIT ?;
    `;
    const params = [num];
    const rows: Post[] = await this.dbm.execute(sql, params);
 
    return rows;
  }
}