upsertCourseOffering method

Future<int> upsertCourseOffering({
  1. required int semesterId,
  2. String? number,
  3. required String nameZh,
  4. String? nameEn,
  5. Value<String?> courseCode = const Value.absent(),
  6. Value<double?> credits = const Value.absent(),
  7. Value<int?> hours = const Value.absent(),
  8. Value<int?> phase = const Value.absent(),
  9. Value<String?> status = const Value.absent(),
  10. Value<String?> language = const Value.absent(),
  11. Value<String?> remarks = const Value.absent(),
  12. bool? inCourseTable,
})

Returns the ID of an existing course offering, or creates/updates one.

Upserts by number. Null-number entries always insert (SQLite treats NULLs as distinct) — caller must delete stale ones first.

Updates the offering's semester to semesterId, allowing a timetable to correct a semester provisionally inferred from a score record. The score keeps its own semester and reference to the same offering.

An omitted Value leaves the stored field untouched, so a source that cannot see a field must omit it rather than pass null.

Implementation

Future<int> upsertCourseOffering({
  required int semesterId,
  String? number,
  required String nameZh,
  String? nameEn,
  Value<String?> courseCode = const Value.absent(),
  Value<double?> credits = const Value.absent(),
  Value<int?> hours = const Value.absent(),
  Value<int?> phase = const Value.absent(),
  Value<String?> status = const Value.absent(),
  Value<String?> language = const Value.absent(),
  Value<String?> remarks = const Value.absent(),
  bool? inCourseTable,
}) async {
  return (await into(courseOfferings).insertReturning(
    CourseOfferingsCompanion.insert(
      courseCode: courseCode,
      semester: semesterId,
      number: Value(number),
      nameZh: nameZh,
      nameEn: Value(nameEn),
      credits: credits,
      hours: hours,
      phase: phase,
      status: status,
      language: language,
      remarks: remarks,
      inCourseTable: Value(inCourseTable ?? false),
    ),
    onConflict: DoUpdate(
      (old) => CourseOfferingsCompanion(
        semester: Value(semesterId),
        courseCode: courseCode,
        nameZh: Value(nameZh),
        nameEn: .absentIfNull(nameEn),
        credits: credits,
        hours: hours,
        phase: phase,
        status: status,
        language: language,
        remarks: remarks,
        inCourseTable: .absentIfNull(inCourseTable),
      ),
      target: [courseOfferings.number],
    ),
  )).id;
}