11import type { Request , Response } from 'express' ;
2- import { ErrorResponse , SuccessResponse } from '../dtos/index.js' ;
2+ import { ErrorResponse , PaginationResponse , SuccessResponse } from '../dtos/index.js' ;
33import { HTTP } from '@/utils/constants.js' ;
44import { projectServices } from '@/services/project.service.js' ;
55import type { CreateProjectInput , UpdateProjectInput } from '@/lib/zod/project.schema.js' ;
@@ -8,140 +8,157 @@ import { tagServices } from '@/services/tag.service.js';
88import { contributorServices } from '@/services/contributor.service.js' ;
99
1010class ProjectController {
11- async getAllProjects ( req : Request , res : Response ) {
12- try {
13- const projectResult = await projectServices . getAllProjects ( ) ;
14-
15- if ( ! projectResult . success || ! projectResult . data ) {
16- return res
17- . status ( HTTP . BAD_REQUEST )
18- . json ( ErrorResponse ( HTTP . BAD_REQUEST , projectResult . error ) ) ;
19- }
20-
21- res . status ( HTTP . OK ) . json ( SuccessResponse ( HTTP . OK , 'Fetched Sucessfully' , projectResult ) ) ;
22- } catch ( error ) {
23- console . error ( 'Error in getAllProjects controller:' , error ) ;
24- res . status ( HTTP . INTERNAL ) . json ( ErrorResponse ( HTTP . INTERNAL , 'Internal Server Error' ) ) ;
25- }
26- }
27-
28- async addProject ( req : Request , res : Response ) {
29- try {
30- const { name, githubLink, demoLink, description, techStacks, tagIds } : CreateProjectInput =
31- req . body ;
32- const thumbnail = req . file ;
33- let repoName = getRepoNameFromGithubUrl ( githubLink ) ;
34-
35- const addProjectResult = await projectServices . createProject (
36- {
37- name,
38- githubLink,
39- demoLink,
40- description,
41- techStacks,
42- tagIds,
43- } ,
44- thumbnail
45- ) ;
46-
47- if ( ! addProjectResult . success || ! addProjectResult . data ) {
48- return res
49- . status ( HTTP . BAD_REQUEST )
50- . json (
51- ErrorResponse (
52- HTTP . BAD_REQUEST ,
53- typeof addProjectResult . error === 'string'
54- ? addProjectResult . error
55- : 'Failed to add project'
56- )
57- ) ;
58- }
59-
60- // If tagIds are provided, associate them with the project
61- const projectId = addProjectResult . data . id ;
62-
63- if ( tagIds && tagIds . length > 0 ) {
64- const tagAssociationResult = await tagServices . associateTagToProject ( projectId , tagIds ) ;
65- if ( ! tagAssociationResult . success || ! tagAssociationResult . data ) {
66- console . warn (
67- 'Some tags failed to associate with the project:' ,
68- tagAssociationResult . error
69- ) ;
70- return res
71- . status ( HTTP . BAD_REQUEST )
72- . json ( ErrorResponse ( HTTP . BAD_REQUEST , 'Failed to associate tags' ) ) ;
73- }
74- }
75-
76- // If repoName is provided add contributors from that repo
77- if ( repoName && addProjectResult . data . id ) {
78- const contributorResult = await contributorServices . addGithubContributorsToProject (
79- repoName ,
80- addProjectResult . data . id
81- ) ;
82- if ( ! contributorResult . success ) {
83- console . warn ( 'Some contributors failed to process:' , contributorResult . error ) ;
84- }
85- }
86-
87- return res
88- . status ( HTTP . OK )
89- . json ( SuccessResponse ( HTTP . OK , 'Project added successfully' , addProjectResult . data ) ) ;
90- } catch ( error ) {
91- console . error ( 'Error in addProject controller:' , error ) ;
92- return res . status ( HTTP . INTERNAL ) . json ( ErrorResponse ( HTTP . INTERNAL , 'Internal Server Error' ) ) ;
93- }
94- }
95-
96- async updateProject ( req : Request , res : Response ) {
97- try {
98- const projectId = req . params . id ;
99- const updates : UpdateProjectInput = req . body ;
100- const thumbnail = req . file ;
101-
102- const updateProjectResult = await projectServices . updateProject (
103- projectId ,
104- updates ,
105- thumbnail
106- ) ;
107-
108- if ( ! updateProjectResult . success || ! updateProjectResult . data ) {
109- return res
110- . status ( HTTP . BAD_REQUEST )
111- . json (
112- ErrorResponse ( HTTP . BAD_REQUEST , updateProjectResult . error || 'Failed to update project' )
113- ) ;
114- }
115- return res
116- . status ( HTTP . OK )
117- . json ( SuccessResponse ( HTTP . OK , 'Project updated successfully' , updateProjectResult . data ) ) ;
118- } catch ( error ) {
119- console . error ( 'Error in updating project:' , error ) ;
120- res . status ( HTTP . INTERNAL ) . json ( ErrorResponse ( HTTP . INTERNAL , 'Internal Server Error' ) ) ;
121- }
122- }
123-
124- async deleteProject ( req : Request , res : Response ) {
125- try {
126- const projectId = req . params . id ;
127-
128- const deleteProjectResult = await projectServices . deleteProject ( projectId ) ;
129-
130- if ( ! deleteProjectResult . success ) {
131- return res
132- . status ( HTTP . BAD_REQUEST )
133- . json (
134- ErrorResponse ( HTTP . BAD_REQUEST , deleteProjectResult . error || 'Failed to delete project' )
135- ) ;
136- }
137- return res
138- . status ( HTTP . OK )
139- . json ( SuccessResponse ( HTTP . OK , 'Project deleted successfully' , null ) ) ;
140- } catch ( error ) {
141- console . error ( 'Error in deleting project:' , error ) ;
142- res . status ( HTTP . INTERNAL ) . json ( ErrorResponse ( HTTP . INTERNAL , 'Internal Server Error' ) ) ;
143- }
144- }
11+ async getAllProjects ( req : Request , res : Response ) {
12+ try {
13+ const query = ( req as any ) . validatedQuery ;
14+ const page = query . page ;
15+ const limit = query . limit ;
16+ const skip = ( page - 1 ) * limit ;
17+
18+ const projectResult = await projectServices . getAllProjects ( { skip, limit } ) ;
19+
20+ if ( ! projectResult . success || ! projectResult . data ) {
21+ return res
22+ . status ( HTTP . BAD_REQUEST )
23+ . json ( ErrorResponse ( HTTP . BAD_REQUEST , projectResult . error ) ) ;
24+ }
25+
26+ const { mappedProjects, total } = projectResult . data ;
27+ res
28+ . status ( HTTP . OK )
29+ . json (
30+ PaginationResponse (
31+ HTTP . OK ,
32+ 'Projects fetched successfully' ,
33+ mappedProjects ,
34+ total ,
35+ page ,
36+ limit
37+ )
38+ ) ;
39+ } catch ( error ) {
40+ console . error ( 'Error in getAllProjects controller:' , error ) ;
41+ res . status ( HTTP . INTERNAL ) . json ( ErrorResponse ( HTTP . INTERNAL , 'Internal Server Error' ) ) ;
42+ }
43+ }
44+
45+ async addProject ( req : Request , res : Response ) {
46+ try {
47+ const { name, githubLink, demoLink, description, techStacks, tagIds } : CreateProjectInput =
48+ req . body ;
49+ const thumbnail = req . file ;
50+ let repoName = getRepoNameFromGithubUrl ( githubLink ) ;
51+
52+ const addProjectResult = await projectServices . createProject (
53+ {
54+ name,
55+ githubLink,
56+ demoLink,
57+ description,
58+ techStacks,
59+ tagIds,
60+ } ,
61+ thumbnail
62+ ) ;
63+
64+ if ( ! addProjectResult . success || ! addProjectResult . data ) {
65+ return res
66+ . status ( HTTP . BAD_REQUEST )
67+ . json (
68+ ErrorResponse (
69+ HTTP . BAD_REQUEST ,
70+ typeof addProjectResult . error === 'string'
71+ ? addProjectResult . error
72+ : 'Failed to add project'
73+ )
74+ ) ;
75+ }
76+
77+ // If tagIds are provided, associate them with the project
78+ const projectId = addProjectResult . data . id ;
79+
80+ if ( tagIds && tagIds . length > 0 ) {
81+ const tagAssociationResult = await tagServices . associateTagToProject ( projectId , tagIds ) ;
82+ if ( ! tagAssociationResult . success || ! tagAssociationResult . data ) {
83+ console . warn (
84+ 'Some tags failed to associate with the project:' ,
85+ tagAssociationResult . error
86+ ) ;
87+ return res
88+ . status ( HTTP . BAD_REQUEST )
89+ . json ( ErrorResponse ( HTTP . BAD_REQUEST , 'Failed to associate tags' ) ) ;
90+ }
91+ }
92+
93+ // If repoName is provided add contributors from that repo
94+ if ( repoName && addProjectResult . data . id ) {
95+ const contributorResult = await contributorServices . addGithubContributorsToProject (
96+ repoName ,
97+ addProjectResult . data . id
98+ ) ;
99+ if ( ! contributorResult . success ) {
100+ console . warn ( 'Some contributors failed to process:' , contributorResult . error ) ;
101+ }
102+ }
103+
104+ return res
105+ . status ( HTTP . OK )
106+ . json ( SuccessResponse ( HTTP . OK , 'Project added successfully' , addProjectResult . data ) ) ;
107+ } catch ( error ) {
108+ console . error ( 'Error in addProject controller:' , error ) ;
109+ return res . status ( HTTP . INTERNAL ) . json ( ErrorResponse ( HTTP . INTERNAL , 'Internal Server Error' ) ) ;
110+ }
111+ }
112+
113+ async updateProject ( req : Request , res : Response ) {
114+ try {
115+ const projectId = req . params . id ;
116+ const updates : UpdateProjectInput = req . body ;
117+ const thumbnail = req . file ;
118+
119+ const updateProjectResult = await projectServices . updateProject (
120+ projectId ,
121+ updates ,
122+ thumbnail
123+ ) ;
124+
125+ if ( ! updateProjectResult . success || ! updateProjectResult . data ) {
126+ return res
127+ . status ( HTTP . BAD_REQUEST )
128+ . json (
129+ ErrorResponse ( HTTP . BAD_REQUEST , updateProjectResult . error || 'Failed to update project' )
130+ ) ;
131+ }
132+ return res
133+ . status ( HTTP . OK )
134+ . json ( SuccessResponse ( HTTP . OK , 'Project updated successfully' , updateProjectResult . data ) ) ;
135+ } catch ( error ) {
136+ console . error ( 'Error in updating project:' , error ) ;
137+ res . status ( HTTP . INTERNAL ) . json ( ErrorResponse ( HTTP . INTERNAL , 'Internal Server Error' ) ) ;
138+ }
139+ }
140+
141+ async deleteProject ( req : Request , res : Response ) {
142+ try {
143+ const projectId = req . params . id ;
144+
145+ const deleteProjectResult = await projectServices . deleteProject ( projectId ) ;
146+
147+ if ( ! deleteProjectResult . success ) {
148+ return res
149+ . status ( HTTP . BAD_REQUEST )
150+ . json (
151+ ErrorResponse ( HTTP . BAD_REQUEST , deleteProjectResult . error || 'Failed to delete project' )
152+ ) ;
153+ }
154+ return res
155+ . status ( HTTP . OK )
156+ . json ( SuccessResponse ( HTTP . OK , 'Project deleted successfully' , null ) ) ;
157+ } catch ( error ) {
158+ console . error ( 'Error in deleting project:' , error ) ;
159+ res . status ( HTTP . INTERNAL ) . json ( ErrorResponse ( HTTP . INTERNAL , 'Internal Server Error' ) ) ;
160+ }
161+ }
145162}
146163
147164export const projectController = new ProjectController ( ) ;
0 commit comments