Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #58 from NavigoLearn/master
Browse files Browse the repository at this point in the history
Pull master into prod
  • Loading branch information
sopyb authored Sep 13, 2023
2 parents 7c4d5dc + 4f317fa commit 46d1cef
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/constants/Paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const Paths = {
Search: {
Base: '/search',
Roadmaps: '/roadmaps',
FeelingLucky: '/feeling-lucky',
},
Roadmaps: {
Base: '/roadmaps',
Expand Down
37 changes: 37 additions & 0 deletions src/controllers/searchController.ts
Original file line number Diff line number Diff line change
@@ -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<unknown> {
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<unknown> {
const db = new ExploreDB();

const roadmap = await db.getRandomRoadmapId();

if (!roadmap)
return responseRoadmapNotFound(res);

return responseFeelingLucky(res, roadmap);
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -22,13 +18,17 @@ function responseSearchRoadmaps(
);
}

export async function searchRoadmaps(
req: RequestWithSearchParameters,
export function responseFeelingLucky(
res: Response,
): Promise<unknown> {
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,
},
),
);
}
10 changes: 9 additions & 1 deletion src/routes/SearchRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -12,4 +15,9 @@ SearchRouter.get(
searchRoadmaps,
);

SearchRouter.get(
Paths.Search.FeelingLucky,
feelingLuckyRoadmap,
);

export default SearchRouter;
22 changes: 22 additions & 0 deletions src/util/Database/ExploreDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ class ExploreDB extends Database {
totalRoadmaps: result2,
};
}

public async getRandomRoadmapId(): Promise<bigint | null> {
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 };

0 comments on commit 46d1cef

Please sign in to comment.