From 7e59c15ae59812ee4fcde8a23ecd665d392301c3 Mon Sep 17 00:00:00 2001 From: vriti62 Date: Sun, 24 Aug 2025 00:49:22 +0530 Subject: [PATCH 1/9] added-saving-project-functionality-and-auto-creation-of-collection --- backend/Controllers/collection.controller.js | 73 ++++++++++++++++++++ backend/Models/collection.model.js | 6 ++ backend/Routes/api/collections.routes.js | 8 +++ backend/Routes/index.js | 3 +- backend/Schemas/collection.schema.js | 19 +++++ backend/package-lock.json | 8 +-- backend/package.json | 6 +- 7 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 backend/Controllers/collection.controller.js create mode 100644 backend/Models/collection.model.js create mode 100644 backend/Routes/api/collections.routes.js create mode 100644 backend/Schemas/collection.schema.js diff --git a/backend/Controllers/collection.controller.js b/backend/Controllers/collection.controller.js new file mode 100644 index 000000000..a522ec00c --- /dev/null +++ b/backend/Controllers/collection.controller.js @@ -0,0 +1,73 @@ +import Collection from "../Models/collection.model.js"; +import User from "../Models/user.model.js"; + +//create a new collection +export const createNewCollection = async(req,res)=>{ + try{ + const userID = req.user; + const {collection_name}=req.body; + const existingUser = await User.findOne({_id:userID}); + if(!existingUser) return res.status(404).json("User not found"); + + const newCollection = new Collection({ + userID, + collection_name + }) + + await newCollection.save(); + return res.status(200).json(`Collection ${newCollection.collection_name} for ${existingUser.personal_info.username}`); + }catch(err){ + console.log(err); + return res.status(400).json(err); + } +} + +//save projects to user profile in existing collection +export const saveProject = async (req, res) => { + try { + const userID = req.user; + const project_id = req.params.id; + const { collection_name } = req.body; + + const existingUser = await User.findById(userID); + if (!existingUser) return res.status(404).json("User not found"); + + const existingCollection = await Collection.find({ userID, collection_name }); + + // Case 1: No collection exists → create new one + if (existingCollection.length === 0) { + const newCollection = new Collection({ userID, collection_name, project_id }); + await newCollection.save(); + + return res.status(201).json( + `New collection '${collection_name}' created and project saved for ${existingUser.personal_info.username}` + ); + } + + // Case 2: Try to update empty slot + const emptySlot = await Collection.findOneAndUpdate( + { userID, collection_name, project_id: null }, + { $set: { project_id } }, + { new: true } + ); + + if (emptySlot) { + return res.status(200).json( + `Project added to empty slot in collection '${collection_name}' for ${existingUser.personal_info.username}` + ); + } + + // Case 3: No empty slots → create new document + const newDoc = new Collection({ userID, collection_name, project_id }); + await newDoc.save(); + + + return res.status(201).json( + `New document created in collection '${collection_name}' with project for ${existingUser.personal_info.username}` + ); + + } catch (err) { + console.log(err); + return res.status(400).json(err); + } +}; diff --git a/backend/Models/collection.model.js b/backend/Models/collection.model.js new file mode 100644 index 000000000..b7b81301d --- /dev/null +++ b/backend/Models/collection.model.js @@ -0,0 +1,6 @@ +import mongoose from 'mongoose' +import collectionSchema from "../Schemas/collection.schema.js"; + +const Collection = mongoose.model("Collection", collectionSchema); + +export default Collection; diff --git a/backend/Routes/api/collections.routes.js b/backend/Routes/api/collections.routes.js new file mode 100644 index 000000000..4a680c87b --- /dev/null +++ b/backend/Routes/api/collections.routes.js @@ -0,0 +1,8 @@ +import express from 'express'; +import { createNewCollection, saveProject} from '../../Controllers/collection.controller.js'; +import { authenticateUser } from '../../Middlewares/auth.middleware.js'; +const collection = express.Router(); +collection.post("/:id", authenticateUser, saveProject); +collection.post("/create-collection", authenticateUser, createNewCollection); + +export default collection; \ No newline at end of file diff --git a/backend/Routes/index.js b/backend/Routes/index.js index c80c7b7f2..8ccee314f 100644 --- a/backend/Routes/index.js +++ b/backend/Routes/index.js @@ -5,6 +5,7 @@ import projectRoutes from './api/project.routes.js'; import userRoutes from './api/user.routes.js'; import notificationRoutes from './api/notification.routes.js'; import subscriberRoutes from './api/subscriber.routes.js'; +import collectionRoutes from './api/collections.routes.js'; const router = express.Router(); @@ -14,5 +15,5 @@ router.use('/media', mediaRoutes); router.use('/project', projectRoutes); router.use('/notification', notificationRoutes); router.use('/subscriber', subscriberRoutes); - +router.use("/collection", collectionRoutes); export default router; diff --git a/backend/Schemas/collection.schema.js b/backend/Schemas/collection.schema.js new file mode 100644 index 000000000..4dff39564 --- /dev/null +++ b/backend/Schemas/collection.schema.js @@ -0,0 +1,19 @@ +import { Schema } from "mongoose"; +const collectionSchema = Schema({ + userID:{ + type:Schema.Types.ObjectId, + required:true, + ref:'User' + }, + collection_name:{ + type:String, + required:true + }, + project_id:{ + type:String, + default:null, + ref:'Project' + } +},{timestamps:true}); + +export default collectionSchema; \ No newline at end of file diff --git a/backend/package-lock.json b/backend/package-lock.json index 7a36baeb5..16b7077ef 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -21,7 +21,7 @@ "nanoid": "^5.1.2" }, "devDependencies": { - "nodemon": "^3.1.9" + "nodemon": "^3.1.10" } }, "node_modules/@fastify/busboy": { @@ -2754,9 +2754,9 @@ } }, "node_modules/nodemon": { - "version": "3.1.9", - "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.9.tgz", - "integrity": "sha512-hdr1oIb2p6ZSxu3PB2JWWYS7ZQ0qvaZsc3hK8DR8f02kRzc8rjYmxAIvdz+aYC+8F2IjNaB7HMcSDg8nQpJxyg==", + "version": "3.1.10", + "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.10.tgz", + "integrity": "sha512-WDjw3pJ0/0jMFmyNDp3gvY2YizjLmmOUQo6DEBY+JgdvW/yQ9mEeSw6H5ythl5Ny2ytb7f9C2nIbjSxMNzbJXw==", "dev": true, "license": "MIT", "dependencies": { diff --git a/backend/package.json b/backend/package.json index ff0c4b6ab..07dad9a9e 100644 --- a/backend/package.json +++ b/backend/package.json @@ -11,7 +11,6 @@ "keywords": [], "author": "Avdhesh Varshney", "license": "ISC", - "description": "", "dependencies": { "bcrypt": "^5.1.1", "cloudinary": "^2.5.1", @@ -25,6 +24,7 @@ "nanoid": "^5.1.2" }, "devDependencies": { - "nodemon": "^3.1.9" - } + "nodemon": "^3.1.10" + }, + "description": "" } From a6c7303bf0a3c71dacf27ab23ab77bf9045dc20c Mon Sep 17 00:00:00 2001 From: vriti62 Date: Sun, 24 Aug 2025 01:24:47 +0530 Subject: [PATCH 2/9] fixed-some-bugs --- backend/Controllers/collection.controller.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/Controllers/collection.controller.js b/backend/Controllers/collection.controller.js index a522ec00c..565e1a191 100644 --- a/backend/Controllers/collection.controller.js +++ b/backend/Controllers/collection.controller.js @@ -33,6 +33,9 @@ export const saveProject = async (req, res) => { if (!existingUser) return res.status(404).json("User not found"); const existingCollection = await Collection.find({ userID, collection_name }); + + const existingProject = await Collection.findOne({userID,collection_name,project_id}); + if(existingProject) return res.status(400).json("Project already exists in this collection"); // Case 1: No collection exists → create new one if (existingCollection.length === 0) { @@ -69,5 +72,6 @@ export const saveProject = async (req, res) => { } catch (err) { console.log(err); return res.status(400).json(err); - } -}; + } +} + From 8a20748aa9c6c1ea9517eefe2875b1a0bedd16a8 Mon Sep 17 00:00:00 2001 From: vriti62 Date: Sun, 24 Aug 2025 01:55:53 +0530 Subject: [PATCH 3/9] added-create-collection-manually-and-fixed-the-same --- backend/Routes/api/collections.routes.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/Routes/api/collections.routes.js b/backend/Routes/api/collections.routes.js index 4a680c87b..16f6c58ef 100644 --- a/backend/Routes/api/collections.routes.js +++ b/backend/Routes/api/collections.routes.js @@ -2,7 +2,8 @@ import express from 'express'; import { createNewCollection, saveProject} from '../../Controllers/collection.controller.js'; import { authenticateUser } from '../../Middlewares/auth.middleware.js'; const collection = express.Router(); -collection.post("/:id", authenticateUser, saveProject); collection.post("/create-collection", authenticateUser, createNewCollection); +collection.post("/:id", authenticateUser, saveProject); + export default collection; \ No newline at end of file From b9ffc6276baf5bca1b488f7dedafda22d575969d Mon Sep 17 00:00:00 2001 From: vriti62 Date: Mon, 25 Aug 2025 23:29:46 +0530 Subject: [PATCH 4/9] added a default-collection collection to directly save if no collection_name --- backend/Controllers/collection.controller.js | 27 +++++++++++++++----- backend/Routes/api/collections.routes.js | 8 +++--- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/backend/Controllers/collection.controller.js b/backend/Controllers/collection.controller.js index 565e1a191..150990cc4 100644 --- a/backend/Controllers/collection.controller.js +++ b/backend/Controllers/collection.controller.js @@ -1,7 +1,9 @@ import Collection from "../Models/collection.model.js"; import User from "../Models/user.model.js"; -//create a new collection +/*create a new collection- + 1.in case of manula creation of collection, the project_id is set to null since nothing is saved */ + export const createNewCollection = async(req,res)=>{ try{ const userID = req.user; @@ -37,17 +39,23 @@ export const saveProject = async (req, res) => { const existingProject = await Collection.findOne({userID,collection_name,project_id}); if(existingProject) return res.status(400).json("Project already exists in this collection"); - // Case 1: No collection exists → create new one + // Case 1: No collection exists → save in 'default-collection' and save the project if (existingCollection.length === 0) { - const newCollection = new Collection({ userID, collection_name, project_id }); + const newCollection = new Collection( + { + userID, + collection_name:"default-collection", + project_id + } + ); await newCollection.save(); return res.status(201).json( - `New collection '${collection_name}' created and project saved for ${existingUser.personal_info.username}` + `Collection added to default collection and project saved for ${existingUser.personal_info.username}` ); } - // Case 2: Try to update empty slot + // 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 } }, @@ -60,8 +68,13 @@ export const saveProject = async (req, res) => { ); } - // Case 3: No empty slots → create new document - const newDoc = new Collection({ userID, collection_name, project_id }); + // Case 3: No empty slots → create new document for the project being saved + const newDoc = new Collection( + { + userID, + collection_name, + project_id + }); await newDoc.save(); diff --git a/backend/Routes/api/collections.routes.js b/backend/Routes/api/collections.routes.js index 16f6c58ef..dcae4418d 100644 --- a/backend/Routes/api/collections.routes.js +++ b/backend/Routes/api/collections.routes.js @@ -1,9 +1,9 @@ import express from 'express'; import { createNewCollection, saveProject} from '../../Controllers/collection.controller.js'; import { authenticateUser } from '../../Middlewares/auth.middleware.js'; -const collection = express.Router(); -collection.post("/create-collection", authenticateUser, createNewCollection); -collection.post("/:id", authenticateUser, saveProject); +const collectionRoutes = express.Router(); +collectionRoutes.post("/create-collection", authenticateUser, createNewCollection); +collectionRoutes.post("/:id", authenticateUser, saveProject); -export default collection; \ No newline at end of file +export default collectionRoutes; \ No newline at end of file From 37a315011941401a9eaa2ff8fc53225cd22f8419 Mon Sep 17 00:00:00 2001 From: vriti62 Date: Fri, 29 Aug 2025 10:30:25 +0530 Subject: [PATCH 5/9] added-delete-projects-functionality --- backend/Controllers/collection.controller.js | 25 ++++++++++++++++++++ backend/Routes/api/collections.routes.js | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/backend/Controllers/collection.controller.js b/backend/Controllers/collection.controller.js index 150990cc4..67fc3d5d7 100644 --- a/backend/Controllers/collection.controller.js +++ b/backend/Controllers/collection.controller.js @@ -1,5 +1,7 @@ import Collection from "../Models/collection.model.js"; +import Project from "../Models/project.model.js"; import User from "../Models/user.model.js"; +import mongoose from "mongoose"; /*create a new collection- 1.in case of manula creation of collection, the project_id is set to null since nothing is saved */ @@ -88,3 +90,26 @@ export const saveProject = async (req, res) => { } } +export const deleteProject = async(req,res)=>{ + try{ + const userID = req.user; + const collectionID = req.params.cid; + const projectID = req.params.project_id; + console.log(userID , projectID, collectionID); + const existingUser = await User.findById(userID); + if(!existingUser) return res.status(404).json("User not found"); + const deletedProject = await Collection.findOne({ + userID:userID, + _id:collectionID, + project_id:projectID + }) + + if(!deletedProject) return res.status(404).json("Project not found in this collection"); + + await Collection.deleteOne(deletedProject); + return res.status(200).json("Project deleted successfully"); + }catch(err){ + console.log(err); + return res.status(400).json(err); + } +} \ No newline at end of file diff --git a/backend/Routes/api/collections.routes.js b/backend/Routes/api/collections.routes.js index dcae4418d..39366ba36 100644 --- a/backend/Routes/api/collections.routes.js +++ b/backend/Routes/api/collections.routes.js @@ -1,9 +1,10 @@ import express from 'express'; -import { createNewCollection, saveProject} from '../../Controllers/collection.controller.js'; +import { createNewCollection, 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.post("/delete/:cid/:project_id", authenticateUser, deleteProject); export default collectionRoutes; \ No newline at end of file From ff5192094fb1404f5cb2c435bb32d10921f88984 Mon Sep 17 00:00:00 2001 From: vriti62 Date: Fri, 29 Aug 2025 14:26:58 +0530 Subject: [PATCH 6/9] fixed-a-minor-issue --- backend/Routes/api/collections.routes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/Routes/api/collections.routes.js b/backend/Routes/api/collections.routes.js index 39366ba36..24577eb69 100644 --- a/backend/Routes/api/collections.routes.js +++ b/backend/Routes/api/collections.routes.js @@ -4,7 +4,7 @@ import { authenticateUser } from '../../Middlewares/auth.middleware.js'; const collectionRoutes = express.Router(); collectionRoutes.post("/create-collection", authenticateUser, createNewCollection); collectionRoutes.post("/:id", authenticateUser, saveProject); -collectionRoutes.post("/delete/:cid/:project_id", authenticateUser, deleteProject); +collectionRoutes.delete("/delete/:cid/:project_id", authenticateUser, deleteProject); export default collectionRoutes; \ No newline at end of file From 0047bec6d08559e413b22771d89412eded9ae245 Mon Sep 17 00:00:00 2001 From: vriti62 Date: Tue, 9 Sep 2025 02:08:31 +0530 Subject: [PATCH 7/9] extracted collection and project id from request body instead of params --- backend/Controllers/collection.controller.js | 3 +-- backend/Routes/api/collections.routes.js | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/backend/Controllers/collection.controller.js b/backend/Controllers/collection.controller.js index 67fc3d5d7..6e04e0760 100644 --- a/backend/Controllers/collection.controller.js +++ b/backend/Controllers/collection.controller.js @@ -93,8 +93,7 @@ export const saveProject = async (req, res) => { export const deleteProject = async(req,res)=>{ try{ const userID = req.user; - const collectionID = req.params.cid; - const projectID = req.params.project_id; + const {collectionID,projectID} = req.body; console.log(userID , projectID, collectionID); const existingUser = await User.findById(userID); if(!existingUser) return res.status(404).json("User not found"); diff --git a/backend/Routes/api/collections.routes.js b/backend/Routes/api/collections.routes.js index 24577eb69..4551c033f 100644 --- a/backend/Routes/api/collections.routes.js +++ b/backend/Routes/api/collections.routes.js @@ -4,7 +4,7 @@ import { authenticateUser } from '../../Middlewares/auth.middleware.js'; const collectionRoutes = express.Router(); collectionRoutes.post("/create-collection", authenticateUser, createNewCollection); collectionRoutes.post("/:id", authenticateUser, saveProject); -collectionRoutes.delete("/delete/:cid/:project_id", authenticateUser, deleteProject); +collectionRoutes.delete("/saved-projects", authenticateUser, deleteProject); export default collectionRoutes; \ No newline at end of file From 8c77b5f75046ef6a4bda4cc7666d1ab376c3af38 Mon Sep 17 00:00:00 2001 From: vriti62 Date: Tue, 16 Sep 2025 22:52:24 +0530 Subject: [PATCH 8/9] updated controller- removed logs --- backend/Controllers/collection.controller.js | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/Controllers/collection.controller.js b/backend/Controllers/collection.controller.js index 6e04e0760..5bcde87e5 100644 --- a/backend/Controllers/collection.controller.js +++ b/backend/Controllers/collection.controller.js @@ -94,7 +94,6 @@ export const deleteProject = async(req,res)=>{ try{ const userID = req.user; const {collectionID,projectID} = req.body; - console.log(userID , projectID, collectionID); const existingUser = await User.findById(userID); if(!existingUser) return res.status(404).json("User not found"); const deletedProject = await Collection.findOne({ From 9f368970d97a6050a0c50db54dceecae881d24ca Mon Sep 17 00:00:00 2001 From: vriti62 Date: Mon, 22 Sep 2025 23:52:01 +0530 Subject: [PATCH 9/9] added-delete-entire-collection-functionality --- backend/Controllers/collection.controller.js | 22 +++++++++++++++++++- backend/Routes/api/collections.routes.js | 3 ++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/backend/Controllers/collection.controller.js b/backend/Controllers/collection.controller.js index 5bcde87e5..cc72965ae 100644 --- a/backend/Controllers/collection.controller.js +++ b/backend/Controllers/collection.controller.js @@ -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, @@ -110,4 +110,24 @@ export const deleteProject = async(req,res)=>{ console.log(err); return res.status(400).json(err); } +} + +//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); + } } \ No newline at end of file diff --git a/backend/Routes/api/collections.routes.js b/backend/Routes/api/collections.routes.js index 4551c033f..a369e0cac 100644 --- a/backend/Routes/api/collections.routes.js +++ b/backend/Routes/api/collections.routes.js @@ -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; \ No newline at end of file