11import * as projectService from "../services/project.service" ;
22import { Request , Response } from "express" ;
33import { ApiError } from "../utils/apiError" ;
4- import { uploadImage } from "../utils/imageUtils" ;
4+ import { deleteImage , uploadImage } from "../utils/imageUtils" ;
55import { supabase } from "../app" ;
66
77
@@ -16,20 +16,16 @@ export const getProjects = async (req: Request, res: Response) => {
1616
1717export const getProjectById = async ( req : Request , res : Response ) => {
1818
19-
2019 const projectId = parseInt ( req . params . projectId ) ;
21-
2220 if ( isNaN ( projectId ) ) throw new ApiError ( "Invalid project ID" , 400 ) ;
2321
2422 const project = await projectService . getProjectById ( projectId ) ;
2523 res . status ( 200 ) . json ( project ) ;
2624
27-
2825} ;
2926
3027export const createProject = async ( req : Request , res : Response ) => {
3128
32-
3329 const file = req . file ;
3430 if ( ! file ) throw new ApiError ( 'Image file not found' , 400 ) ;
3531
@@ -43,7 +39,7 @@ export const createProject = async (req: Request, res: Response) => {
4339 name : req . body . projectData . name ,
4440 imageUrl : imageUrl ,
4541 githubUrl : req . body . projectData . githubUrl ,
46- deployUrl : req . body . deployUrl ,
42+ deployUrl : req . body . projectData . deployUrl ,
4743 AdminId : req . body . projectData . adminId ,
4844 } ;
4945
@@ -54,24 +50,27 @@ export const createProject = async (req: Request, res: Response) => {
5450
5551export const updateProjects = async ( req : Request , res : Response ) => {
5652
57-
5853 const projectInfo = req . body . projectData ;
5954 const projectId = parseInt ( req . params . projectId ) ;
6055 const updatedById = projectInfo . updatedById ;
6156
6257 let imageUrl = null ;
6358 const file = req . file ;
59+ if ( ! projectId ) throw new ApiError ( "ProjectId is missng !!!" , 401 ) ;
6460
65- if ( file ) {
66- imageUrl = await uploadImage ( supabase , file , 'projects' ) ;
61+ if ( file ) {
62+ const response = await projectService . getProjectById ( projectId ) ;
63+ const fileUlr = response ?. imageUrl ;
64+ if ( ! fileUlr ) throw new ApiError ( "File is not exits" ) ;
65+ imageUrl = await uploadImage ( supabase , file , 'projects' , fileUlr ) ;
6766 }
6867
6968 if ( imageUrl ) {
7069 projectInfo . imageUrl = imageUrl ;
7170 }
7271
7372
74- if ( ! projectId || projectInfo . length === 0 || ! updatedById ) throw new ApiError ( " Something is Mising " , 400 ) ;
73+ if ( projectInfo . length === 0 || ! updatedById ) throw new ApiError ( " Something is Mising " , 400 ) ;
7574
7675 const project = await projectService . updateProjects ( projectInfo , projectId ) ;
7776 res . status ( 200 ) . json ( project )
@@ -86,6 +85,13 @@ export const deleteProjects = async (req: Request, res: Response) => {
8685 const projectId = parseInt ( req . params . projectId ) ;
8786 if ( ! projectId ) throw new ApiError ( " Send The project id " , 400 ) ;
8887
88+ const response = await projectService . getProjectById ( projectId ) ;
89+ const fileUrl = response ?. imageUrl ;
90+
91+ if ( fileUrl ) {
92+ await deleteImage ( supabase , fileUrl ) ;
93+ }
94+
8995 const deleted = await projectService . deleteProjects ( projectId ) ;
9096 res . status ( 200 ) . json ( deleted )
9197
0 commit comments