Skip to content

Commit db326dc

Browse files
authored
Save a New Project to an Existing Collection (or Auto create the collection) (#1213)
* 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
1 parent 0ecfcf3 commit db326dc

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

backend/Controllers/collection.controller.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Collection from "../Models/collection.model.js";
22
import User from "../Models/user.model.js";
33

4-
//create a new collection
4+
/*create a new collection-
5+
1.in case of manula creation of collection, the project_id is set to null since nothing is saved */
56
export const createNewCollection = async(req,res)=>{
67
try{
78
const userID = req.user;
@@ -37,17 +38,24 @@ export const saveProject = async (req, res) => {
3738
const existingProject = await Collection.findOne({userID,collection_name,project_id});
3839
if(existingProject) return res.status(400).json("Project already exists in this collection");
3940

40-
// Case 1: No collection exists → create new one
41+
// Case 1: No collection exists → save in 'default-collection' and save the project
4142
if (existingCollection.length === 0) {
42-
const newCollection = new Collection({ userID, collection_name, project_id });
43+
const newCollection = new Collection(
44+
{
45+
userID,
46+
collection_name:"default-collection",
47+
project_id
48+
}
49+
);
4350
await newCollection.save();
4451

4552
return res.status(201).json(
46-
`New collection '${collection_name}' created and project saved for ${existingUser.personal_info.username}`
53+
`Collection added to default collection and project saved for ${existingUser.personal_info.username}`
4754
);
4855
}
4956

50-
// Case 2: Try to update empty slot
57+
// 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
58+
5159
const emptySlot = await Collection.findOneAndUpdate(
5260
{ userID, collection_name, project_id: null },
5361
{ $set: { project_id } },
@@ -60,8 +68,15 @@ export const saveProject = async (req, res) => {
6068
);
6169
}
6270

63-
// Case 3: No empty slots → create new document
64-
const newDoc = new Collection({ userID, collection_name, project_id });
71+
72+
// Case 3: No empty slots → create new document for the project being saved
73+
const newDoc = new Collection(
74+
{
75+
userID,
76+
collection_name,
77+
project_id
78+
});
79+
6580
await newDoc.save();
6681

6782

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import express from 'express';
22
import { createNewCollection, saveProject} from '../../Controllers/collection.controller.js';
33
import { authenticateUser } from '../../Middlewares/auth.middleware.js';
4-
const collection = express.Router();
5-
collection.post("/create-collection", authenticateUser, createNewCollection);
6-
collection.post("/:id", authenticateUser, saveProject);
4+
const collectionRoutes = express.Router();
5+
collectionRoutes.post("/create-collection", authenticateUser, createNewCollection);
6+
collectionRoutes.post("/:id", authenticateUser, saveProject);
77

88

9-
export default collection;
9+
export default collectionRoutes;
10+

backend/Routes/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ router.use('/notification', notificationRoutes);
1919
router.use('/subscriber', subscriberRoutes);
2020
router.use("/collection", collectionRoutes);
2121
router.use('/collaboration', collaborationRoutes);
22-
2322
export default router;

0 commit comments

Comments
 (0)