Search in sources :

Example 1 with QTransaction

use of pl.morecraft.dev.settler.domain.QTransaction 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)

Example 2 with QTransaction

use of pl.morecraft.dev.settler.domain.QTransaction in project Settler by EmhyrVarEmreis.

the class UserService method getUsersWithValue.

public ResponseEntity<List<UserWithValueDTO>> getUsersWithValue(Long userId) {
    if (Objects.isNull(userId) || userId < 0) {
        userId = Security.currentUser().getId();
    }
    QUser user = QUser.user;
    QTransaction transaction = QTransaction.transaction;
    List<Tuple> fetch = new JPAQuery<>(entityManager).from(user, transaction).select(user, transaction.value.sum()).where(transaction.creator.id.eq(userId)).where(user.id.ne(userId)).where(transaction.owners.any().id.user.eq(user).or(transaction.contractors.any().id.user.eq(user))).groupBy(user.id, user.firstName, user.lastName, user.email, user.created, user.avatar, user.login, user.accountExpireDate).orderBy(transaction.value.sum().desc()).fetch();
    ModelMapper preparedModelMapper = getModelMapper();
    List<UserWithValueDTO> userWithValueDTOList = new ArrayList<>(fetch.size());
    for (Tuple tuple : fetch) {
        User u = tuple.get(user);
        Double d = tuple.get(transaction.value.sum());
        if (Objects.nonNull(u)) {
            userWithValueDTOList.add(new UserWithValueDTO(userId, preparedModelMapper.map(u, String.class), d));
        }
    }
    return new ResponseEntity<>(userWithValueDTOList, HttpStatus.OK);
}
Also used : QUser(pl.morecraft.dev.settler.domain.QUser) QTransaction(pl.morecraft.dev.settler.domain.QTransaction) ResponseEntity(org.springframework.http.ResponseEntity) User(pl.morecraft.dev.settler.domain.User) QUser(pl.morecraft.dev.settler.domain.QUser) ArrayList(java.util.ArrayList) JPAQuery(com.querydsl.jpa.impl.JPAQuery) Tuple(com.querydsl.core.Tuple) ModelMapper(org.modelmapper.ModelMapper)

Aggregations

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