Restaurant implements Model, NameProtocol, AddressProtocol { public Map<Integer, List<OpeningHours>> openingHours() { return Arrays.stream(DayOfWeek.values()) .map(dow -> { Query<EntityHandle<WorkingHoursChanged>> scope = QueryFactory.and(equal(WorkingHoursChanged .REFERENCE_ID, getId()), equal(WorkingHoursChanged.DAY_OF_WEEK, dow)); Query<EntityHandle<WorkingHoursChanged>> query = scoped(scope, max(WorkingHoursChanged.TIMESTAMP)); try (ResultSet<EntityHandle<WorkingHoursChanged>> resultSet = getRepository().query(WorkingHoursChanged.class, query)) { return resultSet.uniqueResult().get(); } }) .collect(Collectors.toMap(e -> e.dayOfWeek().getValue(), WorkingHoursChanged::openDuring)); } protected  Restaurant(Repository repository, UUID id); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); static Optional<Restaurant> lookup(Repository repository, UUID id); static ModelCollectionQuery<Restaurant> within10km(Address address); static ModelCollectionQuery<Restaurant> openAt(Date date); static Collection<Restaurant> query(Repository repository, ModelCollectionQuery<Restaurant> query); Map<Integer, List<OpeningHours>> openingHours(); Collection<MenuItem> menu();  }
@Test @SneakyThrows public void initialization() { Restaurant kyzock = repository.publish(kyzockRegistration()).get(); Map<Integer, List<OpeningHours>> openingHours = kyzock.openingHours(); for (DayOfWeek dayOfWeek : DayOfWeek.values()) { assertEquals(openingHours.get(dayOfWeek.getValue()).get(0).from(), new OpeningHours.Time(11, 30)); assertEquals(openingHours.get(dayOfWeek.getValue()).get(0).till(), new OpeningHours.Time(19, 00)); } }
@Test @SneakyThrows public void changeOfHours() { Restaurant kyzock = repository.publish(kyzockRegistration()).get(); repository.publish(new ChangeRestaurantHours(kyzock, DayOfWeek.FRIDAY, Collections.singletonList(new OpeningHours(11, 30, 20, 00)))).get(); repository.publish(new ChangeRestaurantHours(kyzock, DayOfWeek.SATURDAY, Collections.emptyList())).get(); repository.publish(new ChangeRestaurantHours(kyzock, DayOfWeek.SUNDAY, Collections.emptyList())).get(); Map<Integer, List<OpeningHours>> openingHours = kyzock.openingHours(); assertEquals(openingHours.get(DayOfWeek.FRIDAY.getValue()).get(0).till(), new OpeningHours.Time(20, 00)); assertTrue(openingHours.get(DayOfWeek.SATURDAY.getValue()).isEmpty()); assertTrue(openingHours.get(DayOfWeek.SUNDAY.getValue()).isEmpty()); }