friends/database/3ds/get_friend_persistent_infos.go
Daniel López Guimaraes 8d8a05a7e2
Add missing error handling
Now database getters do error handling aswell, and in case of any error,
the NEX or GRPC method throws a proper error about it.

There are still some doubts on how to handle errors when a list of data
is processed, so for now skip that element on the list and continue.

Also add some constant errors to do better error handling.
2023-08-13 23:19:34 +01:00

58 lines
1.9 KiB
Go

package database_3ds
import (
"database/sql"
"time"
"github.com/PretendoNetwork/friends/database"
"github.com/PretendoNetwork/friends/globals"
"github.com/PretendoNetwork/nex-go"
friends_3ds_types "github.com/PretendoNetwork/nex-protocols-go/friends-3ds/types"
)
// GetFriendPersistentInfos returns the persistent information of all friends
func GetFriendPersistentInfos(user1_pid uint32, pids []uint32) ([]*friends_3ds_types.FriendPersistentInfo, error) {
persistentInfos := make([]*friends_3ds_types.FriendPersistentInfo, 0)
rows, err := database.Postgres.Query(`
SELECT pid, region, area, language, favorite_title, favorite_title_version, comment, comment_changed, last_online FROM "3ds".user_data WHERE pid IN ($1)`, database.PIDArrayToString(pids))
if err != nil {
return persistentInfos, err
}
for rows.Next() {
persistentInfo := friends_3ds_types.NewFriendPersistentInfo()
gameKey := friends_3ds_types.NewGameKey()
var lastOnlineTime uint64
var msgUpdateTime uint64
var friendedAtTime uint64
rows.Scan(
&persistentInfo.PID, &persistentInfo.Region, &persistentInfo.Area, &persistentInfo.Language,
&gameKey.TitleID, &gameKey.TitleVersion, &persistentInfo.Message, &msgUpdateTime, &lastOnlineTime)
err = database.Postgres.QueryRow(`
SELECT date FROM "3ds".friendships WHERE user1_pid=$1 AND user2_pid=$2 AND type=0 LIMIT 1`, user1_pid, persistentInfo.PID).Scan(&friendedAtTime)
if err != nil {
if err == sql.ErrNoRows {
friendedAtTime = uint64(time.Now().Unix())
} else {
globals.Logger.Critical(err.Error())
continue
}
}
persistentInfo.MessageUpdatedAt = nex.NewDateTime(msgUpdateTime)
persistentInfo.FriendedAt = nex.NewDateTime(friendedAtTime)
persistentInfo.LastOnline = nex.NewDateTime(lastOnlineTime)
persistentInfo.GameKey = gameKey
persistentInfo.Platform = 2 // Always 3DS
persistentInfos = append(persistentInfos, persistentInfo)
}
return persistentInfos, nil
}