Skip to content

Commit 8436296

Browse files
authored
Merge pull request #65 from Aayush0966/project-image
feat (image): add image upload functionality
2 parents 2b873d7 + 0cde6a5 commit 8436296

4 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/controllers/project.controller.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { CreateProjectInput, UpdateProjectInput } from '@/lib/zod/project.s
66
import { getRepoNameFromGithubUrl } from '@/utils/github.js';
77
import { tagServices } from '@/services/tag.service.js';
88
import { contributorServices } from '@/services/contributor.service.js';
9+
import { uploadImageToCloudinary } from '@/utils/cloudinary.uploader.js';
910

1011
class ProjectController {
1112
async getAllProjects(req: Request, res: Response) {
@@ -159,6 +160,33 @@ class ProjectController {
159160
res.status(HTTP.INTERNAL).json(ErrorResponse(HTTP.INTERNAL, 'Internal Server Error'));
160161
}
161162
}
163+
164+
async uploadImage(req: Request, res: Response) {
165+
try {
166+
if (!req.file) return res
167+
.status(HTTP.BAD_REQUEST)
168+
.json(
169+
ErrorResponse(HTTP.BAD_REQUEST, "Image is required")
170+
)
171+
const image = req.file;
172+
173+
const uploadResult = await uploadImageToCloudinary(image!.path, {folder: "projects"})
174+
if (!uploadResult.success) {
175+
return res
176+
.status(HTTP.BAD_REQUEST)
177+
.json(
178+
ErrorResponse(HTTP.BAD_REQUEST, uploadResult.error|| 'Failed to upload image.')
179+
);
180+
}
181+
return res
182+
.status(HTTP.OK)
183+
.json(
184+
SuccessResponse(HTTP.OK,"Uploaded successfully", uploadResult.url)
185+
);
186+
} catch (error) {
187+
console.log("Error whole uploading image: ", error)
188+
}
189+
}
162190
}
163191

164192
export const projectController = new ProjectController();

src/middleware/auth.middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const isModerator = async (req: Request, res: Response, next: NextFunctio
5757
const session = await auth.api.getSession({ headers });
5858
if (session) {
5959
const user = session.userWithRole;
60-
if (user?.role === "MODERATOR") {
60+
if (user?.role === "MODERATOR" || user?.role === "ADMIN") {
6161
next();
6262
} else {
6363
return res.status(HTTP.UNAUTHORIZED).json(ErrorResponse(HTTP.UNAUTHORIZED, 'Unauthorized'));

src/routers/project.router.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ projectRouter.post(
2525
projectController.addProject
2626
);
2727

28+
projectRouter.post(
29+
'/upload',
30+
upload.single('image'),
31+
projectController.uploadImage
32+
)
2833
projectRouter.patch(
2934
'/:id',
3035
upload.single('thumbnail'),

src/services/project.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ class ProjectServices {
222222
return { success: false, error };
223223
}
224224
}
225+
225226
}
226227

227228
export const projectServices = new ProjectServices();

0 commit comments

Comments
 (0)