Skip to content

Commit ecf0938

Browse files
committed
Merge branch 'main' of https://github.com/call-0f-code/COC-API into Acheivements-Routes
2 parents 0c7bb01 + 33ff4ce commit ecf0938

16 files changed

Lines changed: 1085 additions & 78 deletions

.github/workflows/tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ jobs:
3030
3131
- name: Install dependencies
3232
run: bun install
33+
34+
- name: Generate Prisma client
35+
run: bun run generate
3336

3437
- name: Run tests
3538
run: bun jest
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Request, Response } from "express";
2+
import { ApiError } from "../utils/apiError";
3+
import * as progressServices from "../services/progress.service"
4+
5+
export const getCompletedQuestion = async(req:Request,res:Response)=>{
6+
const memberId = req.params.memberId;
7+
if(!memberId){
8+
throw new ApiError("required field is missing",400);
9+
}
10+
11+
const completedQuestion = await progressServices.getCompletedQuestion(memberId);
12+
res.status(200).json({
13+
status:"SUCCESS",
14+
completedQuestion
15+
})
16+
17+
}
18+
19+
export const toggleQuestions = async(req:Request,res:Response) =>{
20+
const memberId = req.params.memberId;
21+
const questionId = parseInt(req.params.questionId);
22+
if(!memberId || !questionId){
23+
throw new ApiError("required field is missing",400);
24+
}
25+
26+
await progressServices.markQuestion(questionId,memberId);
27+
res.status(200).json({
28+
status:"SUCCESS",
29+
})
30+
31+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { Request, Response } from 'express'
2+
import * as questionServices from '../services/question.service'
3+
import { ApiError } from '../utils/apiError';
4+
5+
6+
export const getQuestionByTopicId = async (req: Request, res: Response) => {
7+
const topicId = parseInt(req.params.topicId);
8+
if (!topicId) {
9+
throw new ApiError("required field missing", 400);
10+
}
11+
12+
const questions = await questionServices.getQuestionByTopicId(topicId);
13+
14+
res.status(200).json({
15+
status: "SUCCESS",
16+
questions
17+
})
18+
19+
20+
}
21+
22+
export const addQuestion = async (req: Request, res: Response) => {
23+
24+
const topicId = parseInt(req.params.topicId);
25+
const { questionName, difficulty, link } = req.body
26+
const adminId = req.body.adminId;
27+
28+
if (!questionName || !difficulty || !link || !adminId || !topicId) {
29+
throw new ApiError("required field missing", 400);
30+
}
31+
32+
const question = await questionServices.addQuestionByTopicId(adminId, topicId, link, difficulty, questionName);
33+
res.status(201).json({
34+
status: "SUCCESS",
35+
question
36+
})
37+
38+
39+
}
40+
41+
export const getQuestionByQuestionId = async (req: Request, res: Response) => {
42+
const questionId = parseInt(req.params.questionId);
43+
if (!questionId) {
44+
throw new ApiError("required field missing", 400);
45+
}
46+
47+
const question = await questionServices.getQuestionByQuestionId(questionId);
48+
49+
if (!question) {
50+
throw new ApiError("No question found with this ID", 404);
51+
}
52+
res.status(200).json({
53+
status: "SUCCESS",
54+
question
55+
})
56+
57+
58+
59+
}
60+
61+
62+
export const updateQuestion = async (req: Request, res: Response) => {
63+
const questionId = parseInt(req.params.questionId);
64+
65+
const adminId = req.body.adminId;
66+
const questionData: questionData = req.body.questionData;
67+
68+
if (!questionData || JSON.stringify(questionData) === "{}" || !adminId) {
69+
throw new ApiError("required field missing", 400);
70+
}
71+
questionData.updatedById = adminId;
72+
73+
const question = await questionServices.updateQuestion(questionData, questionId);
74+
75+
if (!question) {
76+
throw new ApiError("No question found with this ID", 404);
77+
}
78+
79+
res.status(200).json({
80+
status: "SUCCESS",
81+
question
82+
})
83+
84+
}
85+
86+
export const deleteQuestion = async (req: Request, res: Response) => {
87+
const questionId = parseInt(req.params.questionId);
88+
if (!questionId) {
89+
throw new ApiError("required field missing ", 400);
90+
}
91+
92+
93+
await questionServices.deleteQuestion(questionId);
94+
95+
res.status(200).json({
96+
status: "SUCCESS"
97+
})
98+
99+
}
100+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Request, Response } from 'express'
2+
import * as topicServices from '../services/topic.service'
3+
import { ApiError } from '../utils/apiError';
4+
5+
6+
export const getTopics = async (req: Request, res: Response) => {
7+
8+
const topics = await topicServices.getTopics();
9+
10+
res.status(201).json(topics)
11+
12+
}
13+
14+
15+
16+
export const createTopic = async (req: Request, res: Response) => {
17+
const { title, description } = req.body
18+
19+
const adminId = req.body.adminId
20+
if (!title || !description || !adminId) {
21+
throw new ApiError("missing required fields", 400);
22+
}
23+
const topic = await topicServices.createTopics(title, description, adminId);
24+
res.status(201).json(topic);
25+
26+
}
27+
28+
29+
30+
export const updateTopic = async (req: Request, res: Response) => {
31+
const topicId = parseInt(req.params.topicId);
32+
33+
const adminId = req.body.adminId
34+
35+
const updateData: topicData = req.body.updateData
36+
37+
if (!updateData || JSON.stringify(updateData) === '{}' || !adminId) {
38+
throw new ApiError("missing required fields", 400);
39+
}
40+
41+
42+
const updatedTopic = await topicServices.updateTopic(topicId, updateData, adminId);
43+
res.status(200).json(updatedTopic)
44+
45+
46+
}
47+
48+
export const deleteTopic = async (req: Request, res: Response) => {
49+
const topicId = parseInt(req.params.topicId);
50+
if (!topicId) {
51+
throw new ApiError("missing required fields", 400);
52+
}
53+
54+
await topicServices.deleteTopic(topicId);
55+
res.status(200).json({
56+
status: "SUCCESS"
57+
})
58+
59+
}
60+

src/routes/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Router } from 'express'
22
import { Multer } from 'multer'
33
import { SupabaseClient } from '@supabase/supabase-js'
4-
// import membersRouter from './members'
54
import projectsRouter from './projects'
65
import acheivementsRouter from './achievements'
76
import interviewRouter from './interviews'
8-
9-
// bakiche tumhi import kara mala kantala ala
7+
import topicRouter from './topics'
8+
import quetionsRouter from './questions'
9+
import progressRouter from './progress'
1010

1111
export default function routes(upload: Multer, supabase: SupabaseClient) {
1212
const router = Router()
@@ -17,6 +17,13 @@ export default function routes(upload: Multer, supabase: SupabaseClient) {
1717
router.use('/achievements' ,acheivementsRouter(upload, supabase));
1818
router.use('/interviews', interviewRouter(upload, supabase));
1919
// … mount other routers, just write xyzRouter() if the routes in that router don't need multer or supabase
20+
21+
router.use('/topics',topicRouter());
22+
23+
router.use('/questions',quetionsRouter());
24+
25+
router.use('/progress',progressRouter());
26+
2027

2128
return router
2229
}

src/routes/progress.ts

Lines changed: 34 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,40 @@
11
import { Router } from 'express'
2-
import { Multer } from 'multer'
3-
import { SupabaseClient } from '@supabase/supabase-js'
4-
import {
5-
addMembers,
6-
createProject,
7-
deleteProjects,
8-
getMembersByProjectId,
9-
getProjectById,
10-
getProjects,
11-
removeMembers,
12-
updateProjects
13-
} from '../controllers/project.controller'
2+
import { getCompletedQuestion, toggleQuestions } from '../controllers/progress.controller'
143

15-
16-
17-
export default function projectsRouter(
18-
upload: Multer,
19-
supabase: SupabaseClient
20-
) {
4+
export default function progressRouter() {
215
const router = Router()
22-
23-
// Getting all User
24-
router.get(
25-
'/',
26-
getProjects
27-
)
28-
29-
// get Project by Id
30-
router.get(
31-
'/:projectId',
32-
getProjectById
33-
)
34-
35-
// Create project
36-
router.post(
37-
'/',
38-
createProject
39-
)
40-
41-
// Update Project
42-
router.patch(
43-
'/:projectId',
44-
updateProjects
45-
)
46-
47-
// delete projects
48-
router.delete(
49-
'/:projectId',
50-
deleteProjects
51-
)
52-
53-
// getMember by ProjectId
54-
55-
router.get(
56-
'/:projectId/members',
57-
getMembersByProjectId
58-
)
59-
60-
// add member to project
61-
router.post(
62-
'/:projectId/members',
63-
addMembers
64-
)
65-
66-
// Remover the memnber
67-
router.delete(
68-
'/:projectId/members/:memberId',
69-
removeMembers
70-
)
71-
72-
// Photo upload endpoint:
73-
// router.post(
74-
// '/:projectId/photo',
75-
// upload.single('photo'),
76-
// (req, res, next) => memberCtrl.uploadPhoto(req, res, next, supabase)
77-
// )
78-
6+
7+
/**
8+
* @api {get} /progress/:memberId/completed-questions Get Completed Questions
9+
* @apiName GetCompletedQuestions
10+
* @apiGroup Progress
11+
*
12+
* @apiParam {String} memberId ID of the member to retrieve completed questions for.
13+
*
14+
* @apiSuccess {String="SUCCESS"} status Response status.
15+
* @apiSuccess {Object[]} completedQuestion List of completed questions.
16+
* @apiSuccess {Number} completedQuestion.questionId Id of the completed question.
17+
*
18+
* @apiError (Error 400) BadRequest Missing required fields.
19+
* @apiError (Error 500) InternalServerError Error while fetching completed questions.
20+
*
21+
*/
22+
router.get('/:memberId/completed-questions',getCompletedQuestion);
23+
24+
/**
25+
* @api {patch} /progress/:memberId/questions/:questionId/completed/toggle Toggle Question Completion
26+
* @apiName ToggleQuestionCompletion
27+
* @apiGroup Progress
28+
*
29+
* @apiParam {String} memberId ID of the member to toggle question completion for.
30+
* @apiParam {Number} questionId ID of the question to toggle completion for.
31+
*
32+
* @apiSuccess {String="SUCCESS"} status Response status.
33+
*
34+
* @apiError (Error 400) BadRequest Missing required fields.
35+
* @apiError (Error 500) InternalServerError Error while toggling question completion.
36+
*/
37+
router.patch('/:memeberId/questions/:questionId/completed/toggle',toggleQuestions);
7938

8039
return router
8140
}

0 commit comments

Comments
 (0)