Skip to content

Commit

Permalink
refactor(cabin): expose BookingCollection from constructor (#178)
Browse files Browse the repository at this point in the history
* refactor(cabin): expose `BookingCollection` from constructor

Continues #175

* test(cabin): adapt use-cases
  • Loading branch information
albertms10 committed Jan 14, 2023
1 parent 0d4da79 commit 9d4e53d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 43 deletions.
20 changes: 5 additions & 15 deletions lib/src/model/cabin/cabin.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import '../booking/booking.dart';
import '../booking/recurring_booking.dart';
import '../booking/single_booking.dart';
import '../booking_collection.dart';
import '../item.dart';
import 'cabin_elements.dart';
Expand All @@ -23,14 +21,9 @@ class Cabin extends Item {
super.id,
this.number = 0,
CabinElements? elements,
Set<SingleBooking>? bookings,
Set<RecurringBooking>? recurringBookings,
}) : bookingCollection = BookingCollection(
bookings: bookings,
recurringBookings: recurringBookings,
) {
this.elements = elements ?? CabinElements();
}
BookingCollection? bookingCollection,
}) : elements = elements ?? CabinElements(),
bookingCollection = bookingCollection ?? BookingCollection();

/// Creates a new [Cabin] from a JSON Map.
Cabin.fromJson(super.other)
Expand Down Expand Up @@ -62,16 +55,13 @@ class Cabin extends Item {
String? id,
int? number,
CabinElements? elements,
Set<SingleBooking>? bookings,
Set<RecurringBooking>? recurringBookings,
BookingCollection? bookingCollection,
}) =>
Cabin(
id: id ?? this.id,
number: number ?? this.number,
elements: elements ?? this.elements,
bookings: bookings ?? bookingCollection.bookings,
recurringBookings:
recurringBookings ?? bookingCollection.recurringBookings,
bookingCollection: bookingCollection ?? this.bookingCollection,
);

@override
Expand Down
59 changes: 31 additions & 28 deletions test/model/cabin/cabin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ void main() {
lecterns: 2,
tables: 1,
),
bookings: {},
recurringBookings: {},
bookingCollection: BookingCollection(
bookings: {},
recurringBookings: {},
),
),
);
});
Expand Down Expand Up @@ -76,12 +78,14 @@ void main() {
lecterns: 2,
tables: 1,
),
bookings: {
SingleBooking(id: 'booking-id'),
},
recurringBookings: {
RecurringBooking(id: 'recurring-booking-id', occurrences: 1),
},
bookingCollection: BookingCollection(
bookings: {
SingleBooking(id: 'booking-id'),
},
recurringBookings: {
RecurringBooking(id: 'recurring-booking-id', occurrences: 1),
},
),
);
expect(cabin, cabin.copyWith());
expect(identical(cabin, cabin.copyWith()), isFalse);
Expand All @@ -100,40 +104,39 @@ void main() {
lecterns: 2,
tables: 1,
),
bookings: {
SingleBooking(id: 'booking-id'),
},
recurringBookings: {
RecurringBooking(id: 'recurring-booking-id', occurrences: 1),
},
bookingCollection: BookingCollection(
bookings: {
SingleBooking(id: 'booking-id'),
},
recurringBookings: {
RecurringBooking(id: 'recurring-booking-id', occurrences: 1),
},
),
);
final newCabinElements = CabinElements(
pianos: const [Piano(brand: 'Bösendorfer', model: 'Imperial')],
lecterns: 1,
tables: 3,
);
final newBookings = {
SingleBooking(id: 'booking-id-1'),
SingleBooking(id: 'booking-id-2'),
};
final newRecurringBookings = {
RecurringBooking(id: 'recurring-booking-id-1', occurrences: 2),
};
final newBookingCollection = BookingCollection(
bookings: {
SingleBooking(id: 'booking-id-1'),
SingleBooking(id: 'booking-id-2'),
},
recurringBookings: {
RecurringBooking(id: 'recurring-booking-id-1', occurrences: 2),
},
);
final copiedCabin = cabin.copyWith(
id: 'copied-cabin',
number: 2,
elements: newCabinElements,
bookings: newBookings,
recurringBookings: newRecurringBookings,
bookingCollection: newBookingCollection,
);
expect(copiedCabin.id, 'copied-cabin');
expect(copiedCabin.number, 2);
expect(copiedCabin.elements, newCabinElements);
expect(copiedCabin.bookingCollection.bookings, newBookings);
expect(
copiedCabin.bookingCollection.recurringBookings,
newRecurringBookings,
);
expect(copiedCabin.bookingCollection, newBookingCollection);
},
);
});
Expand Down

0 comments on commit 9d4e53d

Please sign in to comment.