Skip to content

Commit b2e9f3f

Browse files
authored
API: delete entire collection (#1243)
* 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
1 parent 23602c9 commit b2e9f3f

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

backend/Controllers/collection.controller.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const saveProject = async (req, res) => {
4242
if(existingProject) return res.status(400).json("Project already exists in this collection");
4343

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

6060
// 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
61-
6261
const emptySlot = await Collection.findOneAndUpdate(
6362
{ userID, collection_name, project_id: null },
6463
{ $set: { project_id } },
@@ -114,3 +113,23 @@ export const deleteProject = async(req,res)=>{
114113
}
115114
}
116115

116+
//delete an entire collection and all existing projects in it
117+
export const deleteCollection = async(req,res)=>{
118+
try{
119+
const userID =req.user;
120+
const {collection_name} = req.body;
121+
const existingUser = await User.findById(userID);
122+
if(!existingUser) return res.status(404).json("User not found");
123+
124+
await Collection.deleteMany(
125+
{
126+
userID:userID,
127+
collection_name:collection_name
128+
}
129+
);
130+
return res.status(200).json("Collection deleted successfully");
131+
}catch(err){
132+
return res.status(400).json(err);
133+
}
134+
}
135+
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import express from 'express';
2-
import { createNewCollection, deleteProject, saveProject} from '../../Controllers/collection.controller.js';
2+
import { createNewCollection, deleteCollection, deleteProject, saveProject} 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);
77
collectionRoutes.delete("/saved-projects", authenticateUser, deleteProject);
8+
collectionRoutes.delete("/", authenticateUser, deleteCollection);
89

910

1011
export default collectionRoutes;

backend/Routes/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import projectRoutes from './api/project.routes.js';
66
import notificationRoutes from './api/notification.routes.js';
77
import subscriberRoutes from './api/subscriber.routes.js';
88
import collectionRoutes from './api/collections.routes.js';
9+
910
import collaborationRoutes from './api/collaboration.routes.js';
1011
import { authMiddleware, generalMiddleware } from '../Middlewares/rateLimit/index.js';
1112

backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,7 @@
4242
"nodemon": "^3.1.10"
4343
},
4444
"description": ""
45+
4546
}
47+
4648
}

0 commit comments

Comments
 (0)