import 'dart:convert'; import 'package:manager_api_new/api.dart'; class Agenda { List events; Agenda({required this.events}); factory Agenda.fromJson(String jsonString) { final List jsonList = json.decode(jsonString); List events = []; for (var eventData in jsonList) { try { events.add(EventAgenda.fromJson(eventData)); } catch(e) { print("Erreur lors du parsing du json : ${e.toString()}"); } } return Agenda(events: events); } } class EventAgenda { String? name; String? description; String? type; DateTime? dateAdded; DateTime? dateFrom; DateTime? dateTo; String? dateHour; EventAddress? address; String? website; String? phone; String? idVideoYoutube; String? videoLink; String? videoResourceUrl; String? email; String? image; EventAgenda({ required this.name, required this.description, required this.type, required this.dateAdded, required this.dateFrom, required this.dateTo, required this.dateHour, required this.address, required this.website, required this.phone, required this.idVideoYoutube, this.videoLink, this.videoResourceUrl, required this.email, required this.image, }); factory EventAgenda.fromDto(EventAgendaDTO dto, String language) { String? pickTranslation(List? list) { if (list == null || list.isEmpty) return null; return (list.firstWhere( (t) => t.language == language, orElse: () => list.first, )).value; } EventAddress? address; final a = dto.address; if (a != null) { final coords = a.geometry?.coordinates as List?; address = EventAddress( address: a.address, lat: coords != null && coords.length >= 2 ? coords[1] : null, lng: coords != null && coords.length >= 2 ? coords[0] : null, zoom: a.zoom, placeId: null, name: null, streetNumber: a.streetNumber, streetName: a.streetName, streetNameShort: null, city: a.city, state: a.state, stateShort: null, postCode: a.postCode, country: a.country, countryShort: null, ); } return EventAgenda( name: pickTranslation(dto.label), description: pickTranslation(dto.description), type: dto.type, dateAdded: dto.dateAdded, dateFrom: dto.dateFrom, dateTo: dto.dateTo, dateHour: null, address: address, website: dto.website, phone: dto.phone, idVideoYoutube: dto.idVideoYoutube, videoLink: dto.videoLink, videoResourceUrl: dto.videoResource?.url, email: dto.email, image: dto.resource?.url, ); } factory EventAgenda.fromJson(Map json) { return EventAgenda( name: json['name'], description: json['description'], type: json['type'] is !bool ? json['type'] : null, dateAdded: json['date_added'] != null && json['date_added'].isNotEmpty ? DateTime.parse(json['date_added']) : null, dateFrom: json['date_from'] != null && json['date_from'].isNotEmpty ? DateTime.parse(json['date_from']) : null, dateTo: json['date_to'] != null && json['date_to'].isNotEmpty ? DateTime.parse(json['date_to']) : null, dateHour: json['date_hour'], address: json['address'] is !bool ? EventAddress.fromJson(json['address']) : null, website: json['website'], phone: json['phone'], idVideoYoutube: json['id_video_youtube'], email: json['email'], image: json['image'], ); } } class EventAddress { String? address; dynamic lat; dynamic lng; int? zoom; String? placeId; String? name; String? streetNumber; String? streetName; String? streetNameShort; String? city; String? state; String? stateShort; String? postCode; String? country; String? countryShort; EventAddress({ required this.address, required this.lat, required this.lng, required this.zoom, required this.placeId, required this.name, required this.streetNumber, required this.streetName, required this.streetNameShort, required this.city, required this.state, required this.stateShort, required this.postCode, required this.country, required this.countryShort, }); factory EventAddress.fromJson(Map json) { return EventAddress( address: json['address'], lat: json['lat'], lng: json['lng'], zoom: json['zoom'], placeId: json['place_id'], name: json['name'], streetNumber: json['street_number'], streetName: json['street_name'], streetNameShort: json['street_name_short'], city: json['city'], state: json['state'], stateShort: json['state_short'], postCode: json['post_code'], country: json['country'], countryShort: json['country_short'], ); } }