From 84df1f3a2f3e2c4cd90dab9fc9f82b7affeeae14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Schro=CC=88ter?= Date: Tue, 13 Oct 2020 21:04:59 +0200 Subject: [PATCH] Allow unlock_score=null and unlock_win=null outside of title repository --- endpoint/duel/query.go | 4 +++ endpoint/friend/query.go | 57 +++++++++++++++++++++++++++++++++++----- endpoint/user/query.go | 57 +++++++++++++++++++++++++++++++++++----- 3 files changed, 106 insertions(+), 12 deletions(-) diff --git a/endpoint/duel/query.go b/endpoint/duel/query.go index 80f541c..2953fd3 100644 --- a/endpoint/duel/query.go +++ b/endpoint/duel/query.go @@ -128,9 +128,13 @@ func (nT *nullableTitle) ToTitle(title *schemas.TitleSchema) bool { } if nT.UnlockScore.Valid { title.UnlockScore = int32(nT.UnlockScore.Int64) + } else { + title.UnlockScore = -1 } if nT.UnlockWin.Valid { title.UnlockWin = int32(nT.UnlockWin.Int64) + } else { + title.UnlockWin = -1 } return nT.ID.Valid && nT.Name.Valid } diff --git a/endpoint/friend/query.go b/endpoint/friend/query.go index f1e7ced..918d132 100644 --- a/endpoint/friend/query.go +++ b/endpoint/friend/query.go @@ -66,6 +66,9 @@ func (MySQLFriendQuery) SelectByID(userID uint32) ([]schemas.InfoUserSchema, err var tmpSubjectDepartment sql.NullString var tmpSubjectDescription sql.NullString + var tmpUnlockScore sql.NullInt32 + var tmpUnlockWin sql.NullInt32 + if errFetch := rows.Scan( &tmpFriend.ID, &tmpFriend.Username, @@ -79,8 +82,8 @@ func (MySQLFriendQuery) SelectByID(userID uint32) ([]schemas.InfoUserSchema, err &(tmpSubjectShortForm), &(tmpSubjectDepartment), &(tmpSubjectDescription), - &tmpFriend.SelectedTitle.UnlockScore, - &tmpFriend.SelectedTitle.UnlockWin, + &tmpUnlockScore, + &tmpUnlockWin, ); errFetch != nil { return nil, fmt.Errorf("Cannot scan db values into friends list. " + errFetch.Error()) } @@ -95,6 +98,18 @@ func (MySQLFriendQuery) SelectByID(userID uint32) ([]schemas.InfoUserSchema, err } } + if tmpUnlockScore.Valid { + tmpFriend.SelectedTitle.UnlockScore = tmpUnlockScore.Int32 + } else { + tmpFriend.SelectedTitle.UnlockScore = -1 + } + + if tmpUnlockWin.Valid { + tmpFriend.SelectedTitle.UnlockWin = tmpUnlockWin.Int32 + } else { + tmpFriend.SelectedTitle.UnlockWin = -1 + } + friends = append(friends, tmpFriend) } @@ -209,6 +224,9 @@ func (MySQLFriendQuery) Insert(userID, userIDToAdd uint32) ([]schemas.InfoUserSc var tmpSubjectDepartment sql.NullString var tmpSubjectDescription sql.NullString + var tmpUnlockScore sql.NullInt32 + var tmpUnlockWin sql.NullInt32 + if errResult := rows.Scan( &tmpFriend.ID, &tmpFriend.Username, @@ -222,8 +240,8 @@ func (MySQLFriendQuery) Insert(userID, userIDToAdd uint32) ([]schemas.InfoUserSc &(tmpSubjectShortForm), &(tmpSubjectDepartment), &(tmpSubjectDescription), - &tmpFriend.SelectedTitle.UnlockScore, - &tmpFriend.SelectedTitle.UnlockWin, + &tmpUnlockScore, + &tmpUnlockWin, ); errResult != nil { if tmpSubjectName.Valid { tmpFriend.SelectedTitle.Subject = &schemas.Subject{ @@ -235,6 +253,18 @@ func (MySQLFriendQuery) Insert(userID, userIDToAdd uint32) ([]schemas.InfoUserSc } } + if tmpUnlockScore.Valid { + tmpFriend.SelectedTitle.UnlockScore = tmpUnlockScore.Int32 + } else { + tmpFriend.SelectedTitle.UnlockScore = -1 + } + + if tmpUnlockWin.Valid { + tmpFriend.SelectedTitle.UnlockWin = tmpUnlockWin.Int32 + } else { + tmpFriend.SelectedTitle.UnlockWin = -1 + } + return nil, fmt.Errorf("failed to provide updated friends list." + errResult.Error()) } result = append(result, tmpFriend) @@ -309,6 +339,9 @@ func (MySQLFriendQuery) Delete(userID, userIDToRemove uint32) ([]schemas.InfoUse var tmpSubjectDepartment sql.NullString var tmpSubjectDescription sql.NullString + var tmpUnlockScore sql.NullInt32 + var tmpUnlockWin sql.NullInt32 + if errResult := rows.Scan( &user.ID, &user.Username, @@ -322,8 +355,8 @@ func (MySQLFriendQuery) Delete(userID, userIDToRemove uint32) ([]schemas.InfoUse &(tmpSubjectShortForm), &(tmpSubjectDepartment), &(tmpSubjectDescription), - &user.SelectedTitle.UnlockScore, - &user.SelectedTitle.UnlockWin, + &tmpUnlockScore, + &tmpUnlockWin, ); errResult != nil { if tmpSubjectName.Valid { user.SelectedTitle.Subject = &schemas.Subject{ @@ -335,6 +368,18 @@ func (MySQLFriendQuery) Delete(userID, userIDToRemove uint32) ([]schemas.InfoUse } } + if tmpUnlockScore.Valid { + user.SelectedTitle.UnlockScore = tmpUnlockScore.Int32 + } else { + user.SelectedTitle.UnlockScore = -1 + } + + if tmpUnlockWin.Valid { + user.SelectedTitle.UnlockWin = tmpUnlockWin.Int32 + } else { + user.SelectedTitle.UnlockWin = -1 + } + return nil, fmt.Errorf("failed to provide updated friends list." + errResult.Error()) } result = append(result, user) diff --git a/endpoint/user/query.go b/endpoint/user/query.go index 12597e6..a7fe9f8 100755 --- a/endpoint/user/query.go +++ b/endpoint/user/query.go @@ -176,6 +176,9 @@ func (MySQLUserQuery) Select(id uint32) ([]schemas.FullUserSchema, error) { var tmpSubjectDepartment sql.NullString var tmpSubjectDescription sql.NullString + var tmpUnlockScore sql.NullInt32 + var tmpUnlockWin sql.NullInt32 + //make sql query and save response to the user pointer err = stmt.QueryRow(id).Scan( &users[0].ID, @@ -194,8 +197,8 @@ func (MySQLUserQuery) Select(id uint32) ([]schemas.FullUserSchema, error) { &(tmpSubjectShortForm), &(tmpSubjectDepartment), &(tmpSubjectDescription), - &users[0].SelectedTitle.UnlockScore, - &users[0].SelectedTitle.UnlockWin, + &tmpUnlockScore, + &tmpUnlockWin, &users[0].Verified, &users[0].University, &users[0].Level) @@ -217,6 +220,18 @@ func (MySQLUserQuery) Select(id uint32) ([]schemas.FullUserSchema, error) { } } + if tmpUnlockScore.Valid { + users[0].SelectedTitle.UnlockScore = tmpUnlockScore.Int32 + } else { + users[0].SelectedTitle.UnlockScore = -1 + } + + if tmpUnlockWin.Valid { + users[0].SelectedTitle.UnlockWin = tmpUnlockWin.Int32 + } else { + users[0].SelectedTitle.UnlockWin = -1 + } + //return with no errors return users, nil } @@ -244,6 +259,9 @@ func (MySQLUserQuery) SelectByUsername(username string) ([]schemas.InfoUserSchem var tmpSubjectDepartment sql.NullString var tmpSubjectDescription sql.NullString + var tmpUnlockScore sql.NullInt32 + var tmpUnlockWin sql.NullInt32 + //make sql query and save response to the user pointer err = stmt.QueryRow(username).Scan( &user[0].ID, @@ -258,8 +276,8 @@ func (MySQLUserQuery) SelectByUsername(username string) ([]schemas.InfoUserSchem &(tmpSubjectShortForm), &(tmpSubjectDepartment), &(tmpSubjectDescription), - &user[0].SelectedTitle.UnlockScore, - &user[0].SelectedTitle.UnlockWin) + &tmpUnlockScore, + &tmpUnlockWin) if err != nil { return nil, err } @@ -274,6 +292,18 @@ func (MySQLUserQuery) SelectByUsername(username string) ([]schemas.InfoUserSchem } } + if tmpUnlockScore.Valid { + user[0].SelectedTitle.UnlockScore = tmpUnlockScore.Int32 + } else { + user[0].SelectedTitle.UnlockScore = -1 + } + + if tmpUnlockWin.Valid { + user[0].SelectedTitle.UnlockWin = tmpUnlockWin.Int32 + } else { + user[0].SelectedTitle.UnlockWin = -1 + } + //return with no errors return user, nil } @@ -463,6 +493,9 @@ func (MySQLUserQuery) SelectAll() ([]schemas.InfoUserSchema, error) { var tmpSubjectDepartment sql.NullString var tmpSubjectDescription sql.NullString + var tmpUnlockScore sql.NullInt32 + var tmpUnlockWin sql.NullInt32 + errScan := rows.Scan( &tmpUser.ID, &tmpUser.Username, @@ -476,8 +509,8 @@ func (MySQLUserQuery) SelectAll() ([]schemas.InfoUserSchema, error) { &(tmpSubjectShortForm), &(tmpSubjectDepartment), &(tmpSubjectDescription), - &tmpUser.SelectedTitle.UnlockScore, - &tmpUser.SelectedTitle.UnlockWin, + &tmpUnlockScore, + &tmpUnlockWin, ) if errScan != nil { @@ -494,6 +527,18 @@ func (MySQLUserQuery) SelectAll() ([]schemas.InfoUserSchema, error) { } } + if tmpUnlockScore.Valid { + tmpUser.SelectedTitle.UnlockScore = tmpUnlockScore.Int32 + } else { + tmpUser.SelectedTitle.UnlockScore = -1 + } + + if tmpUnlockWin.Valid { + tmpUser.SelectedTitle.UnlockWin = tmpUnlockWin.Int32 + } else { + tmpUser.SelectedTitle.UnlockWin = -1 + } + users = append(users, tmpUser) } -- GitLab