Skip to content

Commit

Permalink
partially implemented new restaurants api
Browse files Browse the repository at this point in the history
Co-authored-by: AugustoVSoares <[email protected]>
  • Loading branch information
HenriqueSFernandes and AugustoVSoares committed Nov 16, 2024
1 parent 9e8f5c0 commit ece6d19
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 6 deletions.
86 changes: 84 additions & 2 deletions packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -16,6 +21,22 @@ class RestaurantFetcher {
// List the Restaurant sheet names in the Google Sheets Document
final List<String> restaurantSheets = ['Cantina'];

// TODO: remove later
void printRestaurants(List<Restaurant> 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(
Expand Down Expand Up @@ -44,6 +65,65 @@ class RestaurantFetcher {
);
}

Future<List<Restaurant>> getSASUPRestaurants() async {
// TODO: change to accomodate changes for the new UI.
final upMenus = UPMenusApi();
final establishments = await upMenus.establishments.list();
final restaurants = <Restaurant>[];

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 = <Meal>[];
// 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<String> sigarraMenuEndpoints = [
'${NetworkRouter.getBaseUrl('feup')}CANTINA.EMENTASHOW',
];
Expand All @@ -64,7 +144,9 @@ class RestaurantFetcher {
}

Future<List<Restaurant>> 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
Expand All @@ -90,7 +172,7 @@ class RestaurantFetcher {
)
..insert(0, gSheetsRestaurant);
}

// printRestaurants(restaurants);
return restaurants;
}
}
16 changes: 12 additions & 4 deletions packages/uni_app/lib/model/providers/lazy/restaurant_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ import 'package:uni/model/providers/state_providers.dart';
class RestaurantProvider extends StateProviderNotifier<List<Restaurant>> {
RestaurantProvider() : super(cacheDuration: const Duration(days: 1));

// @override
// Future<List<Restaurant>> loadFromStorage(
// StateProviders stateProviders,
// ) async {
// final restaurantDb = RestaurantDatabase();
// final restaurants = await restaurantDb.getRestaurants();
// return restaurants;
// }

@override
Future<List<Restaurant>> loadFromStorage(
StateProviders stateProviders,
) async {
final restaurantDb = RestaurantDatabase();
final restaurants = await restaurantDb.getRestaurants();
return restaurants;
return loadFromRemote(stateProviders);
}

@override
Expand All @@ -26,6 +33,7 @@ class RestaurantProvider extends StateProviderNotifier<List<Restaurant>> {
final db = RestaurantDatabase();
unawaited(db.saveIfPersistentSession(restaurants));

return filterPastMeals(restaurants);
// return filterPastMeals(restaurants);
return restaurants;
}
}

0 comments on commit ece6d19

Please sign in to comment.