80 lines
3.2 KiB
Java
80 lines
3.2 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 CALCULATION_QUERY = "SELECT entry.*, (IFNULL(upvote.count,0) - IFNULL(downvote.count,0)) as points, (IFNULL(upvote.count,0) - IFNULL(downvote.count,0)) / POW(TIMESTAMPDIFF(HOUR, entry.created, :before)+2,:gravity) 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.entry_status = 'NORMAL' ORDER BY ranking DESC, entry.created DESC";
|
|
|
|
|
|
static final String ARCHIVE_CALCULATION_QUERY = "SELECT entry.*, (IFNULL(upvote.count,0) - IFNULL(downvote.count,0)) as points, (IFNULL(upvote.count,0) - IFNULL(downvote.count,0)) / POW(TIMESTAMPDIFF(HOUR, entry.created, :before)+2,:gravity) 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 ORDER BY ranking DESC, entry.created DESC";
|
|
|
|
|
|
static final String ADDITIONAL_QUERY = "SELECT entry.*, calculation.ranking, calculation.points FROM entries AS entry LEFT JOIN ("
|
|
+ CALCULATION_QUERY
|
|
+ ") as calculation on calculation.id = entry.id WHERE entry.created < :before ORDER BY calculation.ranking DESC, entry.created DESC";
|
|
|
|
|
|
static final String COUNT_QUERY = "SELECT count(*) FROM entries as entry WHERE entry.created < :before";
|
|
|
|
/**
|
|
* Find all by ranking.
|
|
*
|
|
* @param before the before
|
|
* @param gravity the gravity
|
|
* @param pageable the pageable
|
|
* @return the page
|
|
*/
|
|
@Query(value = CALCULATION_QUERY, countQuery = COUNT_QUERY, nativeQuery = true)
|
|
Page<RankedEntry> findAllByRanking(@Param("before") Instant before,
|
|
@Param("gravity") double gravity, Pageable pageable);
|
|
|
|
/**
|
|
* Find all by ranking archive.
|
|
*
|
|
* @param before the before
|
|
* @param gravity the gravity
|
|
* @param pageable the pageable
|
|
* @return the page
|
|
*/
|
|
@Query(value = ARCHIVE_CALCULATION_QUERY, countQuery = COUNT_QUERY, nativeQuery = true)
|
|
Page<RankedEntry> findAllByRankingArchive(@Param("before") Instant before,
|
|
@Param("gravity") double gravity, Pageable pageable);
|
|
|
|
}
|