-
Notifications
You must be signed in to change notification settings - Fork 10
Labels
Description
Hello, Hacktoberfest contributors! 🎃
This is a comprehensive task to build the heart of our application: the Question model. This involves fully defining the schema with validations, implementing business logic for upvoting, and adding database indexes for performance optimization.
By completing this issue, you will have built an entire Mongoose model from start to finish!
## 🎯 The Goal
The objective is to complete all the TODO sections in the models/Question.js file. This includes defining the schema, creating an instance method, and adding database indexes.
## ✅ Acceptance Criteria
This issue is broken down into three main parts. All must be completed.
Part 1: Complete the Schema Definition
- questionText: Add the following validations:
required: true,trim: true, andminlength: 10. - company: Add
required: trueandtrim: true. - topic: Add
required: trueandtrim: true. - role: Add
required: trueandtrim: true. - difficulty: Add
required: trueandenum: ['Easy', 'Medium', 'Hard']. - upvotes: Add a
min: 0validation. - Schema Options: Enable
timestamps: truein the schema's options object.
Part 2: Implement the 'addUpvote' Instance Method
- Create an
asyncinstance method on the schema namedaddUpvotethat accepts auserId. - Toggle Logic:
- If the
userIdis already in thethis.upvotedByarray, remove it and decrementthis.upvotesby 1. - If the
userIdis not in thethis.upvotedByarray, add it and incrementthis.upvotesby 1.
- If the
- The method must save the updated document using
await this.save(). - The method should return the newly saved document.
Part 3: Add Database Indexes for Optimization
- Add a single-field index on the
companyfield. - Add a single-field index on the
topicfield. - Add a single-field index on the
difficultyfield. - Add a compound text index on
questionTextto enable efficient text-based searches. - Add a compound index for common filter combinations:
{ company: 1, topic: 1, role: 1 }.
📂 Relevant File
backend/models/Question.js
🚀 How to Contribute
- Claim this issue: Leave a comment below to let us know you're working on it.
- Fork & Clone: Fork the repository and clone it to your local machine.
- Create a Branch: Make a new branch for your feature (e.g.,
feat/implement-question-model). - Implement: Write the code to satisfy all the acceptance criteria in the relevant file.
- Commit & Push: Commit your changes with a clear message and push them to your fork.
- Open a Pull Request: Submit a PR back to our main branch, linking this issue in the description.
We're excited for your contribution. Happy coding! ✨