From db8ebec7001bd3be751c41b5340a62540aeb2c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Schro=CC=88ter?= Date: Tue, 22 Sep 2020 15:31:58 +0200 Subject: [PATCH] Request pool description from database repositories --- endpoint/duel/query.go | 20 +++++++++++++------- endpoint/pool/query.go | 9 +++++++-- schemas/pool.go | 9 +++++---- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/endpoint/duel/query.go b/endpoint/duel/query.go index a34e8fd..9ebd6fa 100644 --- a/endpoint/duel/query.go +++ b/endpoint/duel/query.go @@ -49,10 +49,11 @@ type nullableUser struct { } type nullablePool struct { - ID sql.NullInt64 - Name sql.NullString - Code sql.NullString - Image sql.NullInt64 + ID sql.NullInt64 + Name sql.NullString + Code sql.NullString + Description sql.NullString + Image sql.NullInt64 } type nullableParticipant struct { @@ -185,6 +186,7 @@ func (nP *nullablePool) ToPool(pool *schemas.PoolSchema) { pool.ID = uint32(nP.ID.Int64) pool.Name = nP.Name.String pool.Code = nP.Code.String + pool.Description = nP.Description.String pool.Image = uint32(nP.Image.Int64) } @@ -733,7 +735,7 @@ func (MySQLDuelQuery) GetPools(duelID uint32, userID uint32) ([]schemas.PoolSche } // Return all pools - pools, errPools := db.Query("SELECT `idpool`, `name`, `shortform`, `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` 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) if errPools != nil { return nil, errPools @@ -742,12 +744,15 @@ func (MySQLDuelQuery) GetPools(duelID uint32, userID uint32) ([]schemas.PoolSche poolsResult := make([]schemas.PoolSchema, 0) for pools.Next() { var tmp schemas.PoolSchema + var nullDescription sql.NullString - err := pools.Scan(&tmp.ID, &tmp.Name, &tmp.Code, &tmp.Image) + err := pools.Scan(&tmp.ID, &tmp.Name, &tmp.Code, &nullDescription, &tmp.Image) if err != nil { return nil, err } + tmp.Description = nullDescription.String + poolsResult = append(poolsResult, tmp) } @@ -921,7 +926,7 @@ func (m *MySQLDuelQuery) GetRoundByID(roundID uint32, userID uint32) (*schemas.F return nil, fmt.Errorf("failed to connect to database." + errPing.Error()) } - queryRoundByID := "SELECT round.idround, round.duel, pool.idpool, pool.name, pool.shortform, pool.image, round.number, user.iduser, user.username, idavatar, avatar.image, avatar.level, title.idtitle, title.name, title_subject.idsubject, title_subject.name, title_subject.shortform, title_subject.department, title_subject.description, title.unlock_score, title.unlock_win, question.idquestion, maintainer.name, question.text, question.image, subject.name, question.answertype FROM round" + + queryRoundByID := "SELECT round.idround, round.duel, pool.idpool, pool.name, pool.shortform, pool.description, pool.image, round.number, user.iduser, user.username, idavatar, avatar.image, avatar.level, title.idtitle, title.name, title_subject.idsubject, title_subject.name, title_subject.shortform, title_subject.department, title_subject.description, title.unlock_score, title.unlock_win, question.idquestion, maintainer.name, question.text, question.image, subject.name, question.answertype FROM round" + " LEFT JOIN user ON (user.iduser=round.started)" + " LEFT JOIN title ON (title.idtitle=user.selected_title)" + " LEFT JOIN avatar ON (idavatar=user.selected_avatar)" + @@ -960,6 +965,7 @@ func (m *MySQLDuelQuery) GetRoundByID(roundID uint32, userID uint32) (*schemas.F &nullRound.Pool.ID, &nullRound.Pool.Name, &nullRound.Pool.Code, + &nullRound.Pool.Description, &nullRound.Pool.Image, &nullRound.Number, &nullRound.UserStarted.ID, diff --git a/endpoint/pool/query.go b/endpoint/pool/query.go index ff0ca51..6f04d60 100644 --- a/endpoint/pool/query.go +++ b/endpoint/pool/query.go @@ -1,6 +1,7 @@ package pool import ( + "database/sql" "errors" "fmt" @@ -20,7 +21,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`, `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` GROUP BY `pool` HAVING COUNT(DISTINCT `question`) >= ?)" db, err := dbhandler.GetDBConnection() @@ -55,12 +56,16 @@ func (m MySQLPoolQuery) Select(userID uint32) ([]schemas.PoolSchema, error) { var selectedInt int + var nullDescription sql.NullString + // Get pool from result - errScan := rows.Scan(&(p.ID), &(p.Name), &(p.Code), &(p.Image), &selectedInt) + errScan := rows.Scan(&(p.ID), &(p.Name), &(p.Code), &nullDescription, &(p.Image), &selectedInt) if errScan != nil { return nil, fmt.Errorf("failed to fetch pools from result." + errScan.Error()) } + p.Description = nullDescription.String + if selectedInt == 1 { *(p.Selected) = true } diff --git a/schemas/pool.go b/schemas/pool.go index 8b972b6..d9a9af8 100644 --- a/schemas/pool.go +++ b/schemas/pool.go @@ -1,10 +1,11 @@ package schemas type PoolSchema struct { - ID uint32 `json:"id" binding:"required"` - Name string `json:"name" binding:"required"` - Code string `json:"code" binding:"required"` - Image uint32 `json:"image" binding:"required"` + ID uint32 `json:"id" binding:"required"` + Name string `json:"name" binding:"required"` + Code string `json:"code" binding:"required"` + Description string `json:"description" binding:"required"` + Image uint32 `json:"image" binding:"required"` // use of pointer leads to omit of only nil but not false. Selected *bool `json:"selected,omitempty"` } -- GitLab