All files postImage.ts

100% Statements 59/59
100% Branches 12/12
100% Functions 9/9
100% Lines 48/48

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          20x                                                       20x             20x 10x               10x 10x   10x                 20x 14x             14x 28x                   20x       13x 26x   13x                   20x       16x   38x 11x 11x     16x                 20x 7x 7x 7x   7x               20x 11x 11x 11x   13x   11x 11x 22x   12x 10x 10x 20x                     20x       2x   3x 1x 1x 2x   2x   1x   1x     20x  
/**
 * Services for the post image table.
 * @packageDocumentation
 */
 
import { BaseService } from "./util";
import { Image } from "./image";
 
/**
 * Post image architecture.
 */
export interface PostImage {
  postID: string;
  imageID: string;
}
 
/**
 * Post image count architecture.
 */
interface PostImageCount {
  count: number;
}
 
/**
 * Post image with only ID architecture.
 */
interface PostImageID {
  imageID: string;
}
 
/**
 * Post image services.
 */
export class PostImageService extends BaseService {
  /**
   * Get all images associated with a post.
   *
   * @param postID A post's ID.
   * @returns The images associated with the post.
   */
  public async getPostImages(postID: string): Promise<Image[]> {
    const sql = `
      SELECT Image.*
        FROM Image
        JOIN (
          SELECT * FROM PostImage WHERE postID = ?
        ) AS PostImageRecords ON Image.id = PostImageRecords.imageID
      ORDER BY PostImageRecords.id;
    `;
    const params = [postID];
    const rows: Image[] = await this.dbm.execute(sql, params);
 
    return rows;
  }
 
  /**
   * Associate an image with a post.
   *
   * @param postID A post's ID.
   * @param imageID An image's ID.
   */
  public async setPostImage(postID: string, imageID: string): Promise<void> {
    const sql = `
      INSERT INTO PostImage (
        postID, imageID
      ) VALUES (
        ?, ?
      );
    `;
    const params = [postID, imageID];
    await this.dbm.execute(sql, params);
  }
 
  /**
   * Create a new image and associate it with a post.
   *
   * @param postID A post's ID.
   * @param imageData The binary data of the new image.
   * @returns The new image's ID.
   */
  public async createPostImage(
    postID: string,
    imageData: Buffer
  ): Promise<string> {
    const imageID = await this.dbm.imageService.createImage(imageData);
    await this.setPostImage(postID, imageID);
 
    return imageID;
  }
 
  /**
   * Create new images and associate them with a post.
   *
   * @param postID A post's ID.
   * @param imageData The binary data of the new images.
   * @returns The IDs of the new images.
   */
  public async createPostImages(
    postID: string,
    imageData: Buffer[]
  ): Promise<string[]> {
    let imageIDs: string[] = [];
 
    for (const data of imageData) {
      const imageID = await this.createPostImage(postID, data);
      imageIDs.push(imageID);
    }
 
    return imageIDs;
  }
 
  /**
   * Get the number of images associated with a post.
   *
   * @param postID A post's ID.
   * @returns The number of images associated with the post.
   */
  public async numImages(postID: string): Promise<number> {
    const sql = `SELECT COUNT(*) AS count FROM PostImage WHERE postID = ?;`;
    const params = [postID];
    const rows: PostImageCount[] = await this.dbm.execute(sql, params);
 
    return rows[0].count;
  }
 
  /**
   * Delete all images associated with a post.
   *
   * @param postID A post's ID.
   */
  public async deletePostImages(postID: string): Promise<void> {
    let sql = `SELECT imageID FROM PostImage WHERE postID = ?`;
    let params: any = [postID];
    const rows: PostImageID[] = await this.dbm.execute(sql, params);
 
    const imageIDs = rows.map((postImage) => postImage.imageID);
 
    sql = `DELETE FROM PostImage WHERE postID = ?;`;
    params = [postID];
    await this.dbm.execute(sql, params);
 
    if (imageIDs.length > 0) {
      sql = `DELETE FROM Image WHERE id IN (?);`;
      params = [imageIDs];
      await this.dbm.execute(sql, params);
    }
  }
 
  /**
   * Delete a specific image from a post.
   *
   * @param postID A post's ID.
   * @param index The index of the post's image.
   * @returns Whether or not the deletion succeeded.
   */
  public async deletePostImage(
    postID: string,
    index: number
  ): Promise<boolean> {
    const images = await this.getPostImages(postID);
 
    if (index >= 0 && index < images.length) {
      const sql = `DELETE FROM PostImage WHERE imageID = ?;`;
      const params = [images[index].id];
      await this.dbm.execute(sql, params);
 
      await this.dbm.imageService.deleteImage(images[index].id);
 
      return true;
    } else {
      return false;
    }
  }
}