83 lines
3.7 KiB
Java
83 lines
3.7 KiB
Java
/**
|
|
*
|
|
*/
|
|
package de.bstly.board.repository;
|
|
|
|
import java.time.Instant;
|
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.data.jpa.repository.JpaRepository;
|
|
import org.springframework.data.jpa.repository.Query;
|
|
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
|
import org.springframework.data.repository.query.Param;
|
|
import org.springframework.stereotype.Repository;
|
|
|
|
import de.bstly.board.model.Entry;
|
|
import de.bstly.board.model.RankedEntry;
|
|
|
|
/**
|
|
* The Interface EntryRepository.
|
|
*/
|
|
@Repository
|
|
public interface EntryRepository
|
|
extends JpaRepository<Entry, Long>, QuerydslPredicateExecutor<Entry> {
|
|
|
|
static final String UPVOTES_QUERY = "SELECT upvote.target,COUNT(upvote.id) AS count FROM votes as upvote WHERE upvote.type = 0 AND upvote.target_type = 1 GROUP BY upvote.target";
|
|
|
|
static final String DOWNVOTES_QUERY = "SELECT downvote.target,COUNT(downvote.id) AS count FROM votes as downvote WHERE downvote.type = 1 AND downvote.target_type = 1 GROUP BY downvote.target";
|
|
|
|
static final String COMMENTS_QUERY = "SELECT comment.target,MAX(comment.created) as last,COUNT(comment.id) AS count FROM comments as comment WHERE comment.flagged_status = 'NORMAL' GROUP BY comment.target";
|
|
|
|
static final String RANK_CALCULATION_QUERY = "SELECT entry.*, (IFNULL(upvote.count,0) - IFNULL(downvote.count,0)) as points, (IFNULL(upvote.count,0) - IFNULL(downvote.count,0)) / IF(:gravity > 0, POW(TIMESTAMPDIFF(HOUR, entry.created, :before)+2,:gravity), 1) AS ranking FROM entries AS entry LEFT JOIN ("
|
|
+ UPVOTES_QUERY
|
|
+ ") AS upvote ON upvote.target = entry.id LEFT JOIN ("
|
|
+ DOWNVOTES_QUERY
|
|
+ ") AS downvote ON downvote.target = entry.id WHERE entry.created < :before AND entry.flagged_status = 'NORMAL' ORDER BY ranking DESC, entry.created DESC";
|
|
|
|
static final String COMMENT_CALCULATION_QUERY = "SELECT entry.*, IFNULL(comment.count,0) as comments, IFNULL(comment.count,0) / IF(:gravity > 0, POW(TIMESTAMPDIFF(HOUR, comment.last, :before)+2,:gravity), 1) AS ranking FROM entries AS entry LEFT JOIN ("
|
|
+ COMMENTS_QUERY
|
|
+ ") AS comment ON comment.target = entry.id WHERE entry.created < :before AND entry.flagged_status = 'NORMAL' ORDER BY ranking DESC, entry.created DESC";
|
|
|
|
static final String LAST_COMMENT_QUERY = "SELECT entry.* FROM entries AS entry LEFT JOIN ("
|
|
+ COMMENTS_QUERY
|
|
+ ") AS comment ON comment.target = entry.id WHERE entry.created < :before AND entry.flagged_status = 'NORMAL' ORDER BY comment.last DESC, entry.created DESC";
|
|
|
|
static final String COUNT_QUERY = "SELECT count(*) FROM entries as entry WHERE entry.created < :before AND entry.flagged_status = 'NORMAL'";
|
|
|
|
/**
|
|
* Find all by ranking.
|
|
*
|
|
* @param before the before
|
|
* @param gravity the gravity
|
|
* @param pageable the pageable
|
|
* @return the page
|
|
*/
|
|
@Query(value = RANK_CALCULATION_QUERY, countQuery = COUNT_QUERY, nativeQuery = true)
|
|
Page<RankedEntry> findAllByRanking(@Param("before") Instant before,
|
|
@Param("gravity") double gravity, Pageable pageable);
|
|
|
|
/**
|
|
* Find all by comments.
|
|
*
|
|
* @param before the before
|
|
* @param gravity the gravity
|
|
* @param pageable the pageable
|
|
* @return the page
|
|
*/
|
|
@Query(value = COMMENT_CALCULATION_QUERY, countQuery = COUNT_QUERY, nativeQuery = true)
|
|
Page<RankedEntry> findAllByComments(@Param("before") Instant before,
|
|
@Param("gravity") double gravity, Pageable pageable);
|
|
|
|
/**
|
|
* Find all by last comment.
|
|
*
|
|
* @param before the before
|
|
* @param pageable the pageable
|
|
* @return the page
|
|
*/
|
|
@Query(value = LAST_COMMENT_QUERY, countQuery = COUNT_QUERY, nativeQuery = true)
|
|
Page<Entry> findAllByLastComment(@Param("before") Instant before, Pageable pageable);
|
|
|
|
}
|