Skip to content

backend/models/Question.js : Fully Implement the Question Mongoose Model #6

@Tirth9978

Description

@Tirth9978

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, and minlength: 10.
  • company: Add required: true and trim: true.
  • topic: Add required: true and trim: true.
  • role: Add required: true and trim: true.
  • difficulty: Add required: true and enum: ['Easy', 'Medium', 'Hard'].
  • upvotes: Add a min: 0 validation.
  • Schema Options: Enable timestamps: true in the schema's options object.

Part 2: Implement the 'addUpvote' Instance Method

  • Create an async instance method on the schema named addUpvote that accepts a userId.
  • Toggle Logic:
    • If the userId is already in the this.upvotedBy array, remove it and decrement this.upvotes by 1.
    • If the userId is not in the this.upvotedBy array, add it and increment this.upvotes by 1.
  • 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 company field.
  • Add a single-field index on the topic field.
  • Add a single-field index on the difficulty field.
  • Add a compound text index on questionText to 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

  1. Claim this issue: Leave a comment below to let us know you're working on it.
  2. Fork & Clone: Fork the repository and clone it to your local machine.
  3. Create a Branch: Make a new branch for your feature (e.g., feat/implement-question-model).
  4. Implement: Write the code to satisfy all the acceptance criteria in the relevant file.
  5. Commit & Push: Commit your changes with a clear message and push them to your fork.
  6. 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! ✨

Metadata

Metadata

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions