Commit db8ebec7 authored by Julien Schröter's avatar Julien Schröter Committed by Frederik Wegner

Request pool description from database repositories

parent d33250c9
...@@ -49,10 +49,11 @@ type nullableUser struct { ...@@ -49,10 +49,11 @@ type nullableUser struct {
} }
type nullablePool struct { type nullablePool struct {
ID sql.NullInt64 ID sql.NullInt64
Name sql.NullString Name sql.NullString
Code sql.NullString Code sql.NullString
Image sql.NullInt64 Description sql.NullString
Image sql.NullInt64
} }
type nullableParticipant struct { type nullableParticipant struct {
...@@ -185,6 +186,7 @@ func (nP *nullablePool) ToPool(pool *schemas.PoolSchema) { ...@@ -185,6 +186,7 @@ func (nP *nullablePool) ToPool(pool *schemas.PoolSchema) {
pool.ID = uint32(nP.ID.Int64) pool.ID = uint32(nP.ID.Int64)
pool.Name = nP.Name.String pool.Name = nP.Name.String
pool.Code = nP.Code.String pool.Code = nP.Code.String
pool.Description = nP.Description.String
pool.Image = uint32(nP.Image.Int64) pool.Image = uint32(nP.Image.Int64)
} }
...@@ -733,7 +735,7 @@ func (MySQLDuelQuery) GetPools(duelID uint32, userID uint32) ([]schemas.PoolSche ...@@ -733,7 +735,7 @@ func (MySQLDuelQuery) GetPools(duelID uint32, userID uint32) ([]schemas.PoolSche
} }
// Return all pools // 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 { if errPools != nil {
return nil, errPools return nil, errPools
...@@ -742,12 +744,15 @@ func (MySQLDuelQuery) GetPools(duelID uint32, userID uint32) ([]schemas.PoolSche ...@@ -742,12 +744,15 @@ func (MySQLDuelQuery) GetPools(duelID uint32, userID uint32) ([]schemas.PoolSche
poolsResult := make([]schemas.PoolSchema, 0) poolsResult := make([]schemas.PoolSchema, 0)
for pools.Next() { for pools.Next() {
var tmp schemas.PoolSchema 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 { if err != nil {
return nil, err return nil, err
} }
tmp.Description = nullDescription.String
poolsResult = append(poolsResult, tmp) poolsResult = append(poolsResult, tmp)
} }
...@@ -921,7 +926,7 @@ func (m *MySQLDuelQuery) GetRoundByID(roundID uint32, userID uint32) (*schemas.F ...@@ -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()) 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 user ON (user.iduser=round.started)" +
" LEFT JOIN title ON (title.idtitle=user.selected_title)" + " LEFT JOIN title ON (title.idtitle=user.selected_title)" +
" LEFT JOIN avatar ON (idavatar=user.selected_avatar)" + " LEFT JOIN avatar ON (idavatar=user.selected_avatar)" +
...@@ -960,6 +965,7 @@ func (m *MySQLDuelQuery) GetRoundByID(roundID uint32, userID uint32) (*schemas.F ...@@ -960,6 +965,7 @@ func (m *MySQLDuelQuery) GetRoundByID(roundID uint32, userID uint32) (*schemas.F
&nullRound.Pool.ID, &nullRound.Pool.ID,
&nullRound.Pool.Name, &nullRound.Pool.Name,
&nullRound.Pool.Code, &nullRound.Pool.Code,
&nullRound.Pool.Description,
&nullRound.Pool.Image, &nullRound.Pool.Image,
&nullRound.Number, &nullRound.Number,
&nullRound.UserStarted.ID, &nullRound.UserStarted.ID,
......
package pool package pool
import ( import (
"database/sql"
"errors" "errors"
"fmt" "fmt"
...@@ -20,7 +21,7 @@ type MySQLPoolQuery struct{} ...@@ -20,7 +21,7 @@ type MySQLPoolQuery struct{}
// Select returns a list of all available playable pools. // Select returns a list of all available playable pools.
func (m MySQLPoolQuery) Select(userID uint32) ([]schemas.PoolSchema, error) { 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() db, err := dbhandler.GetDBConnection()
...@@ -55,12 +56,16 @@ func (m MySQLPoolQuery) Select(userID uint32) ([]schemas.PoolSchema, error) { ...@@ -55,12 +56,16 @@ func (m MySQLPoolQuery) Select(userID uint32) ([]schemas.PoolSchema, error) {
var selectedInt int var selectedInt int
var nullDescription sql.NullString
// Get pool from result // 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 { if errScan != nil {
return nil, fmt.Errorf("failed to fetch pools from result." + errScan.Error()) return nil, fmt.Errorf("failed to fetch pools from result." + errScan.Error())
} }
p.Description = nullDescription.String
if selectedInt == 1 { if selectedInt == 1 {
*(p.Selected) = true *(p.Selected) = true
} }
......
package schemas package schemas
type PoolSchema struct { type PoolSchema struct {
ID uint32 `json:"id" binding:"required"` ID uint32 `json:"id" binding:"required"`
Name string `json:"name" binding:"required"` Name string `json:"name" binding:"required"`
Code string `json:"code" binding:"required"` Code string `json:"code" binding:"required"`
Image uint32 `json:"image" 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. // use of pointer leads to omit of only nil but not false.
Selected *bool `json:"selected,omitempty"` Selected *bool `json:"selected,omitempty"`
} }
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