diff options
| author | Kyren223 <Kyren223@proton.me> | 2024-11-23 18:47:26 +0200 |
|---|---|---|
| committer | Kyren223 <Kyren223@proton.me> | 2024-11-23 18:47:26 +0200 |
| commit | 77d9971982e336aa6c0b816d84f2d37780c02de6 (patch) | |
| tree | 0001051202a93b5b994d3fd5470e5bfc35a67af5 /internal | |
| parent | 63a1d24295122728820a61aa93b3da6cd6d401d9 (diff) | |
Added network queries
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/data/networks.sql.go | 77 | ||||
| -rw-r--r-- | internal/data/users.sql.go | 66 |
2 files changed, 60 insertions, 83 deletions
diff --git a/internal/data/networks.sql.go b/internal/data/networks.sql.go index efa468c..5a9457a 100644 --- a/internal/data/networks.sql.go +++ b/internal/data/networks.sql.go @@ -11,43 +11,58 @@ import ( "github.com/kyren223/eko/pkg/snowflake" ) -const createNetwork = `-- name: CreateNetwork :one -INSERT INTO networks ( - id, name, owner_id -) VALUES ( - ?, ?, ? -) -RETURNING id, owner_id, name, icon, bg_hex_color, fg_hex_color, is_public +const getBannedUsersInNetwork = `-- name: GetBannedUsersInNetwork :many +SELECT users.id, users.name, users.public_key, users.description, users.is_public_dm, users.is_deleted, network_banned_users.banned_at, network_banned_users.reason +FROM network_banned_users +JOIN users ON users.id = network_banned_users.banned_user_id +WHERE network_banned_users.network_id = ? ` -type CreateNetworkParams struct { - ID snowflake.ID - Name string - OwnerID snowflake.ID +type GetBannedUsersInNetworkRow struct { + User User + BannedAt string + Reason *string } -func (q *Queries) CreateNetwork(ctx context.Context, arg CreateNetworkParams) (Network, error) { - row := q.db.QueryRowContext(ctx, createNetwork, arg.ID, arg.Name, arg.OwnerID) - var i Network - err := row.Scan( - &i.ID, - &i.OwnerID, - &i.Name, - &i.Icon, - &i.BgHexColor, - &i.FgHexColor, - &i.IsPublic, - ) - return i, err +func (q *Queries) GetBannedUsersInNetwork(ctx context.Context, networkID snowflake.ID) ([]GetBannedUsersInNetworkRow, error) { + rows, err := q.db.QueryContext(ctx, getBannedUsersInNetwork, networkID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetBannedUsersInNetworkRow + for rows.Next() { + var i GetBannedUsersInNetworkRow + if err := rows.Scan( + &i.User.ID, + &i.User.Name, + &i.User.PublicKey, + &i.User.Description, + &i.User.IsPublicDm, + &i.User.IsDeleted, + &i.BannedAt, + &i.Reason, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil } -const getNetwork = `-- name: GetNetwork :one +const getNetworkById = `-- name: GetNetworkById :one SELECT id, owner_id, name, icon, bg_hex_color, fg_hex_color, is_public FROM networks WHERE id = ? ` -func (q *Queries) GetNetwork(ctx context.Context, id snowflake.ID) (Network, error) { - row := q.db.QueryRowContext(ctx, getNetwork, id) +func (q *Queries) GetNetworkById(ctx context.Context, id snowflake.ID) (Network, error) { + row := q.db.QueryRowContext(ctx, getNetworkById, id) var i Network err := row.Scan( &i.ID, @@ -61,13 +76,13 @@ func (q *Queries) GetNetwork(ctx context.Context, id snowflake.ID) (Network, err return i, err } -const listNetworks = `-- name: ListNetworks :many +const getPublicNetworks = `-- name: GetPublicNetworks :many SELECT id, owner_id, name, icon, bg_hex_color, fg_hex_color, is_public FROM networks -ORDER BY id +WHERE is_public = true ` -func (q *Queries) ListNetworks(ctx context.Context) ([]Network, error) { - rows, err := q.db.QueryContext(ctx, listNetworks) +func (q *Queries) GetPublicNetworks(ctx context.Context) ([]Network, error) { + rows, err := q.db.QueryContext(ctx, getPublicNetworks) if err != nil { return nil, err } diff --git a/internal/data/users.sql.go b/internal/data/users.sql.go index 7d2e065..9728c21 100644 --- a/internal/data/users.sql.go +++ b/internal/data/users.sql.go @@ -119,32 +119,6 @@ func (q *Queries) GetUserByPublicKey(ctx context.Context, publicKey ed25519.Publ return i, err } -const serUserPublicDMs = `-- name: SerUserPublicDMs :one -UPDATE users SET - is_public_dm = ? -WHERE id = ? AND is_deleted = false -RETURNING id, name, public_key, description, is_public_dm, is_deleted -` - -type SerUserPublicDMsParams struct { - IsPublicDm bool - ID snowflake.ID -} - -func (q *Queries) SerUserPublicDMs(ctx context.Context, arg SerUserPublicDMsParams) (User, error) { - row := q.db.QueryRowContext(ctx, serUserPublicDMs, arg.IsPublicDm, arg.ID) - var i User - err := row.Scan( - &i.ID, - &i.Name, - &i.PublicKey, - &i.Description, - &i.IsPublicDm, - &i.IsDeleted, - ) - return i, err -} - const setUserDescription = `-- name: SetUserDescription :one UPDATE users SET description = ? @@ -197,20 +171,20 @@ func (q *Queries) SetUserName(ctx context.Context, arg SetUserNameParams) (User, return i, err } -const setUserPublicKey = `-- name: SetUserPublicKey :one +const setUserPublicDMs = `-- name: SetUserPublicDMs :one UPDATE users SET - public_key = ? + is_public_dm = ? WHERE id = ? AND is_deleted = false RETURNING id, name, public_key, description, is_public_dm, is_deleted ` -type SetUserPublicKeyParams struct { - PublicKey ed25519.PublicKey - ID snowflake.ID +type SetUserPublicDMsParams struct { + IsPublicDm bool + ID snowflake.ID } -func (q *Queries) SetUserPublicKey(ctx context.Context, arg SetUserPublicKeyParams) (User, error) { - row := q.db.QueryRowContext(ctx, setUserPublicKey, arg.PublicKey, arg.ID) +func (q *Queries) SetUserPublicDMs(ctx context.Context, arg SetUserPublicDMsParams) (User, error) { + row := q.db.QueryRowContext(ctx, setUserPublicDMs, arg.IsPublicDm, arg.ID) var i User err := row.Scan( &i.ID, @@ -223,32 +197,20 @@ func (q *Queries) SetUserPublicKey(ctx context.Context, arg SetUserPublicKeyPara return i, err } -const updateUser = `-- name: UpdateUser :one +const setUserPublicKey = `-- name: SetUserPublicKey :one UPDATE users SET - name = COALESCE(?, name), - public_key = COALESCE(?, public_key), - description = COALESCE(?, description), - is_public_dm = COALESCE(?, is_public_dm) + public_key = ? WHERE id = ? AND is_deleted = false RETURNING id, name, public_key, description, is_public_dm, is_deleted ` -type UpdateUserParams struct { - Name string - PublicKey ed25519.PublicKey - Description *string - IsPublicDm bool - ID snowflake.ID +type SetUserPublicKeyParams struct { + PublicKey ed25519.PublicKey + ID snowflake.ID } -func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error) { - row := q.db.QueryRowContext(ctx, updateUser, - arg.Name, - arg.PublicKey, - arg.Description, - arg.IsPublicDm, - arg.ID, - ) +func (q *Queries) SetUserPublicKey(ctx context.Context, arg SetUserPublicKeyParams) (User, error) { + row := q.db.QueryRowContext(ctx, setUserPublicKey, arg.PublicKey, arg.ID) var i User err := row.Scan( &i.ID, |
