Commit 4330a1b5 authored by Julien Schröter's avatar Julien Schröter

Let users just play questions available to their university's members

parent 14a2d51f
Pipeline #2284 passed with stages
in 8 minutes and 25 seconds
......@@ -126,8 +126,8 @@ func postDuel(repository DuelQuery, sessionRepository refreshjwt.RefreshJWTQuery
ctx.AbortWithError(http.StatusNotFound, err)
case ErrNoCommonPool:
// Get pools of user and challenged user for the app
poolsChallenger, errPoolsChallenger := poolRepository.SelectUserPools(challenger)
poolsChallenged, errPoolsChallanged := poolRepository.SelectUserPools(challenged.ID)
poolsChallenger, errPoolsChallenger := poolRepository.SelectUserPools(challenger, challenged.ID)
poolsChallenged, errPoolsChallanged := poolRepository.SelectUserPools(challenged.ID, challenger)
if errPoolsChallenger != nil {
raven.CaptureError(errPoolsChallenger, nil)
ctx.AbortWithError(http.StatusConflict, errPoolsChallenger)
......
......@@ -285,6 +285,7 @@ const queryQuestionsForNewRound = "SELECT `pool_question`.`question` FROM `pool_
"`pool_question`.`pool`=? " +
"AND `pool_question`.`question` NOT IN (SELECT `question` FROM `roundquestion` INNER JOIN `round` ON (`round`.`idround` = `roundquestion`.`round`) WHERE `duel`=?)" +
"AND `question`.`published`=1 " +
"AND (`question`.`university` IS NULL OR `question`.`university` = ALL (SELECT `university` FROM `user` WHERE `iduser`=? OR `iduser`=?))" +
"GROUP BY `pool_question`.`question`" +
"ORDER BY POW(COUNT(IF(`useranswer`.`user`=? AND RAND()<.9, 1, NULL)), 2) + POW(COUNT(IF(`useranswer`.`user`=? AND RAND()<.9, 1, NULL)), 2)" +
"LIMIT ?"
......@@ -759,7 +760,12 @@ func (MySQLDuelQuery) GetPools(duelID uint32, userID uint32) ([]schemas.PoolSche
}
// Return all pools
pools, errPools := db.Query("SELECT `idpool`, `name`, `shortform`, `description`,`image` FROM `pool` WHERE `idpool` IN (SELECT `pool` FROM `pool_question` GROUP BY `pool` HAVING COUNT(`question`) >= ?) AND `idpool` IN (SELECT DISTINCT `pool` FROM `user_pool` WHERE `user`=? OR `user`=? GROUP BY `pool` HAVING COUNT(DISTINCT `user`)=2)", QuestionsPerRound, userID, otherUserID)
pools, errPools := db.Query("SELECT `idpool`, `name`, `shortform`, `description`,`image` FROM `pool` WHERE `idpool` IN (SELECT `pool` FROM `pool_question` "+
"INNER JOIN `question` ON (`question`.`idquestion`=`pool_question`.`question`) "+
"WHERE `published`=1 AND (`university` IS NULL OR `university` = ALL (SELECT `university` FROM `user` WHERE `iduser`=? OR `iduser`=?)) "+
"AND `question` NOT IN (SELECT `question` FROM `roundquestion` INNER JOIN `round` ON (`idround`=`roundquestion`.`round`) WHERE `duel`=?) "+
"GROUP BY `pool` HAVING COUNT(DISTINCT `question`) >= ?) "+
"AND `idpool` IN (SELECT DISTINCT `pool` FROM `user_pool` WHERE `user`=? OR `user`=? GROUP BY `pool` HAVING COUNT(DISTINCT `user`)=2)", userID, otherUserID, duelID, QuestionsPerRound, userID, otherUserID)
if errPools != nil {
return nil, errPools
......@@ -872,7 +878,7 @@ func (m *MySQLDuelQuery) SetPool(duelID, poolID, userID uint32) (*schemas.DuelSc
}
// Select questions and create roundquestions
questions, errQuestions := tx.Query(queryQuestionsForNewRound, poolID, duelID, user1, user2, QuestionsPerRound)
questions, errQuestions := tx.Query(queryQuestionsForNewRound, poolID, duelID, user1, user2, user1, user2, QuestionsPerRound)
if errQuestions != nil {
return nil, errQuestions
......
......@@ -16,7 +16,7 @@ const QuestionsPerRound = 2
type PoolQuery interface {
Select(userID uint32) ([]schemas.PoolSchema, error)
SelectUserPools(userID uint32) ([]uint32, error)
SelectUserPools(userID, secondaryUser uint32) ([]uint32, error)
PatchUserPools(userID uint32, selection []uint32) error
}
......@@ -24,7 +24,7 @@ type MySQLPoolQuery struct{}
// Select returns a list of all available playable pools.
func (m MySQLPoolQuery) Select(userID uint32) ([]schemas.PoolSchema, error) {
queryString := "SELECT `idpool`, `name`, `shortform`, `description`,`image`, `idpool` IN (SELECT DISTINCT pool from user_pool WHERE user=?) AS selected FROM `pool` WHERE `idpool` IN (SELECT `pool` FROM `pool_question` GROUP BY `pool` HAVING COUNT(DISTINCT `question`) >= ?)"
queryString := "SELECT `idpool`, `name`, `shortform`, `description`,`image`, `idpool` IN (SELECT DISTINCT pool from user_pool WHERE user=?) AS selected FROM `pool` WHERE `idpool` IN (SELECT `pool` FROM `pool_question` INNER JOIN `question` ON (`idquestion`=`pool_question`.`question`) WHERE `published`=1 AND (`university` IS NULL OR `university`=(SELECT `university` FROM `user` WHERE `iduser`=?)) GROUP BY `pool` HAVING COUNT(DISTINCT `question`) >= ?)"
db, err := dbhandler.GetDBConnection()
......@@ -39,7 +39,7 @@ func (m MySQLPoolQuery) Select(userID uint32) ([]schemas.PoolSchema, error) {
}
// Fetch pools from database
rows, errRows := db.Query(queryString, userID, QuestionsPerRound)
rows, errRows := db.Query(queryString, userID, userID, QuestionsPerRound)
// Check whether query failed
if errRows != nil {
......@@ -80,14 +80,14 @@ func (m MySQLPoolQuery) Select(userID uint32) ([]schemas.PoolSchema, error) {
}
// Select returns a list of all available playable pools.
func (m MySQLPoolQuery) SelectUserPools(userID uint32) ([]uint32, error) {
func (m MySQLPoolQuery) SelectUserPools(userID, secondaryUser uint32) ([]uint32, error) {
db, err := dbhandler.GetDBConnection()
if err != nil {
return nil, fmt.Errorf("failed to get database instance: %v", err)
}
// Fetch pools from database
rows, err := db.Query("SELECT DISTINCT `pool` FROM `user_pool` WHERE `user`=? AND `pool` IN (SELECT `pool` FROM `pool_question` GROUP BY `pool` HAVING COUNT(DISTINCT `question`) >= ?)", userID, QuestionsPerRound)
rows, err := db.Query("SELECT DISTINCT `pool` FROM `user_pool` WHERE `user`=? AND `pool` IN (SELECT `pool` FROM `pool_question` INNER JOIN `question` ON (`idquestion`=`pool_question`.`question`) WHERE `published`=1 AND (`university` IS NULL OR `university`= ALL (SELECT `university` FROM `user` WHERE `iduser`=? OR `iduser`=?)) GROUP BY `pool` HAVING COUNT(DISTINCT `question`) >= ?)", userID, userID, secondaryUser, QuestionsPerRound)
// Check whether query failed
if err != nil {
......
......@@ -193,14 +193,16 @@ INSERT INTO `duel_player` (`user`, `duel`) VALUES(1, 3);
INSERT INTO `duel_player` (`user`, `duel`) VALUES(4, 3);
-- QUESTION
INSERT INTO question (`idquestion`, `author`, `text`, `subject`, `reviewed`, `verified`, `answertype`, `published`)
VALUE (1, 1, "Question1", 1, 0, 0, "option", 1);
INSERT INTO question (`idquestion`, `author`, `text`, `subject`, `reviewed`, `verified`, `answertype`, `published`)
VALUE (2, 2, "Question2", 1, 0, 0, "option", 1);
INSERT INTO question (`idquestion`, `author`, `text`, `subject`, `reviewed`, `verified`, `answertype`, `published`)
VALUE (3, 1, "Question3", 1, 0, 0, "option", 1);
INSERT INTO question (`idquestion`, `author`, `text`, `subject`, `reviewed`, `verified`, `answertype`, `published`)
VALUE (4, 2, "Question4", 1, 0, 0, "option", 1);
INSERT INTO question (`idquestion`, `author`, `text`, `subject`, `reviewed`, `verified`, `answertype`, `published`, `university`)
VALUE (1, 1, "Question1", 1, 0, 0, "option", 1, 1);
INSERT INTO question (`idquestion`, `author`, `text`, `subject`, `reviewed`, `verified`, `answertype`, `published`, `university`)
VALUE (2, 2, "Question2", 1, 0, 0, "option", 1, 1);
INSERT INTO question (`idquestion`, `author`, `text`, `subject`, `reviewed`, `verified`, `answertype`, `published`, `university`)
VALUE (3, 1, "Question3", 1, 0, 0, "option", 1, NULL);
INSERT INTO question (`idquestion`, `author`, `text`, `subject`, `reviewed`, `verified`, `answertype`, `published`, `university`)
VALUE (4, 2, "Question4", 1, 0, 0, "option", 1, NULL);
INSERT INTO question (`idquestion`, `author`, `text`, `subject`, `reviewed`, `verified`, `answertype`, `published`, `university`)
VALUE (5, 2, "Question5", 1, 0, 0, "option", 1, 2);
-- POOL_QUESTION
INSERT INTO pool_question (`question`, `pool`) VALUES (1, 1);
......@@ -220,6 +222,12 @@ INSERT INTO pool_question (`question`, `pool`) VALUES (4, 3);
INSERT INTO pool_question (`question`, `pool`) VALUES (1, 4);
INSERT INTO pool_question (`question`, `pool`) VALUES (5, 1);
INSERT INTO pool_question (`question`, `pool`) VALUES (5, 2);
INSERT INTO pool_question (`question`, `pool`) VALUES (5, 3);
INSERT INTO pool_question (`question`, `pool`) VALUES (5, 4);
-- ANSWER
INSERT INTO optionanswer (`idoptionanswer`, `text`, `correct`, `question`)
VALUE (1, "A", 1, 1);
......
......@@ -21,7 +21,7 @@ func (p *mockPool) Select(userID uint32) ([]schemas.PoolSchema, error) {
return pools, nil
}
func (*mockPool) SelectUserPools(userId uint32) ([]uint32, error) {
func (*mockPool) SelectUserPools(userId, secondaryUser uint32) ([]uint32, error) {
return nil, nil
}
......
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