Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions backend/Controllers/collection.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const saveProject = async (req, res) => {
if(existingProject) return res.status(400).json("Project already exists in this collection");

// Case 1: No collection exists → save in 'default-collection' and save the project
if (existingCollection.length === 0) {
if (!existingCollection) {
const newCollection = new Collection(
{
userID,
Expand All @@ -58,7 +58,6 @@ export const saveProject = async (req, res) => {
}

// Case 2: Try to update empty project_id - in case of manula creation,we had project_is null, so here we try to update that id for that document, to use this document and avoid redudancy in the collection

const emptySlot = await Collection.findOneAndUpdate(
{ userID, collection_name, project_id: null },
{ $set: { project_id } },
Expand Down Expand Up @@ -114,3 +113,23 @@ export const deleteProject = async(req,res)=>{
}
}

//delete an entire collection and all existing projects in it
export const deleteCollection = async(req,res)=>{
try{
const userID =req.user;
const {collection_name} = req.body;
const existingUser = await User.findById(userID);
if(!existingUser) return res.status(404).json("User not found");

await Collection.deleteMany(
{
userID:userID,
collection_name:collection_name
}
);
return res.status(200).json("Collection deleted successfully");
}catch(err){
return res.status(400).json(err);
}
}

3 changes: 2 additions & 1 deletion backend/Routes/api/collections.routes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import express from 'express';
import { createNewCollection, deleteProject, saveProject} from '../../Controllers/collection.controller.js';
import { createNewCollection, deleteCollection, deleteProject, saveProject} from '../../Controllers/collection.controller.js';
import { authenticateUser } from '../../Middlewares/auth.middleware.js';
const collectionRoutes = express.Router();
collectionRoutes.post("/create-collection", authenticateUser, createNewCollection);
collectionRoutes.post("/:id", authenticateUser, saveProject);
collectionRoutes.delete("/saved-projects", authenticateUser, deleteProject);
collectionRoutes.delete("/", authenticateUser, deleteCollection);


export default collectionRoutes;
1 change: 1 addition & 0 deletions backend/Routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import projectRoutes from './api/project.routes.js';
import notificationRoutes from './api/notification.routes.js';
import subscriberRoutes from './api/subscriber.routes.js';
import collectionRoutes from './api/collections.routes.js';

import collaborationRoutes from './api/collaboration.routes.js';
import { authMiddleware, generalMiddleware } from '../Middlewares/rateLimit/index.js';

Expand Down
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@
"nodemon": "^3.1.10"
},
"description": ""

}

}