All files image.ts

100% Statements 28/28
100% Branches 1/1
100% Functions 5/5
100% Lines 23/23

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          20x                                         20x             20x 17x   17x 17x 34x   17x                 20x 4x 4x 4x   4x                 20x 6x 6x 6x   6x               20x 20x 20x 40x   20x  
/**
 * Services for the image table.
 * @packageDocumentation
 */
 
import { BaseService, getTime, newUniqueID } from "./util";
 
/**
 * Image architecture.
 */
export interface Image {
  id: string;
  data: Buffer;
  registerTime: number;
}
 
/**
 * Image with only ID architecture.
 */
interface ImageID {
  id: string;
}
 
/**
 * Image services.
 */
export class ImageService extends BaseService {
  /**
   * Create an image.
   *
   * @param data Image binary data.
   * @returns The new image's ID.
   */
  public async createImage(data: Buffer): Promise<string> {
    const imageID = await newUniqueID(this.dbm, "Image");
 
    const sql = `INSERT INTO Image (id, data, registerTime) VALUES (?, ?, ?);`;
    const params = [imageID, data, getTime()];
    await this.dbm.execute(sql, params);
 
    return imageID;
  }
 
  /**
   * Check if an image exists.
   *
   * @param imageID An image's ID.
   * @returns Whether or not the image exists.
   */
  public async imageExists(imageID: string): Promise<boolean> {
    const sql = `SELECT id FROM Image WHERE id = ?;`;
    const params = [imageID];
    const rows: ImageID[] = await this.dbm.execute(sql, params);
 
    return rows.length > 0;
  }
 
  /**
   * Get an image.
   *
   * @param imageID An image's ID.
   * @returns The image.
   */
  public async getImage(imageID: string): Promise<Image> {
    const sql = `SELECT * from Image WHERE id = ?;`;
    const params = [imageID];
    const rows: Image[] = await this.dbm.execute(sql, params);
 
    return rows[0];
  }
 
  /**
   * Delete an image.
   *
   * @param imageID An image's ID.
   */
  public async deleteImage(imageID: string): Promise<void> {
    const sql = `DELETE FROM Image WHERE id = ?;`;
    const params = [imageID];
    await this.dbm.execute(sql, params);
  }
}