Skip to content

Commit 06b2167

Browse files
Merge pull request #20 from call-0f-code/minor-changes
Minor changes
2 parents f775c32 + df481bd commit 06b2167

7 files changed

Lines changed: 228 additions & 201 deletions

File tree

src/controllers/achievement.controller.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response } from "express";
22
import * as achievementService from "../services/achievement.service";
3-
import { uploadImage } from "../utils/imageUtils";
3+
import { uploadImage, deleteImage } from "../utils/imageUtils";
44
import { supabase } from "../app";
55
import { ApiError } from "../utils/apiError";
66

@@ -80,10 +80,6 @@ export const updateAchievementById = async (req: Request, res: Response) => {
8080
const file = req.file;
8181
let imageUrl: string | undefined;
8282

83-
if (file) {
84-
imageUrl = await uploadImage(supabase, file, 'achievements');
85-
}
86-
8783
let achievementData = req.body.achievementData;
8884
if (typeof achievementData === 'string') {
8985
try {
@@ -99,25 +95,30 @@ export const updateAchievementById = async (req: Request, res: Response) => {
9995
throw new ApiError("updatedById is required", 400);
10096
}
10197

102-
if (
103-
!title &&
104-
!description &&
105-
!achievedAt &&
106-
!imageUrl &&
107-
(!Array.isArray(memberIds) || memberIds.length === 0)
108-
) {
109-
throw new ApiError("At least one field must be provided for update", 400);
110-
}
111-
98+
11299
const existingAchievement = await achievementService.getAchievementById(achievementId);
113100
if (!existingAchievement) {
114101
throw new ApiError("Achievement not found", 404);
115102
}
116-
103+
104+
if (file) {
105+
imageUrl = await uploadImage(supabase, file, 'achievements', existingAchievement.imageUrl );
106+
}
107+
117108
if (imageUrl) {
118109
achievementData.imageUrl = imageUrl;
119110
}
120-
111+
112+
if (
113+
!title &&
114+
!description &&
115+
!achievedAt &&
116+
!imageUrl &&
117+
(!Array.isArray(memberIds) || memberIds.length === 0)
118+
) {
119+
throw new ApiError("At least one field must be provided for update", 400);
120+
}
121+
121122
const updatedAchievement = await achievementService.updateAchievementById(
122123
achievementId,
123124
achievementData
@@ -141,6 +142,15 @@ export const deleteAchievementById = async (req: Request, res: Response) => {
141142
throw new ApiError("Invalid achievement ID", 400);
142143
}
143144

145+
const existingAchievement = await achievementService.getAchievementById(achievementId);
146+
if (!existingAchievement) {
147+
throw new ApiError("Achievement not found", 404);
148+
}
149+
150+
if (existingAchievement.imageUrl) {
151+
await deleteImage(supabase, existingAchievement.imageUrl);
152+
}
153+
144154
await achievementService.deleteAchievementById(achievementId);
145155

146156
res.status(200).json({

src/controllers/project.controller.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as projectService from "../services/project.service";
22
import { Request, Response } from "express";
33
import { ApiError } from "../utils/apiError";
4-
import { uploadImage } from "../utils/imageUtils";
4+
import { deleteImage, uploadImage } from "../utils/imageUtils";
55
import { supabase } from "../app";
66

77

@@ -16,20 +16,16 @@ export const getProjects = async (req: Request, res: Response) => {
1616

1717
export 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

3027
export 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

5551
export 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

src/routes/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import membersRouter from './members'
1111

1212
export default function routes(upload: Multer, supabase: SupabaseClient) {
1313
const router = Router();
14-
1514
router.use('/members', membersRouter(upload, supabase))
1615

1716
router.use('/projects', projectsRouter(upload, supabase))

0 commit comments

Comments
 (0)