Skip to content

Conversation

@Vriti62
Copy link
Contributor

@Vriti62 Vriti62 commented Aug 23, 2025

Closes: #1194
Linked to parent issue: #1180

Project Published URL 🔗

Backend-only change. Tested locally using Postman

Describe the add-ons or changes you've made 📃

  • Implemented backend logic to allow users to save a project into their saved collections.
  • Added auto-creation of a collection if it does not already exist.
  • Prevented duplicate project entries inside the same collection.
  • Used req.user from authentication middleware to ensure only logged-in users can save projects.
  • Response returns appropriate messages (new collection created, project saved, or duplicate prevention).

Screenshots 📷

image fig1: auto-creation of collection and saving project image fig2: duplicate projects check in same collection image fig3: added to an existing collection

Type of change ☑️

What sort of change have you made:

  • [☑️ ] New feature
  • [☑️ ] Database update

Note to reviewers 📄

This is a backend-only feature and does not require frontend updates.

  • Kindly review the collection auto-creation logic and suggest if collection schema needs adjustment for multiple project references instead of separate documents.
    Always open for feedbacks!!

@vercel
Copy link

vercel bot commented Aug 23, 2025

@Vriti62 is attempting to deploy a commit to the avdheshvarshney's projects Team on Vercel.

A member of the Team first needs to authorize it.

@netlify
Copy link

netlify bot commented Aug 23, 2025

Deploy Preview for code-a2z canceled.

Name Link
🔨 Latest commit 0bea348
🔍 Latest deploy log https://app.netlify.com/projects/code-a2z/deploys/68aa24005f8cb2000807813c

@github-actions
Copy link

Thank you for submitting your pull request! We'll review it as soon as possible. For further communication, join our discord server https://discord.gg/tSqtvHUJzE.

Copy link
Member

@Avdhesh-Varshney Avdhesh-Varshney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Vriti62, You have done nice work.
But also consider these guidelines of PR review.

If you are beginner in git, connect with me your problem

required:true
},
project_id:{
type:String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 1 collection, there should be the multiple projects to be saved.
So type should be array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback!
Actually my initial approach was that I saved a document for each projects, cause its efficient to retrieve like that
with an array, the project_id would be nested (array of multiple project_ids) and hence,
-when a db query goes to fetch, it has to first go to the collection, then search the document, then go into the array and search
Instead, my plan was to make it efficient and make it search only inside the document, since it would be scanning the document anyways.
-it avoids nested array lookups, since queries would only need to scan documents directly rather than traversing into arrays. This felt a bit more efficient in terms of retrieval.

That said, I understand the consistency and maintainability benefits of keeping project_ids as an array inside a collection document. I can update the schema and controller logic to follow that design instead.

collection.post("/:id", authenticateUser, saveProject);


export default collection; No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Routes/index.js, you are importing collectionRoutes and here you are exporting collection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing it out!
I think its just a naming convention error
Ill edit it out !!

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't consider this case. If collection is not present, user either create the new collection or providing a default collection.

As saving videos for watch later in youtube.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this point !
So, what i got from your part is that you want, either the project is saved to a user's collection, and if no collection is there, a separate collection (like watch later) exists.
So, if no name provided, ill be adding the project to a 'default-collection' in the same case!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, if no name provided, ill be adding the project to a 'default-collection' in the same case!

Correct!

);
}

// Case 2: Try to update empty slot
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is empty slot means default collection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty slot here is for project_id
Actually this is a case i considered for when user manually creates a collection
When a manual collection is created- the project_id field of it is null by default
So, now when user adds the first project to the manually created collection, instead of creating a new document for the same collection and setting the project_id as the saved project's id, i modified that document which was created for manual collection creation, and replace the 'null' id with the project_id of the project being saved
This was done to avoid redundancy in the database! So, every document is used properly!

);
}

// Case 3: No empty slots → create new document
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the use cases of all 3 cases

It should be simple. If user saving any project, FE will display the UI to either select the default or select any existing already created collection or create a new collection.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey!
The 3 use cases for the saveproject endpoint is simply to create a new collection (which now is changed to adding the project to a default collection as per your say), and avoiding any redundancy in the document!
Please let me know if its still not justifiable or if any changes are required!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backend: Save a New Project to an Existing Collection (or Auto create the collection)

2 participants