Fixed null user being returned if id not in database

This commit is contained in:
2025-02-22 20:02:27 +11:00
parent 9410056dfb
commit c9fd1c8af0

View File

@@ -56,21 +56,21 @@ func fetchUserData(
return rows, nil return rows, nil
} }
// Scan the next row into the provided user pointer. Calls rows.Next() and // Calls rows.Next() and scans the row into the provided user pointer.
// assumes only row in the result. Providing a rows object with more than 1 // Will error if no row available
// row may result in undefined behaviour.
func scanUserRow(user *User, rows *sql.Rows) error { func scanUserRow(user *User, rows *sql.Rows) error {
for rows.Next() { if !rows.Next() {
err := rows.Scan( return errors.New("User not found")
&user.ID, }
&user.Username, err := rows.Scan(
&user.Password_hash, &user.ID,
&user.Created_at, &user.Username,
&user.Bio, &user.Password_hash,
) &user.Created_at,
if err != nil { &user.Bio,
return errors.Wrap(err, "rows.Scan") )
} if err != nil {
return errors.Wrap(err, "rows.Scan")
} }
return nil return nil
} }