Skip to content

Commit

Permalink
refactor(booking): extend DateRangeItem
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Dec 6, 2022
1 parent 285a7f9 commit 34d70fd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 34 deletions.
26 changes: 5 additions & 21 deletions lib/src/model/booking/booking.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,18 @@ import 'package:cabin_booking/utils/date_time_extension.dart';
import 'package:cabin_booking/utils/time_of_day_extension.dart';
import 'package:intl/intl.dart';

import '../date/date_ranger.dart';
import '../item.dart';
import '../date/date_range_item.dart';

abstract class _JsonFields {
static const description = 'de';
static const startDate = 'sd';
static const endDate = 'ed';
static const isLocked = 'il';
}

/// A booking item.
abstract class Booking extends Item with DateRanger {
abstract class Booking extends DateRangeItem {
/// The description used to visually identify this [Booking].
String? description;

@override
DateTime? startDate;

@override
DateTime? endDate;

/// Whether this [Booking] represents a locked time slot.
bool isLocked;

Expand All @@ -43,9 +34,9 @@ abstract class Booking extends Item with DateRanger {
/// Creates a new [Booking].
Booking({
super.id,
super.startDate,
super.endDate,
this.description,
this.startDate,
this.endDate,
this.isLocked = false,
this.cabinId,
this.recurringBookingId,
Expand All @@ -56,20 +47,13 @@ abstract class Booking extends Item with DateRanger {
/// Creates a new [Booking] from a JSON Map.
Booking.from(super.other)
: description = other[_JsonFields.description] as String?,
startDate = DateTime.tryParse(
other[_JsonFields.startDate] as String? ?? '',
),
endDate =
DateTime.tryParse(other[_JsonFields.endDate] as String? ?? ''),
isLocked = other[_JsonFields.isLocked] as bool,
super.from();

@override
Map<String, dynamic> toJson() => {
...super.toJson(),
_JsonFields.description: description,
_JsonFields.startDate: startDate?.toUtc().toIso8601String(),
_JsonFields.endDate: endDate?.toUtc().toIso8601String(),
_JsonFields.isLocked: isLocked,
};

Expand All @@ -85,9 +69,9 @@ abstract class Booking extends Item with DateRanger {
@override
Booking copyWith({
String? id,
String? description,
DateTime? startDate,
DateTime? endDate,
String? description,
bool? isLocked,
String? cabinId,
});
Expand Down
12 changes: 6 additions & 6 deletions lib/src/model/booking/recurring_booking.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class RecurringBooking extends Booking {

RecurringBooking({
super.id,
super.description,
super.startDate,
super.endDate,
super.description,
super.isLocked,
super.cabinId,
this.periodicity = Periodicity.weekly,
Expand Down Expand Up @@ -74,9 +74,9 @@ class RecurringBooking extends Booking {

return RecurringBooking(
id: booking.id,
description: booking.description,
startDate: booking.startDate,
endDate: booking.endDate,
description: booking.description,
isLocked: booking.isLocked,
cabinId: booking.cabinId,
periodicity: periodicity,
Expand All @@ -96,7 +96,7 @@ class RecurringBooking extends Booking {
_JsonFields.repeatEvery: repeatEvery,
if (method == RecurringBookingMethod.endDate)
_JsonFields.recurringEndDate:
_recurringEndDate?.toIso8601String().split('T').first
_recurringEndDate?.toUtc().toIso8601String()
else if (method == RecurringBookingMethod.occurrences)
_JsonFields.occurrences: _occurrences,
};
Expand Down Expand Up @@ -144,9 +144,9 @@ class RecurringBooking extends Booking {

SingleBooking asSingleBooking({bool linked = true}) => SingleBooking(
id: linked ? '$id-0' : (recurringBookingId ?? id),
description: description,
startDate: startDate,
endDate: endDate,
description: description,
isLocked: isLocked,
cabinId: cabinId,
recurringBookingId: linked ? id : null,
Expand Down Expand Up @@ -191,9 +191,9 @@ class RecurringBooking extends Booking {
@override
RecurringBooking copyWith({
String? id,
String? description,
DateTime? startDate,
DateTime? endDate,
String? description,
bool? isLocked,
String? cabinId,
Periodicity? periodicity,
Expand All @@ -203,9 +203,9 @@ class RecurringBooking extends Booking {
}) =>
RecurringBooking(
id: id ?? this.id,
description: description ?? this.description,
startDate: startDate ?? startDate,
endDate: endDate ?? endDate,
description: description ?? this.description,
isLocked: isLocked ?? this.isLocked,
cabinId: cabinId ?? this.cabinId,
periodicity: periodicity ?? this.periodicity,
Expand Down
10 changes: 5 additions & 5 deletions lib/src/model/booking/single_booking.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import 'booking.dart';
class SingleBooking extends Booking {
SingleBooking({
super.id,
super.description,
super.startDate,
super.endDate,
super.isLocked = false,
super.description,
super.isLocked,
super.cabinId,
super.recurringBookingId,
super.recurringNumber,
Expand All @@ -18,27 +18,27 @@ class SingleBooking extends Booking {
SingleBooking.fromBooking(Booking booking)
: super(
id: booking.id,
description: booking.description,
startDate: booking.startDate,
endDate: booking.endDate,
description: booking.description,
isLocked: booking.isLocked,
cabinId: booking.cabinId,
);

@override
SingleBooking copyWith({
String? id,
String? description,
DateTime? startDate,
DateTime? endDate,
String? description,
bool? isLocked,
String? cabinId,
}) =>
SingleBooking(
id: id ?? super.id,
description: description ?? this.description,
startDate: startDate ?? startDate,
endDate: endDate ?? endDate,
description: description ?? this.description,
isLocked: isLocked ?? this.isLocked,
cabinId: cabinId ?? this.cabinId,
);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/model/date/date_range_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class DateRangeItem extends Item with DateRanger {
@override
Map<String, dynamic> toJson() => {
...super.toJson(),
_JsonFields.startDate: startDate?.toIso8601String().split('T').first,
_JsonFields.endDate: endDate?.toIso8601String().split('T').first,
_JsonFields.startDate: startDate?.toUtc().toIso8601String(),
_JsonFields.endDate: endDate?.toUtc().toIso8601String(),
};

@override
Expand Down

0 comments on commit 34d70fd

Please sign in to comment.