Skip to content

Commit

Permalink
ADDED CONTROLLER AND ROUTES PATH TO PROVIDE ENDPOINT FOR GETTING AL T…
Browse files Browse the repository at this point in the history
…ESTIMONIALS AND DELETING SINGLE TESTIMONIAL
  • Loading branch information
Tomilola-ng committed Jul 22, 2024
1 parent 2670193 commit 6b434b8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/controllers/TestimonialsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,43 @@ export default class TestimonialsController {
res.status(500).send({ message: error.message });
}
}

// CODE BY TOMILLA OLUWAFEMI
public async getAllTestimonials(req: Request, res: Response) {
try {
const testimonials = await AppDataSource.getRepository(Testimonial).find();
res.status(200).json({
message: "Testimonials retrieved successfully",
status_code: 200,
data: testimonials,
});
} catch (error) {
res.status(500).send({ message: error.message });
}
}

public async deleteTestimonial(req: Request, res: Response) {
try {
const { testimonial_id } = req.params;

const testimonialToDelete = await AppDataSource.getRepository(Testimonial).findOne({
where: { id: testimonial_id },
});

if (!testimonialToDelete) {
return res
.status(404)
.send({ message: "Testimonial not found", status_code: 404 });
}

await AppDataSource.getRepository(Testimonial).remove(testimonialToDelete);

res.status(200).json({
message: "Testimonial deleted successfully",
status_code: 200,
});
} catch (error) {
res.status(500).send({ message: error.message });
}
}
}
12 changes: 12 additions & 0 deletions src/routes/testimonial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@ testimonialRoute.get(
testimonialController.getTestimonial.bind(testimonialController)
);

// CODE BY TOMILOLA OLUWAFEMI
testimonialRoute.get(
"/testimonials",
authMiddleware,
testimonialController.getAllTestimonials.bind(testimonialController)
);
testimonialRoute.delete(
"/testimonials/:testimonial_id",
authMiddleware,
testimonialController.deleteTestimonial.bind(testimonialController)
);

export { testimonialRoute };

0 comments on commit 6b434b8

Please sign in to comment.