Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ flutter test
| GET | `/api/sessions/{id}` | Get one session |
| PATCH | `/api/sessions/{id}/close` | Close a session |
| POST | `/api/sessions/{id}/doubts` | Join a session (`student_name`, `student_email`, `topic`, `question`) |
| GET | `/api/sessions/{id}/queue` | Waiting doubts, oldest first |
| GET | `/api/sessions/{id}/queue` | Waiting doubts, oldest first; optional `limit` (1–100) and `offset` (0 or more) query parameters |
| POST | `/api/sessions/{id}/next` | Take the oldest waiting doubt |
| GET | `/api/sessions/{id}/stats` | Doubt counts by status |
| GET | `/api/doubts/{id}` | Get one doubt, with `position` while it is waiting |
Expand Down
9 changes: 8 additions & 1 deletion backend/src/Controller/DoubtController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ public function join(Request $request, array $params): Response

public function queue(Request $request, array $params): Response
{
return new Response(200, $this->doubts->queue($params['id']));
return new Response(
200,
$this->doubts->queue(
$params['id'],
$request->query['limit'] ?? null,
$request->query['offset'] ?? null
)
);
}

public function next(Request $request, array $params): Response
Expand Down
28 changes: 22 additions & 6 deletions backend/src/Repository/DoubtRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,28 @@ public function find(int $id): ?array
return $row === false ? null : $this->format($row);
}

public function findWaiting(int $sessionId): array
{
$stmt = $this->pdo->prepare(
"SELECT * FROM doubts WHERE session_id = ? AND status = 'WAITING' ORDER BY created_at ASC, id ASC"
);
$stmt->execute([$sessionId]);
public function findWaiting(
int $sessionId,
?int $limit = null,
int $offset = 0
): array {
$sql = "SELECT * FROM doubts
WHERE session_id = ? AND status = 'WAITING'
ORDER BY created_at ASC, id ASC";

$params = [$sessionId];

if ($limit !== null) {
$sql .= ' LIMIT ? OFFSET ?';
$params[] = $limit;
$params[] = $offset;
} elseif ($offset > 0) {
$sql .= ' LIMIT -1 OFFSET ?';
$params[] = $offset;
}

$stmt = $this->pdo->prepare($sql);
$stmt->execute($params);

return array_map([$this, 'format'], $stmt->fetchAll());
}
Expand Down
65 changes: 59 additions & 6 deletions backend/src/Service/DoubtService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

class DoubtService
{
public function __construct(private DoubtRepository $doubts, private SessionRepository $sessions)
{
}
public function __construct(private DoubtRepository $doubts, private SessionRepository $sessions) {}

public function join(int $sessionId, array $data): array
{
Expand Down Expand Up @@ -49,11 +47,32 @@ public function get(int $id): array
return $this->withPosition($this->findDoubt($id));
}

public function queue(int $sessionId): array
{
public function queue(
int $sessionId,
mixed $limit = null,
mixed $offset = null
): array {
$this->findSession($sessionId);

return $this->doubts->findWaiting($sessionId);
$validatedLimit = $this->validatePaginationValue(
$limit,
'limit',
1,
100
);

$validatedOffset = $this->validatePaginationValue(
$offset,
'offset',
0,
PHP_INT_MAX
);

return $this->doubts->findWaiting(
$sessionId,
$validatedLimit,
$validatedOffset ?? 0
);
}

public function next(int $sessionId): array
Expand Down Expand Up @@ -151,4 +170,38 @@ private function requiredText(array $data, string $field, int $maxLength): strin

return $value;
}

private function validatePaginationValue(
mixed $value,
string $name,
int $minimum,
int $maximum
): ?int {
if ($value === null) {
return null;
}

if (
!is_string($value)
|| preg_match('/^-?\d+$/D', $value) !== 1
) {
throw new ValidationException(
"$name must be a whole number"
);
}

$validated = filter_var($value, FILTER_VALIDATE_INT);

if (
$validated === false
|| $validated < $minimum
|| $validated > $maximum
) {
throw new ValidationException(
"$name must be between $minimum and $maximum"
);
}

return $validated;
}
}
44 changes: 44 additions & 0 deletions backend/tests/Repository/DoubtRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,48 @@ private function save(string $name, string $createdAt): array
$createdAt
);
}

public function testFindWaitingRespectsLimit(): void
{
$first = $this->save('First', '2026-01-01 10:01:00');
$second = $this->save('Second', '2026-01-01 10:02:00');
$third = $this->save('Third', '2026-01-01 10:03:00');

$waiting = $this->doubts->findWaiting($this->sessionId, 2);

$this->assertCount(2, $waiting);
$this->assertSame(
[$first['id'], $second['id']],
array_column($waiting, 'id')
);
}

public function testFindWaitingRespectsOffset(): void
{
$first = $this->save('First', '2026-01-01 10:01:00');
$second = $this->save('Second', '2026-01-01 10:02:00');
$third = $this->save('Third', '2026-01-01 10:03:00');

$waiting = $this->doubts->findWaiting($this->sessionId, 2, 1);

$this->assertSame(
[$second['id'], $third['id']],
array_column($waiting, 'id')
);
}

public function testFindWaitingWithoutPaginationReturnsAllWaitingDoubts(): void
{
$first = $this->save('First', '2026-01-01 10:01:00');
$second = $this->save('Second', '2026-01-01 10:02:00');
$third = $this->save('Third', '2026-01-01 10:03:00');

$waiting = $this->doubts->findWaiting($this->sessionId);

$this->assertSame(
[$first['id'], $second['id'], $third['id']],
array_column($waiting, 'id')
);
}

}
63 changes: 63 additions & 0 deletions backend/tests/Service/DoubtServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,67 @@ private function validData(): array
'question' => 'How do I loop over an array?',
];
}


public function testQueueWithInvalidLimitThrows(): void
{
$session = $this->sessions->create([
'title' => 'PHP',
'mentor_name' => 'Priya',
]);

foreach (['0', '-1', 'abc', '101', '1.5'] as $limit) {
try {
$this->doubts->queue($session['id'], $limit);
$this->fail("Expected ValidationException for limit $limit");
} catch (ValidationException) {
$this->assertTrue(true);
}
}
}

public function testQueueWithInvalidOffsetThrows(): void
{
$session = $this->sessions->create([
'title' => 'PHP',
'mentor_name' => 'Priya',
]);

foreach (['-1', 'abc', '1.5'] as $offset) {
try {
$this->doubts->queue($session['id'], null, $offset);
$this->fail("Expected ValidationException for offset $offset");
} catch (ValidationException) {
$this->assertTrue(true);
}
}
}

public function testQueueWithOffsetOnlyReturnsRemainingDoubts(): void
{
$session = $this->sessions->create([
'title' => 'PHP',
'mentor_name' => 'Priya',
]);

$first = $this->doubts->join($session['id'], $this->validData());
$second = $this->doubts->join(
$session['id'],
['student_name' => 'Ben'] + $this->validData()
);

$queue = $this->doubts->queue($session['id'], null, '1');

$this->assertSame(
[$second['id']],
array_column($queue, 'id')
);
}

public function testQueueForUnknownSessionThrowsNotFound(): void
{
$this->expectException(NotFoundException::class);

$this->doubts->queue(999999);
}
}