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

Commit

Permalink
Merge branch 'master' into prod
Browse files Browse the repository at this point in the history
  • Loading branch information
sopyb committed Sep 13, 2023
2 parents 3ef35f0 + f6ed798 commit d6fb327
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 52 deletions.
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.0",
"version": "2.0.1",
"description": "Navigo Learn API",
"repository": "https://github.com/NavigoLearn/API.git",
"author": "Navigo",
Expand Down
8 changes: 6 additions & 2 deletions src/constants/Paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const Paths = {
},
Update: {
Base: '/:roadmapId([0-9]+)',
// Owner only
All: '/',
About: '/about',
Name: '/name',
Expand All @@ -39,10 +40,13 @@ const Paths = {
Data: '/data',
MiscData: '/misc-data', // used for roadmap wide data like roadmap theme
Version: '/version',

// Everyone
Progress: '/progress',
Like: '/:roadmapId([0-9]+)/like',
Dislike: '/:roadmapId([0-9]+)/dislike',
},
Delete: '/:roadmapId([0-9]+)',
Like: '/:roadmapId([0-9]+)/like',
Dislike: '/:roadmapId([0-9]+)/dislike',
},
Users: {
Base: '/users',
Expand Down
108 changes: 77 additions & 31 deletions src/controllers/roadmapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@ import {
responseRoadmapCreated,
responseRoadmapDeleted,
responseRoadmapNotFound,
responseRoadmapNotRated,
responseRoadmapNotRated, responseRoadmapProgressUpdated,
responseRoadmapRated,
responseRoadmapUnrated,
responseRoadmapUpdated,
} from '@src/helpers/responses/roadmapResponses';
import {
deleteDBRoadmap,
deleteRoadmapLike,
getRoadmapData,
getRoadmapLike,
getRoadmapObject,
getRoadmapLike, getRoadmapProgress,
getUser,
insertRoadmap,
insertRoadmapLike,
updateRoadmap,
updateRoadmapLike,
updateRoadmapLike, updateRoadmapProgress, insertRoadmapProgress,
} from '@src/helpers/databaseManagement';
import { RequestWithBody } from '@src/middleware/validators/validateBody';
import { Roadmap, RoadmapTopic } from '@src/types/models/Roadmap';
import { ResFullRoadmap } from '@src/types/response/ResFullRoadmap';
import { addRoadmapView } from '@src/util/Views';
import logger from 'jet-logger';
import { RoadmapProgress } from '@src/types/models/RoadmapProgress';

export async function createRoadmap(req: RequestWithBody, res: Response) {
// guaranteed to exist by middleware
Expand Down Expand Up @@ -83,8 +84,8 @@ export async function getRoadmap(req: RequestWithSession, res: Response) {

const db = new Database();

const roadmap = await getRoadmapData(db, BigInt(roadmapId));
if (!roadmap) return responseRoadmapNotFound(res);
const roadmap = await getRoadmapObject(db, BigInt(roadmapId));
if (roadmap === null) return responseRoadmapNotFound(res);

const user = await getUser(db, roadmap.userId);

Expand Down Expand Up @@ -133,9 +134,9 @@ export async function updateAboutRoadmap(req: RequestWithBody, res: Response) {

const db = new Database();

const roadmap = await getRoadmapData(db, BigInt(roadmapId));
const roadmap = await getRoadmapObject(db, BigInt(roadmapId));

if (!roadmap) return responseRoadmapNotFound(res);
if (roadmap === null) return responseRoadmapNotFound(res);
if (roadmap.userId !== userId) return responseNotAllowed(res);

const { name, description, topic, miscData } = req.body;
Expand Down Expand Up @@ -168,9 +169,9 @@ export async function updateAllRoadmap(req: RequestWithBody, res: Response) {

const db = new Database();

const roadmap = await getRoadmapData(db, BigInt(roadmapId));
const roadmap = await getRoadmapObject(db, BigInt(roadmapId));

if (!roadmap) return responseRoadmapNotFound(res);
if (roadmap === null) return responseRoadmapNotFound(res);
if (roadmap.userId !== userId) return responseNotAllowed(res);

const { name, description, data, topic, miscData, isDraft } = req.body;
Expand Down Expand Up @@ -205,9 +206,9 @@ export async function updateNameRoadmap(req: RequestWithBody, res: Response) {

const db = new Database();

const roadmap = await getRoadmapData(db, BigInt(roadmapId));
const roadmap = await getRoadmapObject(db, BigInt(roadmapId));

if (!roadmap) return responseRoadmapNotFound(res);
if (roadmap === null) return responseRoadmapNotFound(res);
if (roadmap.userId !== userId) return responseNotAllowed(res);

const { name } = req.body;
Expand All @@ -234,9 +235,9 @@ export async function updateDescriptionRoadmap(

const db = new Database();

const roadmap = await getRoadmapData(db, BigInt(roadmapId));
const roadmap = await getRoadmapObject(db, BigInt(roadmapId));

if (!roadmap) return responseRoadmapNotFound(res);
if (roadmap === null) return responseRoadmapNotFound(res);
if (roadmap.userId !== userId) return responseNotAllowed(res);

const { description } = req.body;
Expand All @@ -260,9 +261,9 @@ export async function updateDataRoadmap(req: RequestWithBody, res: Response) {

const db = new Database();

const roadmap = await getRoadmapData(db, BigInt(roadmapId));
const roadmap = await getRoadmapObject(db, BigInt(roadmapId));

if (!roadmap) return responseRoadmapNotFound(res);
if (roadmap === null) return responseRoadmapNotFound(res);
if (roadmap.userId !== userId) return responseNotAllowed(res);

const { data } = req.body;
Expand All @@ -286,9 +287,9 @@ export async function updateTopicRoadmap(req: RequestWithBody, res: Response) {

const db = new Database();

const roadmap = await getRoadmapData(db, BigInt(roadmapId));
const roadmap = await getRoadmapObject(db, BigInt(roadmapId));

if (!roadmap) return responseRoadmapNotFound(res);
if (roadmap === null) return responseRoadmapNotFound(res);
if (roadmap.userId !== userId) return responseNotAllowed(res);

const { topic } = req.body;
Expand Down Expand Up @@ -318,9 +319,9 @@ export async function updateMiscDataRoadmap(

const db = new Database();

const roadmap = await getRoadmapData(db, BigInt(roadmapId));
const roadmap = await getRoadmapObject(db, BigInt(roadmapId));

if (!roadmap) return responseRoadmapNotFound(res);
if (roadmap === null) return responseRoadmapNotFound(res);
if (roadmap.userId !== userId) return responseNotAllowed(res);

const { miscData } = req.body;
Expand All @@ -347,9 +348,9 @@ export async function updateIsDraftRoadmap(

const db = new Database();

const roadmap = await getRoadmapData(db, BigInt(roadmapId));
const roadmap = await getRoadmapObject(db, BigInt(roadmapId));

if (!roadmap) return responseRoadmapNotFound(res);
if (roadmap === null) return responseRoadmapNotFound(res);
if (roadmap.userId !== userId) return responseNotAllowed(res);

const { isDraft } = req.body;
Expand Down Expand Up @@ -381,9 +382,9 @@ export async function updateVersionRoadmap(

const db = new Database();

const roadmap = await getRoadmapData(db, BigInt(roadmapId));
const roadmap = await getRoadmapObject(db, BigInt(roadmapId));

if (!roadmap) return responseRoadmapNotFound(res);
if (roadmap === null) return responseRoadmapNotFound(res);

if (roadmap.userId !== userId) return responseNotAllowed(res);

Expand All @@ -404,9 +405,9 @@ export async function deleteRoadmap(req: RequestWithSession, res: Response) {

const db = new Database();

const roadmap = await getRoadmapData(db, BigInt(roadmapId));
const roadmap = await getRoadmapObject(db, BigInt(roadmapId));

if (!roadmap) return responseRoadmapNotFound(res);
if (roadmap === null) return responseRoadmapNotFound(res);
if (roadmap.userId !== userId) return responseNotAllowed(res);

if (await deleteDBRoadmap(db, BigInt(roadmapId)))
Expand All @@ -415,6 +416,8 @@ export async function deleteRoadmap(req: RequestWithSession, res: Response) {
return responseServerError(res);
}

// ! everyone controllers below

export async function likeRoadmap(req: RequestWithSession, res: Response) {
const roadmapId = req.params.roadmapId;
const userId = req.session?.userId;
Expand All @@ -426,7 +429,7 @@ export async function likeRoadmap(req: RequestWithSession, res: Response) {

const likeEntry = await getRoadmapLike(db, userId, BigInt(roadmapId));

if (!likeEntry) {
if (likeEntry === null) {
if (
(await insertRoadmapLike(
db,
Expand All @@ -440,7 +443,7 @@ export async function likeRoadmap(req: RequestWithSession, res: Response) {
return responseRoadmapRated(res);
}

if (!likeEntry) return responseServerError(res);
if (likeEntry === null) return responseServerError(res);
if (likeEntry.value == 1) return responseRoadmapAlreadyLiked(res);

likeEntry.set({ value: 1 });
Expand All @@ -462,7 +465,7 @@ export async function dislikeRoadmap(req: RequestWithSession, res: Response) {

const liked = await getRoadmapLike(db, userId, BigInt(roadmapId));

if (!liked) {
if (liked === null) {
if (
(await insertRoadmapLike(
db,
Expand All @@ -476,7 +479,7 @@ export async function dislikeRoadmap(req: RequestWithSession, res: Response) {
return responseRoadmapRated(res);
}

if (!liked) return responseServerError(res);
if (liked === null) return responseServerError(res);
if (liked.value == -1) return responseRoadmapAlreadyDisliked(res);

liked.set({ value: -1 });
Expand All @@ -501,9 +504,52 @@ export async function removeLikeRoadmap(

const liked = await getRoadmapLike(db, userId, BigInt(roadmapId));

if (!liked) return responseRoadmapNotRated(res);
if (liked == null) return responseRoadmapNotRated(res);

if (await deleteRoadmapLike(db, liked)) return responseRoadmapUnrated(res);

return responseServerError(res);
}

export async function updateProgressDataRoadmap(
req: RequestWithBody,
res: Response,
) {
const roadmapId = req.params.roadmapId;
const userId = req.session?.userId;

const { data } = req.body;

if (!userId) return responseServerError(res);
if (!roadmapId) return responseServerError(res);
if (!data) return responseServerError(res);

const db = new Database();

const roadmap = await getRoadmapObject(db, BigInt(roadmapId));

if (roadmap === null) return responseRoadmapNotFound(res);

const progress = await getRoadmapProgress(db, userId, BigInt(roadmapId));

if (progress === null) {
if (
(await insertRoadmapProgress(
db,
new RoadmapProgress({
userId,
roadmapId: BigInt(roadmapId),
data: data as string,
}),
)) !== -1n
)
return responseRoadmapProgressUpdated(res);
} else {
progress.set({ data: data as string });

if (await updateRoadmapProgress(db, progress.id, progress))
return responseRoadmapProgressUpdated(res);
}

return responseServerError(res);
}
36 changes: 35 additions & 1 deletion src/helpers/databaseManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IUser, User } from '@src/types/models/User';
import { Roadmap } from '@src/types/models/Roadmap';
import { Follower } from '@src/types/models/Follower';
import { IRoadmapLike, RoadmapLike } from '@src/types/models/RoadmapLike';
import { RoadmapProgress } from '@src/types/models/RoadmapProgress';

/*
* Interfaces
Expand Down Expand Up @@ -202,7 +203,7 @@ export async function updateUserInfo(
return await db.updateWhere('userInfo', userInfo, 'userId', userId);
}

export async function getRoadmapData(
export async function getRoadmapObject(
db: DatabaseDriver,
roadmapId: bigint,
): Promise<Roadmap | null> {
Expand Down Expand Up @@ -278,3 +279,36 @@ export async function deleteRoadmapLike(
data.roadmapId,
);
}

export async function getRoadmapProgress(
db: DatabaseDriver,
userId: bigint,
roadmapId: bigint,
): Promise<RoadmapProgress | null> {
const progress = await db.getWhere<RoadmapProgress>(
'roadmapProgress',
'userId',
userId,
'roadmapId',
roadmapId,
);

if (!progress) return null;

return new RoadmapProgress(progress);
}

export async function insertRoadmapProgress(
db: DatabaseDriver,
data: RoadmapProgress,
) {
return await db.insert('roadmapProgress', data);
}

export async function updateRoadmapProgress(
db: DatabaseDriver,
id: bigint,
data: RoadmapProgress,
): Promise<boolean> {
return await db.update('roadmapProgress', id, data);
}
7 changes: 7 additions & 0 deletions src/helpers/responses/roadmapResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,10 @@ export function responseRoadmapUnrated(res: Response) {
success: true,
});
}

export function responseRoadmapProgressUpdated(res: Response) {
return res.status(HttpStatusCodes.OK).json({
message: 'Roadmap progress updated',
success: true,
});
}
16 changes: 0 additions & 16 deletions src/routes/RoadmapsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import validateSession from '@src/middleware/validators/validateSession';
import {
createRoadmap,
deleteRoadmap,
dislikeRoadmap,
likeRoadmap,
removeLikeRoadmap,
} from '@src/controllers/roadmapController';
import validateBody from '@src/middleware/validators/validateBody';

Expand All @@ -33,17 +30,4 @@ RoadmapsRouter.use(Paths.Roadmaps.Get.Base, GetRouter);
RoadmapsRouter.use(Paths.Roadmaps.Update.Base, UpdateRouter);

RoadmapsRouter.delete(Paths.Roadmaps.Delete, validateSession, deleteRoadmap);

/*
! like roadmaps
*/
RoadmapsRouter.all(Paths.Roadmaps.Like, validateSession);
RoadmapsRouter.all(Paths.Roadmaps.Dislike, validateSession);

RoadmapsRouter.get(Paths.Roadmaps.Like, likeRoadmap);
RoadmapsRouter.get(Paths.Roadmaps.Dislike, dislikeRoadmap);

RoadmapsRouter.delete(Paths.Roadmaps.Like, removeLikeRoadmap);
RoadmapsRouter.delete(Paths.Roadmaps.Dislike, removeLikeRoadmap);

export default RoadmapsRouter;
Loading

0 comments on commit d6fb327

Please sign in to comment.