getCourse method

Future<CourseDto> getCourse(
  1. String courseId
)

Fetches detailed information about a specific course from the catalog.

Returns course details including bilingual names, descriptions, credits, and hours per week.

The courseId should be a course code obtained from the course.id field of a ScheduleDto.

Throws an Exception if the course details table is not found.

Implementation

Future<CourseDto> getCourse(String courseId) async {
  final response = await _courseDio.get(
    'Curr.jsp',
    queryParameters: {'format': '-2', 'code': courseId},
  );

  final document = parse(response.data);
  final table = document.querySelector('table');
  if (table == null) {
    throw Exception('Course details table not found.');
  }

  final tableRows = table.querySelectorAll('tr');

  // Second row contains id, name, credits, hours
  final secondRowCells = tableRows[1].children;
  final id = _parseCellText(secondRowCells[0]);
  final nameZh = _parseCellText(secondRowCells[1]);
  final nameEn = _parseCellText(secondRowCells[2]);
  final credits = double.tryParse(secondRowCells[3].text.trim());
  final hours = int.tryParse(secondRowCells[4].text.trim());

  // Second column of the third and fourth rows contain description
  final descriptionZh = _parseCellText(tableRows[2].children[1]);
  final descriptionEn = _parseCellText(tableRows[3].children[1]);

  return (
    id: id,
    nameZh: nameZh,
    nameEn: nameEn,
    credits: credits,
    hours: hours,
    descriptionZh: descriptionZh,
    descriptionEn: descriptionEn,
  );
}