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