mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
Fix 70+ errcheck violations by adding explicit error discards (`_ =`) for unchecked return values across test helpers and deferred Close() calls. Remove unused `senderID` field from broadcastMsg and unused `defaultCleanupMaxWindow` const. Apply De Morgan's law, remove empty branch, and simplify redundant type declaration per staticcheck.
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// GetRoleByID returns the role with the given ID, or nil if not found.
|
|
func (d *DB) GetRoleByID(id int64) (*Role, error) {
|
|
row := d.sqlDB.QueryRow(
|
|
`SELECT id, name, color, permissions, position, is_default FROM roles WHERE id = ?`,
|
|
id,
|
|
)
|
|
r := &Role{}
|
|
var isDefault int
|
|
err := row.Scan(&r.ID, &r.Name, &r.Color, &r.Permissions, &r.Position, &isDefault)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("GetRoleByID: %w", err)
|
|
}
|
|
r.IsDefault = isDefault != 0
|
|
return r, nil
|
|
}
|
|
|
|
// ListRoles returns all roles ordered by position descending.
|
|
func (d *DB) ListRoles() ([]*Role, error) {
|
|
rows, err := d.sqlDB.Query(
|
|
`SELECT id, name, color, permissions, position, is_default FROM roles ORDER BY position DESC`,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ListRoles: %w", err)
|
|
}
|
|
defer rows.Close() //nolint:errcheck
|
|
|
|
var roles []*Role
|
|
for rows.Next() {
|
|
r := &Role{}
|
|
var isDefault int
|
|
if err := rows.Scan(&r.ID, &r.Name, &r.Color, &r.Permissions, &r.Position, &isDefault); err != nil {
|
|
return nil, fmt.Errorf("ListRoles scan: %w", err)
|
|
}
|
|
r.IsDefault = isDefault != 0
|
|
roles = append(roles, r)
|
|
}
|
|
return roles, rows.Err()
|
|
}
|