Search in sources :

Example 1 with CategoryWithValueDTO

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);
}
Also used : QTransaction(pl.morecraft.dev.settler.domain.QTransaction) ResponseEntity(org.springframework.http.ResponseEntity) Category(pl.morecraft.dev.settler.domain.Category) QCategory(pl.morecraft.dev.settler.domain.QCategory) ArrayList(java.util.ArrayList) CategoryWithValueDTO(pl.morecraft.dev.settler.web.dto.CategoryWithValueDTO) QCategory(pl.morecraft.dev.settler.domain.QCategory) Tuple(com.querydsl.core.Tuple) ModelMapper(org.modelmapper.ModelMapper)

Aggregations

Tuple (com.querydsl.core.Tuple)1 ArrayList (java.util.ArrayList)1 ModelMapper (org.modelmapper.ModelMapper)1 ResponseEntity (org.springframework.http.ResponseEntity)1 Category (pl.morecraft.dev.settler.domain.Category)1 QCategory (pl.morecraft.dev.settler.domain.QCategory)1 QTransaction (pl.morecraft.dev.settler.domain.QTransaction)1 CategoryWithValueDTO (pl.morecraft.dev.settler.web.dto.CategoryWithValueDTO)1