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
29 changes: 22 additions & 7 deletions backend/Controllers/collection.controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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;
Expand Down Expand Up @@ -37,17 +38,24 @@ 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 } },
Expand All @@ -60,8 +68,15 @@ 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();


Expand Down
9 changes: 5 additions & 4 deletions backend/Routes/api/collections.routes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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;
export default collectionRoutes;

1 change: 0 additions & 1 deletion backend/Routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ router.use('/notification', notificationRoutes);
router.use('/subscriber', subscriberRoutes);
router.use("/collection", collectionRoutes);
router.use('/collaboration', collaborationRoutes);

export default router;