Skip to content

Commit 3a55af2

Browse files
committed
Better handling of available events and modifying event types
1 parent 6c80960 commit 3a55af2

5 files changed

Lines changed: 329 additions & 138 deletions

File tree

frontend/lib/core/repositories/event_repository.dart

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ abstract class EventRepository {
1212
Future<void> notifyParticipants(int eventId, String message);
1313
Future<TicketType> createTicketType(Map<String, dynamic> data);
1414
Future<bool> deleteTicketType(int typeId);
15-
Future<TicketType> updateTicketType(int typeId, Map<String, dynamic> data);
1615
Future<List<Location>> getLocations();
1716
}
1817

@@ -33,23 +32,15 @@ class ApiEventRepository implements EventRepository {
3332
return (data as List).map((e) => Event.fromJson(e)).toList();
3433
}
3534

36-
@override
37-
Future<TicketType> updateTicketType(int typeId, Map<String, dynamic> data) async {
38-
final response = await _apiClient.put('/ticket-types/$typeId', data: data);
39-
return TicketType.fromJson(response);
40-
}
41-
4235
@override
4336
Future<List<Event>> getOrganizerEvents(int organizerId) async {
44-
final data =
45-
await _apiClient.get('/events', queryParams: {'organizer_id': organizerId});
37+
final data = await _apiClient.get('/events', queryParams: {'organizer_id': organizerId});
4638
return (data as List).map((e) => Event.fromJson(e)).toList();
4739
}
4840

4941
@override
5042
Future<List<TicketType>> getTicketTypesForEvent(int eventId) async {
51-
final data = await _apiClient
52-
.get('/ticket-types/', queryParams: {'event_id': eventId});
43+
final data = await _apiClient.get('/ticket-types/', queryParams: {'event_id': eventId});
5344
return (data as List).map((t) => TicketType.fromJson(t)).toList();
5445
}
5546

@@ -82,6 +73,8 @@ class ApiEventRepository implements EventRepository {
8273
return TicketType.fromJson(response);
8374
}
8475

76+
// REMOVED: updateTicketType method
77+
8578
@override
8679
Future<bool> deleteTicketType(int typeId) async {
8780
final response = await _apiClient.delete('/ticket-types/$typeId');

frontend/lib/presentation/organizer/cubit/event_form_cubit.dart

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class EventFormCubit extends Cubit<EventFormState> {
4343
try {
4444
emit(EventFormTicketTypesLoading());
4545
final ticketTypes = await _eventRepository.getTicketTypesForEvent(eventId);
46-
4746
emit(EventFormTicketTypesLoaded(ticketTypes));
4847
} on ApiException catch (e) {
4948
emit(EventFormError('Failed to load ticket types: ${e.message}'));
@@ -65,44 +64,23 @@ class EventFormCubit extends Cubit<EventFormState> {
6564
}
6665
}
6766

67+
/// Update event details only - ticket types are managed separately
6868
Future<void> updateEventWithTicketTypes(
6969
int eventId,
7070
Map<String, dynamic> eventData,
71-
List<TicketType> additionalTicketTypes
71+
List<TicketType> newTicketTypes // Only new ticket types to be created
7272
) async {
7373
try {
7474
emit(const EventFormSubmitting(locations: []));
7575

7676
// 1. Update the event details first
7777
await _eventRepository.updateEvent(eventId, eventData);
7878

79-
// 2. Get existing ticket types to compare
80-
final existingTicketTypes = await _eventRepository.getTicketTypesForEvent(eventId);
81-
final existingAdditionalTypes = existingTicketTypes
82-
.where((t) => (t.description ?? '') != "Standard Ticket")
83-
.toList();
84-
85-
// 3. Delete removed ticket types
86-
for (final existing in existingAdditionalTypes) {
87-
final stillExists = additionalTicketTypes.any((t) =>
88-
t.typeId != null && t.typeId == existing.typeId);
89-
90-
if (!stillExists && existing.typeId != null) {
91-
await _eventRepository.deleteTicketType(existing.typeId!);
92-
print('Deleted ticket type: ${existing.description}');
93-
}
94-
}
95-
96-
// 4. Create or update ticket types
97-
for (final ticketType in additionalTicketTypes) {
79+
// 2. Create only NEW ticket types (no deletion/updating of existing ones)
80+
for (final ticketType in newTicketTypes) {
9881
if (ticketType.typeId == null) {
99-
// Create new ticket type
10082
await _createSingleTicketType(eventId, ticketType);
10183
print('Created new ticket type: ${ticketType.description}');
102-
} else {
103-
// Update existing ticket type
104-
await _updateSingleTicketType(ticketType);
105-
print('Updated ticket type: ${ticketType.description}');
10684
}
10785
}
10886

@@ -114,31 +92,41 @@ class EventFormCubit extends Cubit<EventFormState> {
11492
}
11593
}
11694

117-
Future<void> _createSingleTicketType(int eventId, TicketType ticketType) async {
118-
if (ticketType.availableFrom == null) {
119-
throw Exception('Available from date is required for ticket type: ${ticketType.description}');
120-
}
121-
122-
await _eventRepository.createTicketType({
123-
'event_id': eventId,
124-
'description': ticketType.description ?? '',
125-
'max_count': ticketType.maxCount,
126-
'price': ticketType.price,
127-
'currency': ticketType.currency,
128-
'available_from': ticketType.availableFrom!.toIso8601String(),
129-
});
95+
bool canDeleteTicketType(TicketType ticketType) {
96+
if (ticketType.availableFrom == null) return false;
97+
return ticketType.availableFrom!.isAfter(DateTime.now());
13098
}
13199

132-
Future<void> _updateSingleTicketType(TicketType ticketType) async {
133-
if (ticketType.typeId == null) {
134-
throw Exception('Cannot update ticket type without ID');
100+
Future<void> deleteTicketType(int typeId, TicketType ticketType) async {
101+
try {
102+
// Check if deletion is allowed
103+
if (!canDeleteTicketType(ticketType)) {
104+
emit(EventFormError(
105+
'Cannot delete ticket type "${ticketType.description ?? ''}" - sales have already started or no availability date set.'
106+
));
107+
return;
108+
}
109+
110+
await _eventRepository.deleteTicketType(typeId);
111+
emit(EventFormTicketTypeDeleted());
112+
113+
if (ticketType.eventId != null) {
114+
await loadExistingTicketTypes(ticketType.eventId);
115+
}
116+
} on ApiException catch (e) {
117+
emit(EventFormError(e.message));
118+
} catch (e) {
119+
emit(EventFormError('Failed to delete ticket type: $e'));
135120
}
121+
}
136122

123+
Future<void> _createSingleTicketType(int eventId, TicketType ticketType) async {
137124
if (ticketType.availableFrom == null) {
138125
throw Exception('Available from date is required for ticket type: ${ticketType.description}');
139126
}
140127

141-
await _eventRepository.updateTicketType(ticketType.typeId!, {
128+
await _eventRepository.createTicketType({
129+
'event_id': eventId,
142130
'description': ticketType.description ?? '',
143131
'max_count': ticketType.maxCount,
144132
'price': ticketType.price,
@@ -151,32 +139,12 @@ class EventFormCubit extends Cubit<EventFormState> {
151139
try {
152140
await _createSingleTicketType(eventId, ticketType);
153141
emit(EventFormTicketTypeCreated());
154-
} on ApiException catch (e) {
155-
emit(EventFormError(e.message));
156-
} catch (e) {
157-
emit(EventFormError('Failed to create ticket type: $e'));
158-
}
159-
}
160142

161-
Future<void> updateTicketType(TicketType ticketType) async {
162-
try {
163-
await _updateSingleTicketType(ticketType);
164-
emit(EventFormTicketTypeUpdated());
143+
await loadExistingTicketTypes(eventId);
165144
} on ApiException catch (e) {
166145
emit(EventFormError(e.message));
167146
} catch (e) {
168-
emit(EventFormError('Failed to update ticket type: $e'));
169-
}
170-
}
171-
172-
Future<void> deleteTicketType(int typeId) async {
173-
try {
174-
await _eventRepository.deleteTicketType(typeId);
175-
emit(EventFormTicketTypeDeleted());
176-
} on ApiException catch (e) {
177-
emit(EventFormError(e.message));
178-
} catch (e) {
179-
emit(EventFormError('Failed to delete ticket type: $e'));
147+
emit(EventFormError('Failed to create ticket type: $e'));
180148
}
181149
}
182150
}

frontend/lib/presentation/organizer/cubit/event_form_state.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,4 @@ class EventFormTicketTypesLoaded extends EventFormState {
5656

5757
class EventFormTicketTypeCreated extends EventFormState {}
5858

59-
class EventFormTicketTypeUpdated extends EventFormState {}
60-
61-
class EventFormTicketTypeDeleted extends EventFormState {} // FIXED: Added missing body
59+
class EventFormTicketTypeDeleted extends EventFormState {}

0 commit comments

Comments
 (0)