Initial commit: Eventify frontend
This commit is contained in:
104
lib/features/events/models/event_models.dart
Normal file
104
lib/features/events/models/event_models.dart
Normal file
@@ -0,0 +1,104 @@
|
||||
// lib/features/events/models/event_models.dart
|
||||
class EventTypeModel {
|
||||
final int id;
|
||||
final String name;
|
||||
final String? iconUrl;
|
||||
|
||||
EventTypeModel({required this.id, required this.name, this.iconUrl});
|
||||
|
||||
factory EventTypeModel.fromJson(Map<String, dynamic> j) {
|
||||
return EventTypeModel(
|
||||
id: j['id'] as int,
|
||||
name: (j['event_type'] ?? j['name'] ?? '') as String,
|
||||
iconUrl: (j['event_type_icon'] ?? j['icon_url']) as String?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class EventImageModel {
|
||||
final bool isPrimary;
|
||||
final String image;
|
||||
|
||||
EventImageModel({required this.isPrimary, required this.image});
|
||||
|
||||
factory EventImageModel.fromJson(Map<String, dynamic> j) {
|
||||
return EventImageModel(
|
||||
isPrimary: j['is_primary'] == true,
|
||||
image: (j['image'] ?? '') as String,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class EventModel {
|
||||
final int id;
|
||||
final String name;
|
||||
final String? title;
|
||||
final String? description;
|
||||
final String startDate; // YYYY-MM-DD
|
||||
final String endDate;
|
||||
final String? startTime;
|
||||
final String? endTime;
|
||||
final String? pincode;
|
||||
final String? place;
|
||||
final bool isBookable;
|
||||
final int? eventTypeId;
|
||||
final String? thumbImg;
|
||||
final List<EventImageModel> images;
|
||||
|
||||
// NEW fields mapped from backend
|
||||
final String? importantInformation;
|
||||
final String? venueName;
|
||||
final String? eventStatus;
|
||||
final String? cancelledReason;
|
||||
|
||||
EventModel({
|
||||
required this.id,
|
||||
required this.name,
|
||||
this.title,
|
||||
this.description,
|
||||
required this.startDate,
|
||||
required this.endDate,
|
||||
this.startTime,
|
||||
this.endTime,
|
||||
this.pincode,
|
||||
this.place,
|
||||
this.isBookable = true,
|
||||
this.eventTypeId,
|
||||
this.thumbImg,
|
||||
this.images = const [],
|
||||
this.importantInformation,
|
||||
this.venueName,
|
||||
this.eventStatus,
|
||||
this.cancelledReason,
|
||||
});
|
||||
|
||||
factory EventModel.fromJson(Map<String, dynamic> j) {
|
||||
final imgs = <EventImageModel>[];
|
||||
if (j['images'] is List) {
|
||||
for (final im in j['images']) {
|
||||
if (im is Map<String, dynamic>) imgs.add(EventImageModel.fromJson(im));
|
||||
}
|
||||
}
|
||||
|
||||
return EventModel(
|
||||
id: j['id'] is int ? j['id'] as int : int.parse(j['id'].toString()),
|
||||
name: (j['name'] ?? '') as String,
|
||||
title: j['title'] as String?,
|
||||
description: j['description'] as String?,
|
||||
startDate: (j['start_date'] ?? '') as String,
|
||||
endDate: (j['end_date'] ?? '') as String,
|
||||
startTime: j['start_time'] as String?,
|
||||
endTime: j['end_time'] as String?,
|
||||
pincode: j['pincode'] as String?,
|
||||
place: (j['place'] ?? j['venue_name']) as String?,
|
||||
isBookable: j['is_bookable'] == null ? true : (j['is_bookable'] == true || j['is_bookable'].toString().toLowerCase() == 'true'),
|
||||
eventTypeId: j['event_type'] is int ? j['event_type'] as int : (j['event_type'] != null ? int.tryParse(j['event_type'].toString()) : null),
|
||||
thumbImg: j['thumb_img'] as String?,
|
||||
images: imgs,
|
||||
importantInformation: j['important_information'] as String?,
|
||||
venueName: j['venue_name'] as String?,
|
||||
eventStatus: j['event_status'] as String?,
|
||||
cancelledReason: j['cancelled_reason'] as String?,
|
||||
);
|
||||
}
|
||||
}
|
||||
70
lib/features/events/services/events_service.dart
Normal file
70
lib/features/events/services/events_service.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
// lib/features/events/services/events_service.dart
|
||||
import 'package:intl/intl.dart';
|
||||
import '../../../core/api/api_client.dart';
|
||||
import '../../../core/api/api_endpoints.dart';
|
||||
import '../models/event_models.dart';
|
||||
|
||||
class EventsService {
|
||||
final ApiClient _api = ApiClient();
|
||||
|
||||
/// Get event types (POST to /events/type-list/)
|
||||
Future<List<EventTypeModel>> getEventTypes() async {
|
||||
final res = await _api.post(ApiEndpoints.eventTypes);
|
||||
final list = <EventTypeModel>[];
|
||||
final data = res['event_types'] ?? res['event_types'] ?? res;
|
||||
if (data is List) {
|
||||
for (final e in data) {
|
||||
if (e is Map<String, dynamic>) list.add(EventTypeModel.fromJson(e));
|
||||
}
|
||||
} else if (res['event_types'] is List) {
|
||||
for (final e in res['event_types']) {
|
||||
list.add(EventTypeModel.fromJson(Map<String, dynamic>.from(e)));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/// Get events filtered by pincode (POST to /events/pincode-events/)
|
||||
/// Use pincode='all' to fetch all events.
|
||||
Future<List<EventModel>> getEventsByPincode(String pincode) async {
|
||||
final res = await _api.post(ApiEndpoints.eventsByPincode, body: {'pincode': pincode});
|
||||
final list = <EventModel>[];
|
||||
final events = res['events'] ?? res['data'] ?? [];
|
||||
if (events is List) {
|
||||
for (final e in events) {
|
||||
if (e is Map<String, dynamic>) list.add(EventModel.fromJson(Map<String, dynamic>.from(e)));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/// Event details
|
||||
Future<EventModel> getEventDetails(int eventId) async {
|
||||
final res = await _api.post(ApiEndpoints.eventDetails, body: {'event_id': eventId});
|
||||
return EventModel.fromJson(Map<String, dynamic>.from(res));
|
||||
}
|
||||
|
||||
/// Events by month and year for calendar (POST to /events/events-by-month-year/)
|
||||
/// Accepts month string and year int.
|
||||
/// Returns Map with 'dates' (list of YYYY-MM-DD) and 'date_events' (list with counts).
|
||||
Future<Map<String, dynamic>> getEventsByMonthYear(String month, int year) async {
|
||||
final res = await _api.post(ApiEndpoints.eventsByMonth, body: {'month': month, 'year': year});
|
||||
// expected keys: dates, total_number_of_events, date_events
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Convenience: get events for a specific date (YYYY-MM-DD)
|
||||
Future<List<EventModel>> getEventsForDate(String date) async {
|
||||
// Simplest approach: hit pincode-events with filter or hit events-by-month-year and then
|
||||
// query event-details for events of that date. Assuming backend doesn't provide direct endpoint,
|
||||
// we'll call eventsByPincode('all') and filter locally by date — acceptable for demo/small datasets.
|
||||
final all = await getEventsByPincode('all');
|
||||
return all.where((e) {
|
||||
try {
|
||||
return e.startDate == date || e.endDate == date || (DateTime.parse(e.startDate).isBefore(DateTime.parse(date)) && DateTime.parse(e.endDate).isAfter(DateTime.parse(date)));
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user