import 'package:amplify_flutter/amplify_flutter.dart'; import 'package:amplify_trips_planner/models/Trip.dart'; import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; final tripsDataStoreServiceProvider = Provider((ref) { final service = TripsDataStoreService(); return service; }); class TripsDataStoreService { TripsDataStoreService(); Stream> listenToTrips() { return Amplify.DataStore.observeQuery( Trip.classType, sortBy: [Trip.STARTDATE.ascending()], ) .map((event) => event.items .where((element) => element.endDate.getDateTime().isAfter(DateTime.now())) .toList()) .handleError( (error) { debugPrint('listenToTrips: A Stream error happened'); }, ); } Stream> listenToPastTrips() { return Amplify.DataStore.observeQuery( Trip.classType, sortBy: [Trip.STARTDATE.ascending()], ) .map((event) => event.items .where((element) => element.endDate.getDateTime().isBefore(DateTime.now())) .toList()) .handleError( (error) { debugPrint('listenToTrips: A Stream error happened'); }, ); } Stream getTripStream(String id) { final tripStream = Amplify.DataStore.observeQuery(Trip.classType, where: Trip.ID.eq(id)) .map((event) => event.items.toList().single); return tripStream; } Future addTrip(Trip trip) async { try { await Amplify.DataStore.save(trip); } on Exception catch (error) { debugPrint(error.toString()); } } Future deleteTrip(Trip trip) async { try { await Amplify.DataStore.delete(trip); } on Exception catch (error) { debugPrint(error.toString()); } } Future updateTrip(Trip updatedTrip) async { try { final tripsWithId = await Amplify.DataStore.query( Trip.classType, where: Trip.ID.eq(updatedTrip.id), ); final oldTrip = tripsWithId.first; final newTrip = oldTrip.copyWith( id: oldTrip.id, tripName: updatedTrip.tripName, destination: updatedTrip.destination, startDate: updatedTrip.startDate, endDate: updatedTrip.endDate, tripImageKey: updatedTrip.tripImageKey, tripImageUrl: updatedTrip.tripImageUrl, ); await Amplify.DataStore.save(newTrip); } on Exception catch (error) { debugPrint(error.toString()); } } }