All files rating.ts

100% Statements 30/30
100% Branches 1/1
100% Functions 5/5
100% Lines 25/25

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          20x                                                                         20x             20x 10x 10x 10x   10x             10x 20x   10x                 20x 2x 2x 2x   2x                 20x 4x 4x 4x   4x               20x 9x 9x 18x   20x  
/**
 * Services for the rating table.
 * @packageDocumentation
 */
 
import { BaseService, newUniqueID } from "./util";
 
/**
 * Rating architecture.
 */
export interface Rating {
  id: string;
  general: number;
  cost: number | null;
  quality: number | null;
  safety: number | null;
  cleanliness: number | null;
  guestServices: number | null;
}
 
/**
 * Rating parameters.
 */
export interface RatingParams {
  general: number;
  cost?: number;
  quality?: number;
  safety?: number;
  cleanliness?: number;
  guestService?: number;
}
 
/**
 * Rating with only ID architecture.
 */
interface RatingID {
  id: string;
}
 
/**
 * Rating services.
 */
export class RatingService extends BaseService {
  /**
   * Create a rating.
   *
   * @param rating The user's rating.
   * @returns The new rating's ID.
   */
  public async createRating(rating: RatingParams): Promise<string> {
    const ratingID = await newUniqueID(this.dbm, "Rating");
    const cols = Object.keys(rating);
    const values = Object.values(rating);
 
    const sql = `
      INSERT INTO Rating (
        id, ${cols.join(", ")}
      ) VALUES (
        ?, ${"?, ".repeat(values.length).slice(0, -2)}
      );
    `;
    const params = [ratingID, ...values];
    await this.dbm.execute(sql, params);
 
    return ratingID;
  }
 
  /**
   * Check if a rating exists.
   *
   * @param ratingID A rating's ID.
   * @returns Whether or not the rating exists.
   */
  public async ratingExists(ratingID: string): Promise<boolean> {
    const sql = `SELECT id FROM Rating WHERE id = ?;`;
    const params = [ratingID];
    const rows: RatingID[] = await this.dbm.execute(sql, params);
 
    return rows.length > 0;
  }
 
  /**
   * Get a rating.
   *
   * @param ratingID A rating's ID.
   * @returns The rating.
   */
  public async getRating(ratingID: string): Promise<Rating> {
    const sql = `SELECT * FROM Rating WHERE id = ?;`;
    const params = [ratingID];
    const rows: Rating[] = await this.dbm.execute(sql, params);
 
    return rows[0];
  }
 
  /**
   * Delete a rating.
   *
   * @param ratingID A rating's ID.
   */
  public async deleteRating(ratingID: string): Promise<void> {
    const sql = `DELETE FROM Rating WHERE id = ?;`;
    const params = [ratingID];
    await this.dbm.execute(sql, params);
  }
}