Skip to content

BE_12 - Answers Fetch API Integration #32

@notjackl3

Description

@notjackl3

Summary

Implement service layer to fetch answers for a specific question with proper sorting and author visibility.
[Estimated hours: 3-4]

Objectives

  • Create answer fetching service
  • Include accepted answer indicator
  • Implement pagination for questions with many answers
  • Return author info (respecting anonymity settings)
  • Sort answers by accepted status, votes, then time

Description

Complements BE_11 by providing the logic to fetch answers for a specific question. Used when a user expands a question to view all answers or when hydrating state on page load.

Fetch Flow

User clicks on question to expand
     → GET /api/questions/:id/answers
     → Controller calls AnswerService.getAnswers()
     → Service validates user can view parent question
     → Returns answers sorted by relevance
     → Frontend displays answer thread

Technical Details

File Structure

src/services/
└── answerService.ts
    Functions:
    - getQuestionAnswers(questionId, userId, options)
    - getAnswerById(answerId, userId)
    - sortAnswers(answers)

Response Shape

{
  answers: Answer[],
  nextCursor: string | null,
  acceptedAnswerId: string | null
}

Acceptance Criteria

  • User can fetch answers for questions they can view
  • Cannot fetch answers for questions user cannot view (403)
  • Accepted answer is flagged in response with isAccepted: true
  • Answers sorted: accepted first, then by votes (desc), then by createdAt (asc)
  • Anonymous answer authors hidden from students
  • TAs and professors can see anonymous answer authors
  • Pagination works correctly for 50+ answers
  • Deleted answers not returned
  • Each answer includes authorRole field (STUDENT, TA, PROFESSOR)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions