Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • meissnerfl73755/softa
1 result
Show changes
Commits on Source (2)
...@@ -4,6 +4,7 @@ import ohm.softa.a11.openmensa.OpenMensaAPI; ...@@ -4,6 +4,7 @@ import ohm.softa.a11.openmensa.OpenMensaAPI;
import ohm.softa.a11.openmensa.OpenMensaAPIService; import ohm.softa.a11.openmensa.OpenMensaAPIService;
import ohm.softa.a11.openmensa.model.Canteen; import ohm.softa.a11.openmensa.model.Canteen;
import ohm.softa.a11.openmensa.model.PageInfo; import ohm.softa.a11.openmensa.model.PageInfo;
import retrofit2.HttpException;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -53,27 +54,52 @@ public class App { ...@@ -53,27 +54,52 @@ public class App {
} }
private static void printCanteens() { private static void printCanteens() {
System.out.print("Fetching canteens ["); /* fetch all canteens and print them to STDOUT
/* TODO fetch all canteens and print them to STDOUT
* at first get a page without an index to be able to extract the required pagination information * at first get a page without an index to be able to extract the required pagination information
* afterwards you can iterate the remaining pages * afterwards you can iterate the remaining pages
* keep in mind that you should await the process as the user has to select canteen with a specific id */ * keep in mind that you should await the process as the user has to select canteen with a specific id */
OpenMensaAPI api = OpenMensaAPIService.getInstance().getOpenMensaAPI(); var response = openMensaAPI.getCanteens();
var response = api.getCanteens();
try { try {
var pageInfo = PageInfo.extractFromResponse(response.get()); var pageInfo = PageInfo.extractFromResponse(response.get());
var firstCanteenPage = response.get().body(); var firstPage = response.get().body();
var N = pageInfo.getTotalCountOfPages(); var N = pageInfo.getTotalCountOfPages();
var canteens = IntStream.range(2, N).parallel() /*var canteens = IntStream.range(2, N).parallel()
.mapToObj((n) -> api.getCanteens(n).join()) .mapToObj((n) -> api.getCanteens(n).join())
.flatMap(Collection::stream) .flatMap(Collection::stream)
.collect(Collectors.toList()); .collect(Collectors.toList());*/
canteens.addAll(0, firstCanteenPage);
canteens.sort(Comparator.comparingInt(Canteen::getId));
//canteens = canteens.thenCombine(api.getCanteens(Integer.parseInt(pageN.get())), (l1, l2) -> Stream.concat(l1.parallelStream(), l2.parallelStream()))
canteens.forEach((c) -> System.out.println(c.toString()));
final var id = CompletableFuture.completedFuture(firstPage);
var request = IntStream
.range(2, N + 1) // one-indexed
.mapToObj(openMensaAPI::getCanteens)
.reduce(
id,
(f1, f2) -> f1.thenCombine(
f2,
// lambda combining two lists
// cannot move to variable, because no templating and no type inference for lambdas
// fuck java
(l1, l2) -> Stream
.concat(l1.stream(), l2.stream())
.collect(Collectors.toList()))
)
.thenAccept(canteens -> {
canteens.stream()
.sorted(Comparator.comparingInt(Canteen::getId))
.forEach(System.out::println);
System.out.println(pageInfo.getTotalCountOfItems() + " <- Header | actual -> " + canteens.size());
})
.exceptionally(e -> {
do {
System.out.println(e.getMessage());
e = e.getCause();
} while (e != null);
return null;
});
// lazy -> eager
request.join();
} catch (InterruptedException | ExecutionException | NumberFormatException e) { } catch (InterruptedException | ExecutionException | NumberFormatException e) {
e.printStackTrace(); e.printStackTrace();
System.out.println("WHYYYYYYYYYYy!!!!"); System.out.println("WHYYYYYYYYYYy!!!!");
...@@ -84,6 +110,21 @@ public class App { ...@@ -84,6 +110,21 @@ public class App {
/* TODO fetch all meals for the currently selected canteen /* TODO fetch all meals for the currently selected canteen
* to avoid errors retrieve at first the state of the canteen and check if the canteen is opened at the selected day * to avoid errors retrieve at first the state of the canteen and check if the canteen is opened at the selected day
* don't forget to check if a canteen was selected previously! */ * don't forget to check if a canteen was selected previously! */
if (currentCanteenId == -1) {
System.out.println("Error: no canteen selected");
return;
}
openMensaAPI
.getMeals(currentCanteenId, dateFormat.format(currentDate.getTime()))
.exceptionally((e) -> {
System.out.println(e.getMessage());
if (e instanceof HttpException) System.out.println(((HttpException) e).response());
return List.of();
})
.thenAccept(meals -> meals.forEach(System.out::println))
.join();
} }
/** /**
......