Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
taugk committed Dec 14, 2023
1 parent 99085e5 commit 82b668b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ app.use(
app.use(cors());

app.use("/auth", authRoutes);
app.use("/", postsRoutes);
app.use(postsRoutes);
app.use("/categories", CategoriesRoutes);

app.listen(port, () => {
Expand Down
69 changes: 48 additions & 21 deletions routes/postsRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const geolib = require("geolib");
const { invalidatedTokens } = require("./auth");

//Get Routes posts
router.get("posts/", async (req, res) => {
router.get("/", async (req, res) => {
try {
const { page = 1, limit = 10, lat, lon, search } = req.query;
const offset = (page - 1) * limit;
Expand Down Expand Up @@ -69,17 +69,17 @@ router.get("posts/", async (req, res) => {
}
});

router.get("/", async (req, res) => {
try {
const allPosts = await Post.findAll();
res.status(200).json({ posts: allPosts });
} catch (error) {
console.error("Error fetching all posts", error);
res.status(500).json({ message: "Internal server error" });
}
});

router.get("latest/", async (req, res) => {
// router.get("/", async (req, res) => {
// try {
// const allPosts = await Post.findAll();
// res.status(200).json({ posts: allPosts });
// } catch (error) {
// console.error("Error fetching all posts", error);
// res.status(500).json({ message: "Internal server error" });
// }
// });

router.get("/latest", async (req, res) => {
try {
const latestPosts = await Post.findAll({
order: [["createdAt", "DESC"]],
Expand All @@ -91,7 +91,7 @@ router.get("latest/", async (req, res) => {
}
});

router.get("posts/:id", async (req, res) => {
router.get("/posts/:id", async (req, res) => {
try {
const post = await Post.findByPk(req.params.id, {
include: [
Expand Down Expand Up @@ -124,7 +124,7 @@ router.get("posts/:id", async (req, res) => {
}
});

router.get("Category/:categoryId", async (req, res) => {
router.get("/Category/:categoryId", async (req, res) => {
try {
const id_cat = req.params.categoryId;
const postsByCategory = await Post.findAll({
Expand All @@ -139,7 +139,34 @@ router.get("Category/:categoryId", async (req, res) => {
}
});

router.get("posts/recommendation/nearby", async (req, res) => {
router.get("/filterByTitle", async (req, res) => {
try {
const titleFilter = req.query.title;
if (!titleFilter) {
return res.status(400).json({ message: "Missing title parameter" });
}
const filteredPosts = await Post.findAll({
where: {
title: {
[Op.iLike]: `%${titleFilter}%`,
},
},
});

if (filteredPosts.length === 0) {
return res
.status(404)
.json({ message: "No posts found with the specified title" });
}

res.status(200).json({ posts: filteredPosts });
} catch (error) {
console.error("Error fetching posts by title", error);
res.status(500).json({ message: "Internal server error" });
}
});

router.get("/recommendation/nearby", async (req, res) => {
try {
const { lat, lon } = req.query;
if (!lat || !lon) {
Expand Down Expand Up @@ -169,7 +196,7 @@ router.get("posts/recommendation/nearby", async (req, res) => {
}
});

router.get("posts/recommendation/restaurant", async (req, res) => {
router.get("/recommendation/restaurant", async (req, res) => {
try {
const { lat, lon } = req.query;

Expand Down Expand Up @@ -225,7 +252,7 @@ router.get("posts/recommendation/restaurant", async (req, res) => {
}
});

router.get("posts/recommendation/homefood", async (req, res) => {
router.get("/recommendation/homefood", async (req, res) => {
try {
const { lat, lon } = req.query;
if (!lat || !lon) {
Expand Down Expand Up @@ -276,7 +303,7 @@ router.get("posts/recommendation/homefood", async (req, res) => {
}
});

router.get("posts/recommendation/rawIngredients", async (req, res) => {
router.get("/recommendation/rawIngredients", async (req, res) => {
try {
const { lat, lon } = req.query;
if (!lat || !lon) {
Expand Down Expand Up @@ -329,7 +356,7 @@ router.get("posts/recommendation/rawIngredients", async (req, res) => {

//Post Routes
router.post(
"posts/food",
"/food",
ImgUpload.uploadToGcs,
ImgUpload.handleUpload,
async (req, res) => {
Expand Down Expand Up @@ -417,7 +444,7 @@ router.post(

//Put Routes
router.put(
"posts/Update/:id",
"/postsUpdate/:id",
ImgUpload.uploadToGcs,
ImgUpload.handleUpload,
async (req, res) => {
Expand Down Expand Up @@ -498,7 +525,7 @@ router.put(
);

//Delete Routes
router.delete("posts/delete/:id", async (req, res) => {
router.delete("/deletePost/:id", async (req, res) => {
try {
const headerAuth = req.headers["authorization"];
const token = headerAuth && headerAuth.split(" ")[1];
Expand Down

0 comments on commit 82b668b

Please sign in to comment.