From ece6d191d08046b2ea91e9817bba1f8689e162da Mon Sep 17 00:00:00 2001 From: HenriqueSFernandes Date: Sat, 16 Nov 2024 20:16:35 +0000 Subject: [PATCH] partially implemented new restaurants api Co-authored-by: AugustoVSoares --- .../fetchers/restaurant_fetcher.dart | 86 ++++++++++++++++++- .../providers/lazy/restaurant_provider.dart | 16 +++- 2 files changed, 96 insertions(+), 6 deletions(-) diff --git a/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart b/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart index 979759046..6569f471b 100644 --- a/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart +++ b/packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart @@ -1,7 +1,12 @@ +import 'dart:convert'; + import 'package:uni/controller/networking/network_router.dart'; import 'package:uni/controller/parsers/parser_restaurants.dart'; +import 'package:uni/model/entities/meal.dart'; import 'package:uni/model/entities/restaurant.dart'; +import 'package:uni/model/utils/day_of_week.dart'; import 'package:uni/session/flows/base/session.dart'; +import 'package:up_menus/up_menus.dart'; /// Class for fetching the menu class RestaurantFetcher { @@ -16,6 +21,22 @@ class RestaurantFetcher { // List the Restaurant sheet names in the Google Sheets Document final List restaurantSheets = ['Cantina']; + // TODO: remove later + void printRestaurants(List restaurants) { + for (final restaurant in restaurants) { + print(restaurant.id); + print(restaurant.name); + print(restaurant.reference); + final meals = restaurant.meals; + meals.forEach((day, mealList) { + print(day); + for (final meal in mealList) { + print(' - ${meal.name}'); + } + }); + } + } + // Generate the Gsheets endpoints list based on a list of sheets String buildGSheetsEndpoint(String sheet) { return Uri.encodeFull( @@ -44,6 +65,65 @@ class RestaurantFetcher { ); } + Future> getSASUPRestaurants() async { + // TODO: change to accomodate changes for the new UI. + final upMenus = UPMenusApi(); + final establishments = await upMenus.establishments.list(); + final restaurants = []; + + final icbas = await upMenus.establishments.get(4); + print(icbas.toJson()); + + print("74"); + // For every establishement... + for (final establishment in establishments) { + // Get the menu for the current week + if (establishment.dayMenu == false) continue; + final dayMenus = + (await upMenus.dayMenus.get(establishment.id, Period.lunch)) + .followedBy( + await upMenus.dayMenus.get(establishment.id, Period.dinner), + ) + .followedBy( + await upMenus.dayMenus.get(establishment.id, Period.snackBar), + ) + .followedBy( + await upMenus.dayMenus.get(establishment.id, Period.breakfast), + ); + print("89"); + print("${establishment.namePt} id: ${establishment.id}"); + final meals = []; + // For every day... + print("93"); + for (final dayMenu in dayMenus) { + print("94"); + // And for every dish... + for (final dish in dayMenu.dishes) { + // Extract the information about the meal. + print("96"); + meals.add( + Meal( + dish.dishType.namePt, + dish.dish.namePt, + parseDateTime(dayMenu.day), + dayMenu.day, + ), + ); + } + } + print("110"); + + restaurants.add( + Restaurant(establishment.id, establishment.namePt, '', meals: meals), + ); + print("115"); + printRestaurants(restaurants); + } + print("hereee"); + print(restaurants); + return restaurants; + } + final List sigarraMenuEndpoints = [ '${NetworkRouter.getBaseUrl('feup')}CANTINA.EMENTASHOW', ]; @@ -64,7 +144,9 @@ class RestaurantFetcher { } Future> getRestaurants(Session session) async { - final restaurants = await fetchSigarraRestaurants(session); + final restaurants = await getSASUPRestaurants(); + print(restaurants); + // final restaurants = await fetchSigarraRestaurants(session); // Check for restaurants without associated meals and attempt to parse them // from GSheets @@ -90,7 +172,7 @@ class RestaurantFetcher { ) ..insert(0, gSheetsRestaurant); } - + // printRestaurants(restaurants); return restaurants; } } diff --git a/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart b/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart index f6f9599f7..258633ae2 100644 --- a/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart +++ b/packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart @@ -9,13 +9,20 @@ import 'package:uni/model/providers/state_providers.dart'; class RestaurantProvider extends StateProviderNotifier> { RestaurantProvider() : super(cacheDuration: const Duration(days: 1)); + // @override + // Future> loadFromStorage( + // StateProviders stateProviders, + // ) async { + // final restaurantDb = RestaurantDatabase(); + // final restaurants = await restaurantDb.getRestaurants(); + // return restaurants; + // } + @override Future> loadFromStorage( StateProviders stateProviders, ) async { - final restaurantDb = RestaurantDatabase(); - final restaurants = await restaurantDb.getRestaurants(); - return restaurants; + return loadFromRemote(stateProviders); } @override @@ -26,6 +33,7 @@ class RestaurantProvider extends StateProviderNotifier> { final db = RestaurantDatabase(); unawaited(db.saveIfPersistentSession(restaurants)); - return filterPastMeals(restaurants); + // return filterPastMeals(restaurants); + return restaurants; } }