backend answer module

πŸ“… 24 Jul 2025

View on Github β†—

date: 2025-07-24 topics: [nodejs, backend, architecture, clean-code, devflow] day: 8

πŸ“˜ Day 8 – DevFlow: Answer Module + Clean Backend Architecture

βœ… What I Worked On

  • Designed & implemented Answer module with a scalable backend structure
  • Applied proper layered architecture: Controller β†’ Service β†’ Repository
  • Added secure deletion logic: Only the author can delete their answer
  • Integrated Husky + lint-staged for automated code formatting/linting

🧠 Key Highlights

🧱 Layered Backend Design

  • Built Controller β†’ Service β†’ Repository abstraction
  • Followed single responsibility principle (SRP)
  • Connected Mongoose models via BaseRepository

πŸ” Secure Answer Deletion Logic

if (answer.userId !== req.user.id) {
  throw new UnauthorizedError('Not allowed');
}

βœ… Prevents users from deleting others' answers βœ… Enforces strict ownership in business logic layer

πŸ§ͺ AnswerService Architecture

  • Input validation via custom validator
  • Added methods like createAnswer, findByUser, findAnswerById
  • Unit-testable service with clear doc comments

πŸ—ƒοΈ Repository Pattern

  • Used BaseRepository to avoid duplicate DB logic
  • Custom methods for queries (e.g., findByUser, getByQuestionId)
  • Keeps data access abstracted and clean

πŸ“‹ Swagger + JSDoc Style Route Docs

/**
 * @route   POST /api/v1/answers
 * @desc    Create new answer
 * @access  Private
 */

Helps devs onboard quickly and keeps APIs self-documented

🧼 Code Quality Automation with Husky

  • pre-commit β†’ npm run lint:fix
  • pre-push β†’ format checks or tests
  • Ensures no messy code lands in the repo

❌ Blockers

  • Small issue with TypeScript hinting on inherited repository methods (resolved)
  • Swagger parsing didn't like multiline JSDoc comments at first

🧠 Reflection

I didn’t just write code β€” I built the foundation for something scalable. Security, structure, and automation are now baked into DevFlow.

β€œSpeed is nothing without stability β€” and that’s what architecture gives.”

πŸ”œ Next Up

  • Finish AnswerController: update, getById, getAllByQuestionId
  • Add unit tests for service logic
  • Consider adding soft delete for future moderation features

πŸ”— References / Code