use of com.querydsl.core.Tuple in project Settler by EmhyrVarEmreis.
the class CategoryService method getCategoriesWithValue.
public ResponseEntity<List<CategoryWithValueDTO>> getCategoriesWithValue(Long userId) {
if (Objects.isNull(userId) || userId < 0) {
userId = Security.currentUser().getId();
}
QCategory category = QCategory.category;
QTransaction transaction = QTransaction.transaction;
List<Tuple> fetch = new JPAQuery<>(entityManager).from(category, transaction).select(category, transaction.value.sum()).where(transaction.creator.id.eq(userId)).where(transaction.categories.contains(category)).groupBy(category.code, category.description).orderBy(transaction.value.sum().desc()).fetch();
ModelMapper preparedModelMapper = getModelMapper();
List<CategoryWithValueDTO> categoryWithValueDTOList = new ArrayList<>(fetch.size());
for (Tuple tuple : fetch) {
Category c = tuple.get(category);
Double d = tuple.get(transaction.value.sum());
if (Objects.nonNull(c)) {
categoryWithValueDTOList.add(new CategoryWithValueDTO(userId, preparedModelMapper.map(c, CategoryDTO.class), d));
}
}
return new ResponseEntity<>(categoryWithValueDTOList, HttpStatus.OK);
}
use of com.querydsl.core.Tuple in project tutorials by eugenp.
the class QueryDSLIntegrationTest method whenGroupingByTitle_thenReturnsTuples.
@Test
public void whenGroupingByTitle_thenReturnsTuples() {
QBlogPost blogPost = QBlogPost.blogPost;
NumberPath<Long> count = Expressions.numberPath(Long.class, "c");
List<Tuple> userTitleCounts = queryFactory.select(blogPost.title, blogPost.id.count().as(count)).from(blogPost).groupBy(blogPost.title).orderBy(count.desc()).fetch();
assertEquals("Hello World!", userTitleCounts.get(0).get(blogPost.title));
assertEquals(new Long(2), userTitleCounts.get(0).get(count));
assertEquals("My Second Post", userTitleCounts.get(1).get(blogPost.title));
assertEquals(new Long(1), userTitleCounts.get(1).get(count));
}
use of com.querydsl.core.Tuple in project querydsl by querydsl.
the class GroupByIterateTest method map22.
@Test
public void map22() {
CloseableIterator<Map<Integer, String>> results = MAP2_RESULTS.transform(groupBy(postId).iterate(map(commentId, commentText)));
List<Map<Integer, String>> actual = IteratorAdapter.asList(results);
Object commentId = null;
Map<Integer, String> comments = null;
List<Map<Integer, String>> expected = new LinkedList<Map<Integer, String>>();
for (Iterator<Tuple> iterator = MAP2_RESULTS.iterate(); iterator.hasNext(); ) {
Tuple tuple = iterator.next();
Object[] array = tuple.toArray();
if (comments == null || !(commentId == array[0] || commentId != null && commentId.equals(array[0]))) {
comments = new LinkedHashMap<Integer, String>();
expected.add(comments);
}
commentId = array[0];
@SuppressWarnings("unchecked") Pair<Integer, String> pair = (Pair<Integer, String>) array[1];
comments.put(pair.getFirst(), pair.getSecond());
}
assertEquals(expected.toString(), actual.toString());
}
use of com.querydsl.core.Tuple in project querydsl by querydsl.
the class GroupByIterateTest method map4.
@Test
public void map4() {
CloseableIterator<Map<Map<Integer, String>, String>> results = MAP4_RESULTS.transform(groupBy(postId).iterate(map(map(postId, commentText), postName)));
List<Map<Map<Integer, String>, String>> actual = IteratorAdapter.asList(results);
Object commentId = null;
Map<Map<Integer, String>, String> comments = null;
List<Map<Map<Integer, String>, String>> expected = new LinkedList<Map<Map<Integer, String>, String>>();
for (Iterator<Tuple> iterator = MAP4_RESULTS.iterate(); iterator.hasNext(); ) {
Tuple tuple = iterator.next();
Object[] array = tuple.toArray();
if (comments == null || !(commentId == array[0] || commentId != null && commentId.equals(array[0]))) {
comments = new LinkedHashMap<Map<Integer, String>, String>();
expected.add(comments);
}
commentId = array[0];
@SuppressWarnings("unchecked") Pair<Pair<Integer, String>, String> pair = (Pair<Pair<Integer, String>, String>) array[1];
Pair<Integer, String> first = pair.getFirst();
Map<Integer, String> posts = Collections.singletonMap(first.getFirst(), first.getSecond());
comments.put(posts, pair.getSecond());
}
assertEquals(expected.toString(), actual.toString());
}
use of com.querydsl.core.Tuple in project querydsl by querydsl.
the class GroupByMapTest method map3.
@Test
public void map3() {
Map<Integer, Map<Integer, Map<Integer, String>>> actual = MAP3_RESULTS.transform(groupBy(postId).as(map(postId, map(commentId, commentText))));
Map<Integer, Map<Integer, Map<Integer, String>>> expected = new LinkedHashMap<Integer, Map<Integer, Map<Integer, String>>>();
for (Iterator<Tuple> iterator = MAP3_RESULTS.iterate(); iterator.hasNext(); ) {
Tuple tuple = iterator.next();
Object[] array = tuple.toArray();
Map<Integer, Map<Integer, String>> posts = expected.get(array[0]);
if (posts == null) {
posts = new LinkedHashMap<Integer, Map<Integer, String>>();
expected.put((Integer) array[0], posts);
}
@SuppressWarnings("unchecked") Pair<Integer, Pair<Integer, String>> pair = (Pair<Integer, Pair<Integer, String>>) array[1];
Integer first = pair.getFirst();
Map<Integer, String> comments = posts.computeIfAbsent(first, k -> new LinkedHashMap<Integer, String>());
Pair<Integer, String> second = pair.getSecond();
comments.put(second.getFirst(), second.getSecond());
}
assertEquals(expected.toString(), actual.toString());
}
Aggregations