feat: add template id to AvailibityModel to indicate if a template was used for creation

This commit is contained in:
Freek van de Ven 2024-07-03 13:06:32 +02:00 committed by FlutterJoey
parent 6748a2440c
commit 61bc0a8b53
3 changed files with 20 additions and 2 deletions

View file

@ -8,6 +8,7 @@ class AvailabilityModel {
required this.endDate, required this.endDate,
required this.breaks, required this.breaks,
this.id, this.id,
this.templateId,
}); });
/// the identifier for this availability /// the identifier for this availability
@ -16,6 +17,10 @@ class AvailabilityModel {
/// The uniquely identifiable string for who or what the /// The uniquely identifiable string for who or what the
final String userId; final String userId;
/// The identifier of the [AvailabilityTemplateModel] that this availability
/// is associated with if any.
final String? templateId;
/// The from date of this availability. /// The from date of this availability.
/// ///
/// [startDate] will always have to be before the end date. /// [startDate] will always have to be before the end date.
@ -34,6 +39,7 @@ class AvailabilityModel {
AvailabilityModel copyWith({ AvailabilityModel copyWith({
String? id, String? id,
String? userId, String? userId,
String? templateId,
DateTime? startDate, DateTime? startDate,
DateTime? endDate, DateTime? endDate,
List<AvailabilityBreakModel>? breaks, List<AvailabilityBreakModel>? breaks,
@ -41,6 +47,7 @@ class AvailabilityModel {
AvailabilityModel( AvailabilityModel(
id: id ?? this.id, id: id ?? this.id,
userId: userId ?? this.userId, userId: userId ?? this.userId,
templateId: templateId ?? this.templateId,
startDate: startDate ?? this.startDate, startDate: startDate ?? this.startDate,
endDate: endDate ?? this.endDate, endDate: endDate ?? this.endDate,
breaks: breaks ?? this.breaks, breaks: breaks ?? this.breaks,

View file

@ -70,7 +70,12 @@ class AvailabilityTemplateModel {
/// applies the template to a range of dates /// applies the template to a range of dates
List<AvailabilityModel> apply(DateTime start, DateTime end) => List<AvailabilityModel> apply(DateTime start, DateTime end) =>
templateData.apply(userId: userId, start: start, end: end); templateData.apply(
userId: userId,
start: start,
end: end,
templateId: id,
);
} }
/// Used as the key for defining week-based templates /// Used as the key for defining week-based templates
@ -124,6 +129,7 @@ abstract interface class TemplateData {
required String userId, required String userId,
required DateTime start, required DateTime start,
required DateTime end, required DateTime end,
required String? templateId,
}); });
/// Serialize the template to representational data /// Serialize the template to representational data
@ -199,9 +205,9 @@ class WeekTemplateData implements TemplateData {
required String userId, required String userId,
required DateTime start, required DateTime start,
required DateTime end, required DateTime end,
String? templateId,
}) { }) {
var dates = _getDatesBetween(start, end); var dates = _getDatesBetween(start, end);
return [ return [
for (var date in dates) for (var date in dates)
if (data.containsKey(WeekDay.fromDateTime(date))) if (data.containsKey(WeekDay.fromDateTime(date)))
@ -209,6 +215,7 @@ class WeekTemplateData implements TemplateData {
start: date, start: date,
end: date, end: date,
userId: userId, userId: userId,
templateId: templateId,
), ),
]; ];
} }
@ -266,6 +273,7 @@ class DayTemplateData implements TemplateData {
required String userId, required String userId,
required DateTime start, required DateTime start,
required DateTime end, required DateTime end,
String? templateId,
}) { }) {
var dates = _getDatesBetween(start, end); var dates = _getDatesBetween(start, end);
@ -273,6 +281,7 @@ class DayTemplateData implements TemplateData {
for (var date in dates) ...[ for (var date in dates) ...[
AvailabilityModel( AvailabilityModel(
userId: userId, userId: userId,
templateId: templateId,
startDate: date.mergeTime(startTime), startDate: date.mergeTime(startTime),
endDate: date.mergeTime(endTime), endDate: date.mergeTime(endTime),
breaks: [ breaks: [

View file

@ -30,6 +30,7 @@ void main() {
var sunday = createTemplate(7); var sunday = createTemplate(7);
var sut = AvailabilityTemplateModel( var sut = AvailabilityTemplateModel(
id: "",
userId: "", userId: "",
name: "", name: "",
color: 0, color: 0,
@ -142,6 +143,7 @@ void main() {
} }
var weektemplate = AvailabilityTemplateModel( var weektemplate = AvailabilityTemplateModel(
id: "id",
userId: "1", userId: "1",
name: "test", name: "test",
color: 0xFFAABBCC, color: 0xFFAABBCC,