-
Notifications
You must be signed in to change notification settings - Fork 365
Backend: Save a New Project to an Existing Collection (or Auto create the collection) #1207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@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. |
✅ Deploy Preview for code-a2z canceled.
|
|
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. |
Avdhesh-Varshney
left a comment
There was a problem hiding this 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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!!
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 📃
req.userfrom authentication middleware to ensure only logged-in users can save projects.Screenshots 📷
Type of change ☑️
What sort of change have you made:
Note to reviewers 📄
This is a backend-only feature and does not require frontend updates.
Always open for feedbacks!!