Skip to content

Commit

Permalink
Change Id.id to Id.value. (#1550)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas <--help>
  • Loading branch information
Jonas-Sander authored Apr 24, 2024
1 parent 7eccb70 commit 99c7e29
Show file tree
Hide file tree
Showing 43 changed files with 98 additions and 95 deletions.
2 changes: 1 addition & 1 deletion app/lib/grades/grades_service/grades_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ enum WeightType {
}

class GradeTypeId extends Id {
const GradeTypeId(super.id);
const GradeTypeId(super.value);
}

class Subject extends Equatable {
Expand Down
53 changes: 27 additions & 26 deletions app/lib/grades/grades_service/src/grades_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,24 @@ class FirestoreGradesStateRepository extends GradesStateRepository {

static Map<String, Object> toDto(GradesState state) {
final currentTermOrNull =
state.terms.firstWhereOrNull((term) => term.isActiveTerm)?.id.id;
state.terms.firstWhereOrNull((term) => term.isActiveTerm)?.id.value;
return {
if (currentTermOrNull != null) 'currentTerm': currentTermOrNull,
'terms': state.terms
.map((term) => MapEntry(term.id.id, TermDto.fromTerm(term).toData()))
.map((term) =>
MapEntry(term.id.value, TermDto.fromTerm(term).toData()))
.toMap(),
'grades': state.terms
.expand((term) => term.subjects.expand((p0) => p0.grades))
.map((g) => MapEntry(g.id.id, GradeDto.fromGrade(g).toData()))
.map((g) => MapEntry(g.id.value, GradeDto.fromGrade(g).toData()))
.toMap(),
'customGradeTypes': state.customGradeTypes
.map((element) => MapEntry(element.id.id,
.map((element) => MapEntry(element.id.value,
CustomGradeTypeDto.fromGradeType(element).toData()))
.toMap(),
'subjects': state.subjects
.map((subject) =>
MapEntry(subject.id.id, SubjectDto.fromSubject(subject).toData()))
.map((subject) => MapEntry(
subject.id.value, SubjectDto.fromSubject(subject).toData()))
.toMap()
};
}
Expand Down Expand Up @@ -228,7 +229,7 @@ class FirestoreGradesStateRepository extends GradesStateRepository {
final combinedTermSubjects = allTermSubjects
.map((termSub) {
final subject = subjects.firstWhereOrNull(
(subject) => subject.id.id == termSub.termSubject.id);
(subject) => subject.id.value == termSub.termSubject.id);
if (subject == null) {
log('No subject found for the term subject id ${termSub.termSubject.id}.');
return null;
Expand Down Expand Up @@ -259,11 +260,11 @@ class FirestoreGradesStateRepository extends GradesStateRepository {
.map((key, value) => MapEntry(GradeTypeId(key), value.toWeight()))
.toIMap(),
weightingForTermGrade:
subTerm.subjectWeights[subject.id.id]?.toWeight() ??
subTerm.subjectWeights[subject.id.value]?.toWeight() ??
const Weight.factor(1),
grades: grades
.where((grade) =>
grade.subjectId == subject.id && grade.termId.id == termId)
grade.subjectId == subject.id && grade.termId.value == termId)
.toIList(),
weightType: termSubject.gradeComposition.weightType,
gradingSystem: subTerm.gradingSystem.toGradingSystemModel(),
Expand All @@ -282,7 +283,7 @@ class FirestoreGradesStateRepository extends GradesStateRepository {
gradingSystem: dto.gradingSystem.toGradingSystemModel(),
isActiveTerm: data['currentTerm'] == dto.id,
subjects: termSubjects
.where((subject) => subject.termId.id == dto.id)
.where((subject) => subject.termId.value == dto.id)
.toIList(),
name: dto.displayName,
// Change both to num
Expand Down Expand Up @@ -411,7 +412,7 @@ class CustomGradeTypeDto {

factory CustomGradeTypeDto.fromGradeType(GradeType gradeType) {
return CustomGradeTypeDto(
id: gradeType.id.id,
id: gradeType.id.value,
displayName: gradeType.displayName!,
);
}
Expand Down Expand Up @@ -447,16 +448,16 @@ class TermDto {

factory TermDto.fromTerm(TermModel term) {
return TermDto(
id: term.id.id,
id: term.id.value,
displayName: term.name,
createdOn: term.createdOn?.toTimestampOrNull(),
finalGradeTypeId: term.finalGradeType.id,
finalGradeTypeId: term.finalGradeType.value,
gradingSystem: term.gradingSystem.spec.gradingSystem,
subjects: term.subjects.map(TermSubjectDto.fromSubject).toList(),
subjectWeights: Map.fromEntries(term.subjects.map((subject) =>
MapEntry(subject.id.id, subject.weightingForTermGrade.toDto()))),
MapEntry(subject.id.value, subject.weightingForTermGrade.toDto()))),
gradeTypeWeights: term.gradeTypeWeightings
.map((gradeId, weight) => MapEntry(gradeId.id, weight.toDto()))
.map((gradeId, weight) => MapEntry(gradeId.value, weight.toDto()))
.unlock,
);
}
Expand Down Expand Up @@ -511,9 +512,9 @@ class TermSubjectDto {

factory TermSubjectDto.fromSubject(SubjectModel subject) {
return TermSubjectDto(
id: subject.id.id,
grades: subject.grades.map((grade) => grade.id.id).toList(),
finalGradeType: subject.finalGradeType.id,
id: subject.id.value,
grades: subject.grades.map((grade) => grade.id.value).toList(),
finalGradeType: subject.finalGradeType.value,
gradeComposition: SubjectGradeCompositionDto.fromSubject(subject),
createdOn: subject.createdOn?.toTimestampOrNull(),
);
Expand Down Expand Up @@ -558,10 +559,10 @@ class SubjectGradeCompositionDto {
return SubjectGradeCompositionDto(
weightType: subject.weightType,
gradeTypeWeights: subject.gradeTypeWeightings
.map((gradeId, weight) => MapEntry(gradeId.id, weight.toDto()))
.map((gradeId, weight) => MapEntry(gradeId.value, weight.toDto()))
.unlock,
gradeWeights: Map.fromEntries(subject.grades
.map((grade) => MapEntry(grade.id.id, grade.weight.toDto()))),
.map((grade) => MapEntry(grade.id.value, grade.weight.toDto()))),
);
}

Expand Down Expand Up @@ -612,13 +613,13 @@ class GradeDto {

factory GradeDto.fromGrade(GradeModel grade) {
return GradeDto(
id: grade.id.id,
termId: grade.termId.id,
subjectId: grade.subjectId.id,
id: grade.id.value,
termId: grade.termId.value,
subjectId: grade.subjectId.value,
numValue: grade.value.asNum,
originalInput: grade.originalInput,
gradingSystem: grade.gradingSystem.spec.gradingSystem,
gradeType: grade.gradeType.id,
gradeType: grade.gradeType.value,
receivedAt: grade.date,
includeInGrading: grade.takenIntoAccount,
title: grade.title,
Expand Down Expand Up @@ -691,7 +692,7 @@ class SubjectDto {

factory SubjectDto.fromSubject(Subject subject) {
return SubjectDto(
id: subject.id.id,
id: subject.id.value,
design: subject.design,
name: subject.name,
abbreviation: subject.abbreviation,
Expand Down Expand Up @@ -745,7 +746,7 @@ class ConnectedCourseDto {

factory ConnectedCourseDto.fromConnectedCourse(ConnectedCourse course) {
return ConnectedCourseDto(
id: course.id.id,
id: course.id.value,
name: course.name,
abbreviation: course.abbreviation,
subjectName: course.subjectName,
Expand Down
2 changes: 1 addition & 1 deletion app/lib/grades/models/grade_id.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
import 'package:common_domain_models/common_domain_models.dart';

class GradeId extends Id {
const GradeId(super.id);
const GradeId(super.value);
}
2 changes: 1 addition & 1 deletion app/lib/grades/models/subject_id.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
import 'package:common_domain_models/common_domain_models.dart';

class SubjectId extends Id {
const SubjectId(super.id);
const SubjectId(super.value);
}
2 changes: 1 addition & 1 deletion app/lib/grades/models/term_id.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
import 'package:common_domain_models/common_domain_models.dart';

class TermId extends Id {
const TermId(super.id);
const TermId(super.value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CreateTermPageController extends ChangeNotifier {
}

try {
final termId = TermId(Id.generate().id);
final termId = TermId(Id.generate().value);
gradesService.addTerm(
id: termId,
name: view.name!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class GradesDialogController extends ChangeNotifier {
.firstWhereOrNull((subject) => subject.name == course.subject);
final hasSubjectForThisCourse = matchingSubject != null;
if (!hasSubjectForThisCourse) {
final subjectId = SubjectId(Id.generate().id);
final subjectId = SubjectId(Id.generate().value);
subjects.add(course.toSubject(subjectId));
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/lib/grades/pages/shared/saved_grade_id.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
import 'package:common_domain_models/common_domain_models.dart';

class SavedGradeId extends Id {
const SavedGradeId(super.id);
const SavedGradeId(super.value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class TermSettingsPageController extends ChangeNotifier {
final connectedCourses = _getConnectedCourses(subjectId);
// We generate a new subject ID because the previous subject ID was just the
// course ID (as temporary ID).
final newSubjectId = SubjectId(Id.generate().id);
final newSubjectId = SubjectId(Id.generate().value);
gradesService.addSubject(
Subject(
id: newSubjectId,
Expand Down
2 changes: 1 addition & 1 deletion app/lib/homework/homework_dialog/homework_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class _HomeworkDialogState extends State<HomeworkDialog> {

if (widget.id != null) {
homework = szContext.api.homework
.singleHomework(widget.id!.id, source: Source.cache)
.singleHomework(widget.id!.value, source: Source.cache)
.then((value) {
bloc = HomeworkDialogBloc(
homeworkId: widget.id,
Expand Down
14 changes: 8 additions & 6 deletions app/lib/homework/homework_dialog/homework_dialog_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -939,9 +939,11 @@ class HomeworkDialogApi {

Future<HomeworkDto> loadHomework(HomeworkId homeworkId) async {
try {
return _api.homework.singleHomework(homeworkId.id, source: Source.cache);
return _api.homework
.singleHomework(homeworkId.value, source: Source.cache);
} catch (e) {
return _api.homework.singleHomework(homeworkId.id, source: Source.server);
return _api.homework
.singleHomework(homeworkId.value, source: Source.server);
}
}

Expand All @@ -953,21 +955,21 @@ class HomeworkDialogApi {
}

Future<Course> loadCourse(CourseId courseId) async {
return (await _api.course.streamCourse(courseId.id).first)!;
return (await _api.course.streamCourse(courseId.value).first)!;
}

Future<HomeworkDto> createHomework(
CourseId courseId, UserInput userInput) async {
final localFiles = userInput.localFiles;
final course = (await _api.course.streamCourse(courseId.id).first)!;
final course = (await _api.course.streamCourse(courseId.value).first)!;
final authorID = _api.user.authUser!.uid;
final authorReference = _api.references.users.doc(authorID);
final user = (await _api.user.userStream.first)!;
final authorName = user.name;
final typeOfUser = user.typeOfUser;

final attachments = await _api.fileSharing.uploadAttachments(
localFiles, courseId.id, authorReference.id, authorName,
localFiles, courseId.value, authorReference.id, authorName,
isPrivate: userInput.private);

final homework = HomeworkDto.create(
Expand Down Expand Up @@ -1011,7 +1013,7 @@ class HomeworkDialogApi {
Future<HomeworkDto> editHomework(HomeworkId homeworkId, UserInput userInput,
{List<CloudFile> removedCloudFiles = const []}) async {
final oldHomework =
await _api.homework.singleHomeworkStream(homeworkId.id).first;
await _api.homework.singleHomeworkStream(homeworkId.value).first;
List<String> attachments = oldHomework.attachments.toList();
final editorName = (await _api.user.userStream.first)!.name;
final editorID = _api.user.authUser!.uid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class HomeworkCompletionUserListBloc extends BlocBase {
}

Stream<List<UserHasCompletedHomeworkView>> get userViews {
return _gateway.singleHomeworkStream(_id.id).asyncMap(_mapToViews);
return _gateway.singleHomeworkStream(_id.value).asyncMap(_mapToViews);
}

Future<List<UserHasCompletedHomeworkView>> _mapToViews(
Expand Down
2 changes: 1 addition & 1 deletion app/lib/legal/privacy_policy/src/document_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class DocumentController {

Future<void> scrollToDocumentSection(DocumentSectionId documentSectionId) {
return anchorController.scrollToAnchor(
documentSectionId.id,
documentSectionId.value,
duration: const Duration(milliseconds: 100),
// Overscroll a tiny bit. Otherwise it can sometimes happen that the
// section / element we scroll to is still not marked as currently read.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:fast_immutable_collections/fast_immutable_collections.dart';
part 'section_expansion.dart';

class DocumentSectionId extends Id {
const DocumentSectionId(super.id);
const DocumentSectionId(super.value);
}

/// Model for a table of contents with expandable sections and subsections.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import 'package:common_domain_models/common_domain_models.dart';

class ProductId extends Id {
const ProductId(super.id);
const ProductId(super.value);
}

abstract class PurchaseService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class EventDialogApi {
EventDialogApi(this._api);

Future<Course> loadCourse(CourseId courseId) async {
return (await _api.course.streamCourse(courseId.id).first)!;
return (await _api.course.streamCourse(courseId.value).first)!;
}

Future<void> createEvent(
Expand All @@ -33,7 +33,7 @@ class EventDialogApi {
// The 'createdOn' field will be added in the gateway because we use
// serverTimestamp().
createdOn: null,
groupID: command.courseId.id,
groupID: command.courseId.value,
groupType: GroupType.course,
eventType: command.eventType,
date: command.date,
Expand Down
2 changes: 1 addition & 1 deletion app/test/grades/grades_test_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ TestSubject subjectWith({
List<ConnectedCourse> connectedCourses = const [],
}) {
final idRes = id ?? SubjectId(randomAlpha(5));
final nameRes = name ?? idRes.id;
final nameRes = name ?? idRes.value;
final abbreviationRes = abbreviation ?? nameRes.substring(0, 2);
return TestSubject(
id: idRes,
Expand Down
Loading

0 comments on commit 99c7e29

Please sign in to comment.