Skip to content

Commit f368eb7

Browse files
authored
API: sorting projects in collection (#1246)
* added-saving-project-functionality-and-auto-creation-of-collection * fixed-some-bugs * added-create-collection-manually-and-fixed-the-same * added a default-collection collection to directly save if no collection_name * added-delete-projects-functionality * fixed-a-minor-issue * extracted collection and project id from request body instead of params * updated controller- removed logs * added-delete-entire-collection-functionality * added-sorting-and-like-functionality-in-collection
1 parent 65362dd commit f368eb7

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

backend/Controllers/collection.controller.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ export const deleteCollection = async(req,res)=>{
118118
try{
119119
const userID =req.user;
120120
const {collection_name} = req.body;
121+
122+
if (!collection_name) {
123+
return res.status(400).json({ error: "Collection name is required" });
124+
}
125+
121126
const existingUser = await User.findById(userID);
122127
if(!existingUser) return res.status(404).json("User not found");
123128

@@ -133,3 +138,51 @@ export const deleteCollection = async(req,res)=>{
133138
}
134139
}
135140

141+
//sorting by date and likes
142+
export const sortProject = async(req,res)=>{
143+
try {
144+
const userID = req.user;
145+
146+
if (!userID) return res.status(401).json({ message: "User not authenticated" });
147+
148+
const { sortBy } = req.query; //likes,newest,oldest
149+
150+
//for likes sorting
151+
const savedProjects = await Collection.find({ userID });
152+
const projectIds = savedProjects.map(p => p.project_id);
153+
154+
let objectIds = [];
155+
let stringIds = [];
156+
157+
savedProjects.forEach(p => {
158+
if (mongoose.Types.ObjectId.isValid(p.project_id)) objectIds.push(p.project_id);
159+
else stringIds.push(p.project_id);
160+
});
161+
162+
let query = Project.find({
163+
$or: [
164+
{ _id: { $in: objectIds } },
165+
{ project_id: { $in: stringIds } }
166+
]
167+
});
168+
169+
170+
//sorting
171+
if (sortBy === "likes") {
172+
query = query.sort({ "activity.total_likes": -1 });
173+
} else if (sortBy === "newest") {
174+
query = query.sort({ createdAt: -1 });
175+
} else if (sortBy === "oldest") {
176+
query = query.sort({ createdAt: 1 });
177+
}
178+
179+
const projects = await query.lean();
180+
181+
return res.status(200).json(projects);
182+
} catch (err) {
183+
console.error(err);
184+
return res.status(400).json(err);
185+
}
186+
}
187+
188+

backend/Routes/api/collections.routes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import express from 'express';
2-
import { createNewCollection, deleteCollection, deleteProject, saveProject} from '../../Controllers/collection.controller.js';
2+
import { createNewCollection, deleteCollection, deleteProject, saveProject, sortProject} from '../../Controllers/collection.controller.js';
33
import { authenticateUser } from '../../Middlewares/auth.middleware.js';
44
const collectionRoutes = express.Router();
55
collectionRoutes.post("/create-collection", authenticateUser, createNewCollection);
66
collectionRoutes.post("/:id", authenticateUser, saveProject);
7+
collectionRoutes.get("/sorted-project", authenticateUser, sortProject);
8+
collectionRoutes.delete("/saved-projects", authenticateUser, deleteProject);
9+
collectionRoutes.delete("/", authenticateUser, deleteCollection);
710
collectionRoutes.delete("/saved-projects", authenticateUser, deleteProject);
811
collectionRoutes.delete("/", authenticateUser, deleteCollection);
912

backend/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,4 @@
4343
},
4444
"description": ""
4545

46-
}
47-
4846
}

0 commit comments

Comments
 (0)