bstlboard-back/src/main/java/de/bstly/board/businesslogic/UserPageManager.java

127 lines
3.1 KiB
Java

/**
*
*/
package de.bstly.board.businesslogic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import de.bstly.board.model.QUserPage;
import de.bstly.board.model.UserPage;
import de.bstly.board.repository.UserPageRepository;
/**
* The Class UserPageManager.
*/
@Component
public class UserPageManager {
@Autowired
private UserPageRepository userPageRepository;
private QUserPage qUserPage = QUserPage.userPage;
/**
* Exists.
*
* @param username the username
* @param name the name
* @return true, if successful
*/
public boolean exists(String username, String name) {
return userPageRepository
.exists(qUserPage.username.equalsIgnoreCase(username).and(qUserPage.name.eq(name)));
}
/**
* Gets the.
*
* @param id the id
* @return the user page
*/
public UserPage get(Long id) {
return userPageRepository.findById(id).orElse(null);
}
/**
* Gets the.
*
* @param username the username
* @param name the name
* @return the user page
*/
public UserPage get(String username, String name) {
return userPageRepository
.findOne(qUserPage.username.equalsIgnoreCase(username).and(qUserPage.name.eq(name)))
.orElse(null);
}
/**
* Save.
*
* @param userPage the user page
* @return the user page
*/
public UserPage save(UserPage userPage) {
return userPageRepository.save(userPage);
}
/**
* Gets the by user.
*
* @param username the username
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param desc the desc
* @return the by user
*/
public Page<UserPage> getByUser(String username, int page, int size, String sortBy,
boolean desc) {
return userPageRepository.findAll(qUserPage.username.equalsIgnoreCase(username),
PageRequest.of(page, size, Sort.by(desc ? Direction.DESC : Direction.ASC, sortBy)));
}
/**
* Count by user.
*
* @param username the username
* @return the long
*/
public long countByUser(String username) {
return userPageRepository.count(qUserPage.username.equalsIgnoreCase(username));
}
/**
* Gets the public.
*
* @param username the username
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param desc the desc
* @return the public
*/
public Page<UserPage> getPublic(String username, int page, int size, String sortBy,
boolean desc) {
return userPageRepository.findAll(
qUserPage.username.notEqualsIgnoreCase(username).and(qUserPage.publicPage.isTrue()),
PageRequest.of(page, size, Sort.by(desc ? Direction.DESC : Direction.ASC, sortBy)));
}
/**
* Delete.
*
* @param username the username
* @param name the name
*/
public void delete(String username, String name) {
Assert.isTrue(exists(username, name), "UserPage not found!");
userPageRepository.delete(get(username, name));
}
}