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 | 5x 5x 5x 3x 2x 2x 10x 4x 4x 6x 6x 25x 25x 25x 9x 9x 9x 5x 9x 9x 9x | import { iWelcomeFactService, WelcomeFact } from "@/interfaces/iWelcomeFact"; import WelcomeFactEntity from "@/myorm/welcomeFact-entity"; import { FactQueryError, RemoteFactError } from "@/utils/ErrorTypes"; import logger from "@/utils/logger"; import assert from "assert"; export class WelcomeFactService implements iWelcomeFactService { /** * Gets the fact list from the remote repo, this list contains string, lines that * start with '#', these need to be discarded. (currently only enlish) * * @precondition Connected to the internet * * @returns A list of string from the remote repo, these need to be parsed * to remove comments * * @throws RemoteFactError there is an issue retrieving the remote fact list */ async getRemoteFactList(): Promise<string[]> { try { const response = await fetch( "https://raw.githubusercontent.com/ThompsonC-collab/immsapp-data/refs/heads/main/welcomeFacts.txt" ); const factList = await response.text(); return factList.trim().split("\n"); } catch (error) { if (error instanceof TypeError) { throw new RemoteFactError( `Response not of type 'txt', Error: ${error}` ); } else E{ throw new RemoteFactError(); } } } /** * Wipes the current fact list and saves the new one to the database. * * This works because the random fact is retrieved before the table is * cleared. * * @precondition list cannot be empty * @async * @param list The list of facts to save into the database */ async saveFactList(list: WelcomeFact[]): Promise<void> { if (list.length === 0) { logger.warning("The list of WelcomeFacts is empty"); // Do not clear as there is nothing to add return; } // Simply remove all other facts WelcomeFactEntity.clear(); // Add each fact into the database for (const fact of list) { logger.debug(fact); const factEntity = new WelcomeFactEntity(fact); await factEntity.save(); } } /** * * Gets a random fact from the WelcomeFact table. * * @precondition the WelcomeFact table must exist * @precondition A fact must exist in the database. * * @param language the language the fact should be in. Defaults to english !!NOT IMPLEMENTED!! * @returns a promise containng a 'random' fact * * @throws FaceQueryError if there is an issue with the WelcomeFaceEntity query. */ async getRandomFact( language: "english" | "french" = "english" ): Promise<WelcomeFact> { try { const numberOfFacts = await WelcomeFactEntity.count(); if (numberOfFacts === 0) { logger.warning("There are no welcome facts in the DB"); } const randomN = Math.floor(Math.random() * numberOfFacts); const randomFact: WelcomeFact[] = await WelcomeFactEntity.query( `SELECT * FROM $table LIMIT 1 OFFSET ?`, [randomN] ); return randomFact[0]; } catch (error) { logger.error(error); throw new FactQueryError(); } } } |