use of pl.morecraft.dev.settler.web.dto.CategoryWithValueDTO 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);
}
Aggregations