@@ -2,6 +2,8 @@ import * as projectService from "../services/project.service";
22import { Request , Response } from "express" ;
33import { ApiError } from "../utils/apiError" ;
44import { error } from "console" ;
5+ import { uploadImage } from "../utils/imageUtils" ;
6+ import { supabase } from "../app" ;
57
68export const getProjects = async ( req : Request , res : Response ) => {
79
@@ -31,15 +33,27 @@ export const getProjectById = async ( req : Request , res : Response ) => {
3133export const createProject = async ( req : Request , res : Response ) => {
3234
3335 try {
34- const projectContent = req . body ;
35- const AdminId = req . body . AdminId ;
36- if ( projectContent . name . length === 0 || ! projectContent . githubUrl || ! AdminId || ! projectContent . imageUrl ) throw new ApiError ( " Field is missing !!!" , 400 ) ;
36+ const file = req . file ;
37+ if ( ! file ) throw new ApiError ( 'Image file is not find' , 400 ) ;
3738
38- const project = await projectService . createProject ( projectContent , AdminId ) ;
39- res . status ( 200 ) . json ( project ) ;
39+ const imageUrl = await uploadImage ( supabase , file , 'projects' ) ;
40+
41+ if ( ! imageUrl ) throw new ApiError ( "Image url is missing" , 400 ) ;
42+
43+ const projectContent = {
44+ name : req . body . projectData . project ,
45+ imageUrl : imageUrl ,
46+ githubUrl : req . body . projectData . githubUrl ,
47+ deployUrl : req . body . projectData . deployUrl ,
48+ AdminId : req . body . projectData . AdminId ,
49+ } ;
50+
51+ const project = await projectService . createProject ( projectContent ) ;
52+ res . status ( 200 ) . json ( project ) ;
4053
4154 } catch ( error ) {
42- throw new ApiError ( error as string , 500 ) ;
55+ console . log ( error ) ;
56+ throw new ApiError ( error as string , 404 ) ;
4357 }
4458} ;
4559
0 commit comments