Commit 11129889 authored by Julien Schröter's avatar Julien Schröter Committed by Julien Schröter

Allow specifying limit for querying finished duels

parent 68aa3753
......@@ -82,13 +82,13 @@ func getDuel(repository DuelQuery) gin.HandlerFunc {
} else {
// No duel specified
// return all current duels of that user
result, errDB := repository.GetAllDuels(userID)
result, errDB := repository.GetAllDuels(userID, 0)
if errDB != nil {
raven.CaptureError(errDB, nil)
// sorting rounds by nunber and participants by id
// sorting rounds by number and participants by id
for _, d := range result {
......@@ -257,19 +257,20 @@ const queryDuelsByID = "SELECT `duel`.`idduel`, `duel`.`round_current`, `duel`.`
"WHERE `idduel` = ?;"
const queryDuelsByUser = "SELECT `duel`.`idduel`, `duel`.`round_current`, `duel`.`round_max`, `duel`.`started_on`, `duel`.`modified_on`, `duel`.`finished_on`, `duel`.`running`, `duel`.`started`, `duel`.`next`, `duel_player_score`.`score`, `participant`.`iduser`, `participant`.`username`, idavatar, avatar.image, avatar.level, title.idtitle,, subject.idsubject,, subject.shortform, subject.department, subject.description, title.unlock_score, title.unlock_win, `round`.`idround`, `round`.`number`, `round`.`started`, `pool`.`idpool`, `pool`.`name`, `pool`.`shortform`, `pool`.`image`" +
"FROM `duel`" +
"LEFT JOIN `duel_player_score` ON (`duel_player_score`.`duel` = `duel`.`idduel`)" +
"LEFT JOIN `user` AS `participant` ON (`duel_player_score`.`user` = `participant`.`iduser`)" +
"LEFT JOIN `title` ON (`idtitle` = `participant`.`selected_title`)" +
"LEFT JOIN `subject` ON (`idsubject` = `title`.`subject`)" +
"LEFT JOIN `avatar` ON (`idavatar` = `participant`.`selected_avatar`)" +
"LEFT JOIN `round` ON (`round`.`duel` = `duel`.`idduel`)" +
"LEFT JOIN `pool` ON (`round`.`pool` = `pool`.`idpool`)" +
"WHERE `idduel` IN (SELECT `duel` FROM `duel_player_score` WHERE `user` = ?)" +
"ORDER BY `idduel`"
"INNER JOIN (WITH `ds` AS (SELECT ROW_NUMBER() OVER (PARTITION BY `running` ORDER BY `finished_on`, `modified_on` DESC) row_num, `idduel` AS `limit_id`, `running` FROM `duel` AS `d` INNER JOIN `duel_player` ON (`d`.`idduel`=`duel_player`.`duel`) WHERE `user`=?)" +
"SELECT * FROM `ds` WHERE `ds`.`running`=1 OR (`ds`.`running`=0 AND `row_num`<=?)) as `limits` ON (`duel`.`idduel`=`limits`.`limit_id`)" +
"WHERE `idduel` IN (SELECT `duel` FROM `duel_player_score` WHERE `user`=?)" +
"ORDER BY `duel`.`finished_on` ASC, `duel`.`modified_on` DESC, `duel`.`idduel` ASC"
// queryQuestionsForNewRound randomly queries questions for a new duel round. Questions which
// weren't played often by both users are preferred. Thus, the score for each question is
......@@ -292,7 +293,7 @@ const queryQuestionsForNewRound = "SELECT `pool_question`.`question` FROM `pool_
// There can be various implementations for different datastores.
type DuelQuery interface {
Insert(challengerID, challengedID uint32) (*schemas.DuelSchema, error)
GetAllDuels(userID uint32) ([]schemas.DuelSchema, error)
GetAllDuels(userID, limit uint32) ([]schemas.DuelSchema, error)
GetDuel(id, userID uint32) (*schemas.DuelSchema, error)
GetPools(duelID, userID uint32) ([]schemas.PoolSchema, error)
SetPool(duelID, poolID, userID uint32) (*schemas.DuelSchema, error)
......@@ -446,7 +447,7 @@ func (m *MySQLDuelQuery) Insert(challengerID, challengedID uint32) (*schemas.Due
// the given user is a participant the duel.
// The resulting objects will contain everything except the exact questions and answers including
// answers of specific users.
func (m *MySQLDuelQuery) GetAllDuels(userID uint32) ([]schemas.DuelSchema, error) {
func (m *MySQLDuelQuery) GetAllDuels(userID, limit uint32) ([]schemas.DuelSchema, error) {
db, err := dbhandler.GetDBConnection()
if err != nil {
return nil, fmt.Errorf("failed to get database connection." + err.Error())
......@@ -465,7 +466,7 @@ func (m *MySQLDuelQuery) GetAllDuels(userID uint32) ([]schemas.DuelSchema, error
defer stmtDuels.Close()
rowsDuels, errQueryDuels := stmtDuels.Query(userID)
rowsDuels, errQueryDuels := stmtDuels.Query(userID, limit, userID)
if errQueryDuels != nil {
return nil, fmt.Errorf("Failed to fetch request result." + errQueryDuels.Error())
......@@ -66,7 +66,7 @@ func (m *mockDuel) Insert(challengerID, challengedID uint32) (*schemas.DuelSchem
return &duel, nil
func (m *mockDuel) GetAllDuels(userID uint32) ([]schemas.DuelSchema, error) {
func (m *mockDuel) GetAllDuels(userID, limit uint32) ([]schemas.DuelSchema, error) {
result := make([]schemas.DuelSchema, 0)
for _, d := range m.duels {
if d.ContainsParticipant(userID) {
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment