|
1 | | -import type { Request, Response } from "express"; |
2 | | -import { ErrorResponse, SuccessResponse } from "../dtos/index.js"; |
3 | | -import { HTTP } from "@/utils/constants.js"; |
4 | | -import { projectServices } from "@/services/project.service.js"; |
5 | | -import type { CreateProjectInput, UpdateProjectInput } from "@/lib/zod/project.schema.js"; |
6 | | -import { getRepoNameFromGithubUrl } from "@/utils/github.js"; |
7 | | -import { tagServices } from "@/services/tag.service.js"; |
8 | | -import { contributorServices } from "@/services/contributor.service.js"; |
| 1 | +import type { Request, Response } from 'express'; |
| 2 | +import { ErrorResponse, SuccessResponse } from '../dtos/index.js'; |
| 3 | +import { HTTP } from '@/utils/constants.js'; |
| 4 | +import { projectServices } from '@/services/project.service.js'; |
| 5 | +import type { CreateProjectInput, UpdateProjectInput } from '@/lib/zod/project.schema.js'; |
| 6 | +import { getRepoNameFromGithubUrl } from '@/utils/github.js'; |
| 7 | +import { tagServices } from '@/services/tag.service.js'; |
| 8 | +import { contributorServices } from '@/services/contributor.service.js'; |
9 | 9 |
|
10 | 10 | class 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.addContributorsToProject( |
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(projectId, updates, thumbnail); |
103 | | - |
104 | | - if (!updateProjectResult.success || !updateProjectResult.data) { |
105 | | - return res |
106 | | - .status(HTTP.BAD_REQUEST) |
107 | | - .json( |
108 | | - ErrorResponse(HTTP.BAD_REQUEST, updateProjectResult.error || "Failed to update project") |
109 | | - ); |
110 | | - } |
111 | | - return res |
112 | | - .status(HTTP.OK) |
113 | | - .json(SuccessResponse(HTTP.OK, "Project updated successfully", updateProjectResult.data)); |
114 | | - } catch (error) { |
115 | | - console.error("Error in updating project:", error); |
116 | | - res.status(HTTP.INTERNAL).json(ErrorResponse(HTTP.INTERNAL, "Internal Server Error")); |
117 | | - } |
118 | | - } |
119 | | - |
120 | | - async deleteProject(req: Request, res: Response) { |
121 | | - try { |
122 | | - const projectId = req.params.id; |
123 | | - |
124 | | - const deleteProjectResult = await projectServices.deleteProject(projectId); |
125 | | - |
126 | | - if (!deleteProjectResult.success) { |
127 | | - return res |
128 | | - .status(HTTP.BAD_REQUEST) |
129 | | - .json( |
130 | | - ErrorResponse(HTTP.BAD_REQUEST, deleteProjectResult.error || "Failed to delete project") |
131 | | - ); |
132 | | - } |
133 | | - return res |
134 | | - .status(HTTP.OK) |
135 | | - .json(SuccessResponse(HTTP.OK, "Project deleted successfully", null)); |
136 | | - } catch (error) { |
137 | | - console.error("Error in deleting project:", error); |
138 | | - res.status(HTTP.INTERNAL).json(ErrorResponse(HTTP.INTERNAL, "Internal Server Error")); |
139 | | - } |
140 | | - } |
| 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 | + } |
141 | 145 | } |
142 | 146 |
|
143 | 147 | export const projectController = new ProjectController(); |
0 commit comments