|
1 | 1 | import * as projectService from "../services/project.service"; |
2 | 2 | import { Request, Response } from "express"; |
3 | 3 | import { ApiError } from "../utils/apiError"; |
| 4 | +import { uploadImage } from "../utils/imageUtils"; |
| 5 | +import { supabase } from "../app"; |
| 6 | + |
4 | 7 |
|
5 | 8 | export const getProjects = async (req: Request, res: Response) => { |
6 | | - try { |
7 | | - const projects = await projectService.getPrjects(); |
8 | | - res.json(projects); |
9 | | - } catch (error) { |
10 | | - throw new ApiError("No project found !!!", 404); |
11 | | - } |
| 9 | + |
| 10 | + |
| 11 | + const projects = await projectService.getProjects(); |
| 12 | + res.status(200).json(projects); |
| 13 | + |
12 | 14 | }; |
13 | 15 |
|
| 16 | + |
14 | 17 | export const getProjectById = async (req: Request, res: Response) => { |
15 | | - try { |
16 | | - const projectId = parseInt(req.params.projectId); |
17 | | - const project = await projectService.getProjectById(projectId); |
18 | | - res.json(project); |
19 | | - } catch (error) { |
20 | | - throw new ApiError("No project with this ID", 404); |
21 | | - } |
| 18 | + |
| 19 | + |
| 20 | + const projectId = parseInt(req.params.projectId); |
| 21 | + |
| 22 | + if (isNaN(projectId)) throw new ApiError("Invalid project ID", 400); |
| 23 | + |
| 24 | + const project = await projectService.getProjectById(projectId); |
| 25 | + res.status(200).json(project); |
| 26 | + |
| 27 | + |
22 | 28 | }; |
23 | 29 |
|
24 | 30 | export const createProject = async (req: Request, res: Response) => { |
25 | | - try { |
26 | | - const projectContent = { |
27 | | - name: req.body.name, |
28 | | - imageUrl: req.body.imageUrl, |
29 | | - githubUrl: req.body.githubUrl, |
30 | | - deployUrl: req.body.deployUrl, |
31 | | - adminId: req.AdminId, |
32 | | - }; |
33 | | - |
34 | | - const project = await projectService.createProject(projectContent); |
35 | | - res.json(project); |
36 | | - } catch (error) { |
37 | | - throw new ApiError(error as string, 404); |
38 | | - } |
| 31 | + |
| 32 | + |
| 33 | + const file = req.file; |
| 34 | + if (!file) throw new ApiError('Image file not found', 400); |
| 35 | + |
| 36 | + const imageUrl = await uploadImage(supabase, file, 'projects'); |
| 37 | + |
| 38 | + if (!imageUrl) throw new ApiError("Image url is missing", 400); |
| 39 | + |
| 40 | + if (!req.body.projectData.name || !req.body.projectData.githubUrl) throw new ApiError(" fiels is missing ", 400); |
| 41 | + |
| 42 | + const projectContent = { |
| 43 | + name: req.body.projectData.name, |
| 44 | + imageUrl: imageUrl, |
| 45 | + githubUrl: req.body.projectData.githubUrl, |
| 46 | + deployUrl: req.body.deployUrl, |
| 47 | + AdminId: req.body.projectData.adminId, |
| 48 | + }; |
| 49 | + |
| 50 | + const project = await projectService.createProject(projectContent); |
| 51 | + res.status(200).json(project); |
| 52 | + |
39 | 53 | }; |
40 | 54 |
|
41 | 55 | export const updateProjects = async (req: Request, res: Response) => { |
42 | | - try { |
43 | | - const projectInfo = req.body; |
44 | | - const projectId = parseInt(req.params.projectId); |
45 | | - if (!projectId) throw new ApiError(" Send The project id ", 401); |
46 | | - |
47 | | - const project = await projectService.updateProjects(projectInfo, projectId); |
48 | | - res.json(project); |
49 | | - } catch (error) { |
50 | | - throw new ApiError(error as string, 404); |
| 56 | + |
| 57 | + |
| 58 | + const projectInfo = req.body.projectData; |
| 59 | + const projectId = parseInt(req.params.projectId); |
| 60 | + const updatedById = projectInfo.updatedById; |
| 61 | + |
| 62 | + let imageUrl = null; |
| 63 | + const file = req.file; |
| 64 | + |
| 65 | + if (file) { |
| 66 | + imageUrl = await uploadImage(supabase, file, 'projects'); |
51 | 67 | } |
52 | | -}; |
53 | 68 |
|
54 | | -export const deleteProjects = async (req: Request, res: Response) => { |
55 | | - try { |
56 | | - const projectId = parseInt(req.params.projectId); |
57 | | - if (!projectId) throw new ApiError(" Send The project id ", 401); |
58 | | - |
59 | | - const deleted = await projectService.deleteProjects(projectId); |
60 | | - res.json(deleted); |
61 | | - } catch (error) { |
62 | | - console.log(error); |
63 | | - throw new ApiError(error as string, 404); |
| 69 | + if (imageUrl) { |
| 70 | + projectInfo.imageUrl = imageUrl; |
64 | 71 | } |
65 | | -}; |
| 72 | + |
| 73 | + |
| 74 | + if (!projectId || projectInfo.length === 0 || !updatedById) throw new ApiError(" Something is Mising ", 400); |
| 75 | + |
| 76 | + const project = await projectService.updateProjects(projectInfo, projectId); |
| 77 | + res.status(200).json(project) |
| 78 | + |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +export const deleteProjects = async (req: Request, res: Response) => { |
| 84 | + |
| 85 | + |
| 86 | + const projectId = parseInt(req.params.projectId); |
| 87 | + if (!projectId) throw new ApiError(" Send The project id ", 400); |
| 88 | + |
| 89 | + const deleted = await projectService.deleteProjects(projectId); |
| 90 | + res.status(200).json(deleted) |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +} |
66 | 95 |
|
67 | 96 | export const getMembersByProjectId = async (req: Request, res: Response) => { |
68 | | - try { |
69 | | - const projectId = parseInt(req.params.ProjectId); |
70 | 97 |
|
71 | | - if (!projectId) throw new ApiError(" Project Id required !!! ", 401); |
72 | 98 |
|
73 | | - const members = await projectService.getMembersByProjectId(projectId); |
74 | | - res.json(members); |
75 | | - } catch (error) { |
76 | | - throw new ApiError(error as string, 404); |
77 | | - } |
78 | | -}; |
| 99 | + const projectId = parseInt(req.params.projectId); |
| 100 | + if (!projectId) throw new ApiError(" Project Id required !!! ", 400); |
| 101 | + |
| 102 | + const members = await projectService.getMembersByProjectId(projectId); |
| 103 | + res.status(200).json(members) |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +} |
79 | 109 |
|
80 | 110 | export const addMembers = async (req: Request, res: Response) => { |
81 | | - try { |
82 | | - const data = { |
83 | | - projectId: parseInt(req.params.projectId), |
84 | | - memberId: req.body.memberId, |
85 | | - }; |
86 | | - |
87 | | - const member = await projectService.addMembers(data); |
88 | | - res.json(member); |
89 | | - } catch (error) { |
90 | | - console.log(error); |
91 | | - throw new ApiError(error as string, 404); |
92 | | - } |
93 | | -}; |
| 111 | + |
| 112 | + |
| 113 | + const projectId = parseInt(req.params.projectId); |
| 114 | + const memberData = req.body.memberId; |
| 115 | + if (!projectId || !memberData || memberData.length === 0) throw new ApiError(" field is missing ", 400); |
| 116 | + |
| 117 | + const data = memberData.map((memberId: string) => ({ |
| 118 | + projectId, |
| 119 | + memberId |
| 120 | + })) |
| 121 | + |
| 122 | + const member = await projectService.addMembers(data); |
| 123 | + res.status(200).json(member); |
| 124 | + |
| 125 | + |
| 126 | +} |
| 127 | + |
94 | 128 |
|
95 | 129 | export const removeMembers = async (req: Request, res: Response) => { |
96 | | - try { |
97 | | - const data = { |
98 | | - projectId: parseInt(req.params.projectId), |
99 | | - memberId: req.body.memberId, |
100 | | - }; |
101 | | - |
102 | | - const removedMember = await projectService.removeMembers(data); |
103 | | - res.json(removedMember); |
104 | | - } catch (error) { |
105 | | - throw new ApiError(error as string, 404); |
| 130 | + |
| 131 | + |
| 132 | + const data = { |
| 133 | + projectId: parseInt(req.params.projectId), |
| 134 | + memberId: req.params.memberId |
106 | 135 | } |
107 | | -}; |
| 136 | + |
| 137 | + if (!data.projectId || !data.memberId) throw new ApiError(' Field is missing !!!', 400); |
| 138 | + |
| 139 | + const removedMember = await projectService.removeMembers(data); |
| 140 | + res.status(200).json(removedMember); |
| 141 | + |
| 142 | + |
| 143 | +} |
| 144 | + |
0 commit comments