diff --git a/package-lock.json b/package-lock.json index d738128..27694a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "navigo-learn-api", - "version": "2.0.0", + "version": "2.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "navigo-learn-api", - "version": "2.0.0", + "version": "2.0.2", "license": "BSD 3-Clause", "dependencies": { "axios": "^1.4.0", diff --git a/package.json b/package.json index 34d8362..919278d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "navigo-learn-api", - "version": "2.0.1", + "version": "2.0.2", "description": "Navigo Learn API", "repository": "https://github.com/NavigoLearn/API.git", "author": "Navigo", diff --git a/src/constants/Paths.ts b/src/constants/Paths.ts index 5767219..1a73b47 100644 --- a/src/constants/Paths.ts +++ b/src/constants/Paths.ts @@ -19,6 +19,7 @@ const Paths = { Search: { Base: '/search', Roadmaps: '/roadmaps', + FeelingLucky: '/feeling-lucky', }, Roadmaps: { Base: '/roadmaps', diff --git a/src/controllers/searchController.ts b/src/controllers/searchController.ts new file mode 100644 index 0000000..52bd71b --- /dev/null +++ b/src/controllers/searchController.ts @@ -0,0 +1,37 @@ +import { + RequestWithSearchParameters, +} from '@src/middleware/validators/validateSearchParameters'; +import { Request, Response } from 'express'; +import { ExploreDB } from '@src/util/Database/ExploreDB'; +import { + responseFeelingLucky, + responseSearchRoadmaps, +} from '@src/helpers/responses/searchResponses'; +import { + responseRoadmapNotFound, +} from '@src/helpers/responses/roadmapResponses'; + +export async function searchRoadmaps( + req: RequestWithSearchParameters, + res: Response, +): Promise { + const db = new ExploreDB(); + + const roadmaps = await db.getRoadmaps(req, req.session?.userId); + + return responseSearchRoadmaps(res, roadmaps.result, roadmaps.totalRoadmaps); +} + +export async function feelingLuckyRoadmap( + req: Request, + res: Response, +): Promise { + const db = new ExploreDB(); + + const roadmap = await db.getRandomRoadmapId(); + + if (!roadmap) + return responseRoadmapNotFound(res); + + return responseFeelingLucky(res, roadmap); +} \ No newline at end of file diff --git a/src/controllers/exploreController.ts b/src/helpers/responses/searchResponses.ts similarity index 51% rename from src/controllers/exploreController.ts rename to src/helpers/responses/searchResponses.ts index 34bf966..5e19fff 100644 --- a/src/controllers/exploreController.ts +++ b/src/helpers/responses/searchResponses.ts @@ -1,13 +1,9 @@ -import { - RequestWithSearchParameters, -} from '@src/middleware/validators/validateSearchParameters'; import { Response } from 'express'; -import { ExploreDB } from '@src/util/Database/ExploreDB'; -import HttpStatusCodes from '@src/constants/HttpStatusCodes'; import { ResRoadmap } from '@src/types/response/ResRoadmap'; +import HttpStatusCodes from '@src/constants/HttpStatusCodes'; import { JSONSafety } from '@src/util/misc'; -function responseSearchRoadmaps( +export function responseSearchRoadmaps( res: Response, roadmaps: ResRoadmap[], total: bigint, @@ -22,13 +18,17 @@ function responseSearchRoadmaps( ); } -export async function searchRoadmaps( - req: RequestWithSearchParameters, +export function responseFeelingLucky( res: Response, -): Promise { - const db = new ExploreDB(); - - const roadmaps = await db.getRoadmaps(req, req.session?.userId); - - return responseSearchRoadmaps(res, roadmaps.result, roadmaps.totalRoadmaps); -} + roadmap: bigint, +): unknown { + return res.status(HttpStatusCodes.OK).json( + JSONSafety( + { + success: true, + message: 'Roadmap found', + data: roadmap, + }, + ), + ); +} \ No newline at end of file diff --git a/src/routes/SearchRouter.ts b/src/routes/SearchRouter.ts index deec899..d19aae4 100644 --- a/src/routes/SearchRouter.ts +++ b/src/routes/SearchRouter.ts @@ -2,7 +2,10 @@ import { Router } from 'express'; import Paths from '@src/constants/Paths'; import validateSearchParameters from '@src/middleware/validators/validateSearchParameters'; -import { searchRoadmaps } from '@src/controllers/exploreController'; +import { + feelingLuckyRoadmap, + searchRoadmaps, +} from '@src/controllers/searchController'; const SearchRouter = Router(); @@ -12,4 +15,9 @@ SearchRouter.get( searchRoadmaps, ); +SearchRouter.get( + Paths.Search.FeelingLucky, + feelingLuckyRoadmap, +); + export default SearchRouter; diff --git a/src/util/Database/ExploreDB.ts b/src/util/Database/ExploreDB.ts index 9e8505e..bead8cf 100644 --- a/src/util/Database/ExploreDB.ts +++ b/src/util/Database/ExploreDB.ts @@ -113,6 +113,28 @@ class ExploreDB extends Database { totalRoadmaps: result2, }; } + + public async getRandomRoadmapId(): Promise { + const query = ` + SELECT id + FROM roadmaps + WHERE isPublic = 1 + AND isDraft = 0 + ORDER BY RAND() + LIMIT 1 + `; + + const result = await this.getQuery(query); + + if (result === null) + return null; + if(result.length === 0) + return null; + if(result[0].id === null) + return null; + + return result[0].id as bigint; + } } export { ExploreDB };