From 990b94446896c361941b48f11bbc77ab131c3a45 Mon Sep 17 00:00:00 2001 From: JasonChong96 Date: Wed, 29 Jul 2020 16:18:07 +0800 Subject: [PATCH 1/3] docs: documentation for helper methods in UrlRepository --- src/server/repositories/UrlRepository.ts | 52 ++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/server/repositories/UrlRepository.ts b/src/server/repositories/UrlRepository.ts index 4045c82af..7117653e3 100644 --- a/src/server/repositories/UrlRepository.ts +++ b/src/server/repositories/UrlRepository.ts @@ -180,6 +180,11 @@ export class UrlRepository implements UrlRepositoryInterface { } } + /** + * Invalidates the redirect entry on the cache for the input + * short url. + * @param {string} shortUrl The short url to invalidate. + */ private invalidateCache: (shortUrl: string) => Promise = async ( shortUrl, ) => { @@ -190,6 +195,12 @@ export class UrlRepository implements UrlRepositoryInterface { }) } + /** + * Retrieves the long url which the short url redisrects to + * from the database. + * @param {string} shortUrl Short url. + * @returns The long url that the short url redirects to. + */ private getLongUrlFromDatabase: ( shortUrl: string, ) => Promise = async (shortUrl) => { @@ -204,6 +215,12 @@ export class UrlRepository implements UrlRepositoryInterface { return url.longUrl } + /** + * Retrieves the long url which the short url redisrects to + * from the cache. + * @param {string} shortUrl Short url. + * @returns The long url that the short url redirects to. + */ private getLongUrlFromCache: (shortUrl: string) => Promise = ( shortUrl, ) => { @@ -226,6 +243,11 @@ export class UrlRepository implements UrlRepositoryInterface { ) } + /** + * Caches the input short url to long url mapping in redis cache. + * @param {string} shortUrl Short url. + * @param {string} longUrl Long url. + */ private cacheShortUrl: ( shortUrl: string, longUrl: string, @@ -238,6 +260,16 @@ export class UrlRepository implements UrlRepositoryInterface { }) } + /** + * Retrieves relevant urls from database based on the input search parameters. + * @param {string} tableName Name of the database table to retrieve from. + * @param {string} urlVector Vector representation of url. + * @param {string} rankingAlgorithm The ranking algorithm to be used. + * @param {number} limit Maximum number of results to be retrieved. + * @param {number} offset Number of results to ignore. + * @param {string} query The search query. + * @returns Relevant urls in an array. + */ private async getRelevantUrls( tableName: string, urlVector: string, @@ -245,7 +277,7 @@ export class UrlRepository implements UrlRepositoryInterface { limit: number, offset: number, query: string, - ) { + ): Promise> { const rawQuery = ` SELECT ${tableName}.* FROM ${tableName}, plainto_tsquery('english', $query) query @@ -266,10 +298,17 @@ export class UrlRepository implements UrlRepositoryInterface { return urlsModel } + /** + * Generates the ranking algorithm to be used in the ORDER BY clause in the + * SQL statement based on the input sort order. + * @param {SearchResultsSortOrder} order + * @param {string} tableName + * @returns The clause as a string. + */ private getRankingAlgorithm( order: SearchResultsSortOrder, tableName: string, - ) { + ): string { let rankingAlgorithm switch (order) { case SearchResultsSortOrder.Relevance: @@ -294,10 +333,17 @@ export class UrlRepository implements UrlRepositoryInterface { return rankingAlgorithm } + /** + * Retrieves the number of urls that match the plain text search + * query. + * @param {string} tableName Name of the table urls are stored in. + * @param {string} query Search query. + * @returns Number of matching urls. + */ private async getPlainTextSearchResultsCount( tableName: string, query: string, - ) { + ): Promise { const rawCountQuery = ` SELECT count(*) FROM ${tableName}, plainto_tsquery('english', $query) query From 1590756f3f03ad5186fa0a20ad2797b8c168b5be Mon Sep 17 00:00:00 2001 From: JasonChong96 Date: Wed, 29 Jul 2020 16:25:33 +0800 Subject: [PATCH 2/3] docs: add documentation to search controller" --- .../controllers/interfaces/SearchControllerInterface.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/server/controllers/interfaces/SearchControllerInterface.ts b/src/server/controllers/interfaces/SearchControllerInterface.ts index 091ab9803..efc5c396a 100644 --- a/src/server/controllers/interfaces/SearchControllerInterface.ts +++ b/src/server/controllers/interfaces/SearchControllerInterface.ts @@ -1,5 +1,11 @@ import Express from 'express' export interface SearchControllerInterface { + /** + * Controller for plain text search on urls. + * @param {Express.Request} req Express request. + * @param {Express.Response} res Express response. + * @returns Empty promise that resolves when the request has been processed. + */ urlSearchPlainText(req: Express.Request, res: Express.Response): Promise } From eb72e906a722f67ad4f5883933160bc3bc0c6b9c Mon Sep 17 00:00:00 2001 From: JasonChong96 Date: Wed, 29 Jul 2020 17:20:41 +0800 Subject: [PATCH 3/3] fix: typo --- src/server/repositories/UrlRepository.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/repositories/UrlRepository.ts b/src/server/repositories/UrlRepository.ts index 7117653e3..264b9dfd2 100644 --- a/src/server/repositories/UrlRepository.ts +++ b/src/server/repositories/UrlRepository.ts @@ -196,7 +196,7 @@ export class UrlRepository implements UrlRepositoryInterface { } /** - * Retrieves the long url which the short url redisrects to + * Retrieves the long url which the short url redirects to * from the database. * @param {string} shortUrl Short url. * @returns The long url that the short url redirects to. @@ -216,7 +216,7 @@ export class UrlRepository implements UrlRepositoryInterface { } /** - * Retrieves the long url which the short url redisrects to + * Retrieves the long url which the short url redirects to * from the cache. * @param {string} shortUrl Short url. * @returns The long url that the short url redirects to.