mirror of
https://github.com/PrivateCaptcha/PrivateCaptcha.git
synced 2026-02-09 07:19:08 -06:00
254 lines
7.6 KiB
Go
254 lines
7.6 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: organizations.sql
|
|
|
|
package generated
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const createOrganization = `-- name: CreateOrganization :one
|
|
INSERT INTO backend.organizations (name, user_id) VALUES ($1, $2) RETURNING id, name, user_id, created_at, updated_at, deleted_at
|
|
`
|
|
|
|
type CreateOrganizationParams struct {
|
|
Name string `db:"name" json:"name"`
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) CreateOrganization(ctx context.Context, arg *CreateOrganizationParams) (*Organization, error) {
|
|
row := q.db.QueryRow(ctx, createOrganization, arg.Name, arg.UserID)
|
|
var i Organization
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.UserID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const deleteOrganizations = `-- name: DeleteOrganizations :exec
|
|
DELETE FROM backend.organizations WHERE id = ANY($1::INT[])
|
|
`
|
|
|
|
func (q *Queries) DeleteOrganizations(ctx context.Context, dollar_1 []int32) error {
|
|
_, err := q.db.Exec(ctx, deleteOrganizations, dollar_1)
|
|
return err
|
|
}
|
|
|
|
const findUserOrgByName = `-- name: FindUserOrgByName :one
|
|
SELECT id, name, user_id, created_at, updated_at, deleted_at from backend.organizations WHERE user_id = $1 AND name = $2 AND deleted_at IS NULL
|
|
`
|
|
|
|
type FindUserOrgByNameParams struct {
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
Name string `db:"name" json:"name"`
|
|
}
|
|
|
|
func (q *Queries) FindUserOrgByName(ctx context.Context, arg *FindUserOrgByNameParams) (*Organization, error) {
|
|
row := q.db.QueryRow(ctx, findUserOrgByName, arg.UserID, arg.Name)
|
|
var i Organization
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.UserID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const getOrganizationWithAccess = `-- name: GetOrganizationWithAccess :one
|
|
SELECT o.id, o.name, o.user_id, o.created_at, o.updated_at, o.deleted_at, ou.level
|
|
FROM backend.organizations o
|
|
LEFT JOIN backend.organization_users ou ON
|
|
o.id = ou.org_id
|
|
AND ou.user_id = $2
|
|
AND o.user_id != $2 -- Only do the join if user isn't the owner
|
|
WHERE o.id = $1
|
|
`
|
|
|
|
type GetOrganizationWithAccessParams struct {
|
|
ID int32 `db:"id" json:"id"`
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
}
|
|
|
|
type GetOrganizationWithAccessRow struct {
|
|
Organization Organization `db:"organization" json:"organization"`
|
|
Level NullAccessLevel `db:"level" json:"level"`
|
|
}
|
|
|
|
func (q *Queries) GetOrganizationWithAccess(ctx context.Context, arg *GetOrganizationWithAccessParams) (*GetOrganizationWithAccessRow, error) {
|
|
row := q.db.QueryRow(ctx, getOrganizationWithAccess, arg.ID, arg.UserID)
|
|
var i GetOrganizationWithAccessRow
|
|
err := row.Scan(
|
|
&i.Organization.ID,
|
|
&i.Organization.Name,
|
|
&i.Organization.UserID,
|
|
&i.Organization.CreatedAt,
|
|
&i.Organization.UpdatedAt,
|
|
&i.Organization.DeletedAt,
|
|
&i.Level,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const getSoftDeletedOrganizations = `-- name: GetSoftDeletedOrganizations :many
|
|
SELECT o.id, o.name, o.user_id, o.created_at, o.updated_at, o.deleted_at
|
|
FROM backend.organizations o
|
|
JOIN backend.users u ON o.user_id = u.id
|
|
WHERE o.deleted_at IS NOT NULL
|
|
AND o.deleted_at < $1
|
|
AND u.deleted_at IS NULL
|
|
LIMIT $2
|
|
`
|
|
|
|
type GetSoftDeletedOrganizationsParams struct {
|
|
DeletedAt pgtype.Timestamptz `db:"deleted_at" json:"deleted_at"`
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
}
|
|
|
|
type GetSoftDeletedOrganizationsRow struct {
|
|
Organization Organization `db:"organization" json:"organization"`
|
|
}
|
|
|
|
func (q *Queries) GetSoftDeletedOrganizations(ctx context.Context, arg *GetSoftDeletedOrganizationsParams) ([]*GetSoftDeletedOrganizationsRow, error) {
|
|
rows, err := q.db.Query(ctx, getSoftDeletedOrganizations, arg.DeletedAt, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*GetSoftDeletedOrganizationsRow
|
|
for rows.Next() {
|
|
var i GetSoftDeletedOrganizationsRow
|
|
if err := rows.Scan(
|
|
&i.Organization.ID,
|
|
&i.Organization.Name,
|
|
&i.Organization.UserID,
|
|
&i.Organization.CreatedAt,
|
|
&i.Organization.UpdatedAt,
|
|
&i.Organization.DeletedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getUserOrganizations = `-- name: GetUserOrganizations :many
|
|
SELECT o.id, o.name, o.user_id, o.created_at, o.updated_at, o.deleted_at, 'owner'::backend.access_level as level FROM backend.organizations o WHERE o.user_id = $1 AND o.deleted_at IS NULL
|
|
UNION ALL
|
|
SELECT o.id, o.name, o.user_id, o.created_at, o.updated_at, o.deleted_at, ou.level
|
|
FROM backend.organizations o
|
|
JOIN backend.organization_users ou ON o.id = ou.org_id
|
|
WHERE ou.user_id = $1 AND o.deleted_at IS NULL
|
|
`
|
|
|
|
type GetUserOrganizationsRow struct {
|
|
Organization Organization `db:"organization" json:"organization"`
|
|
Level AccessLevel `db:"level" json:"level"`
|
|
}
|
|
|
|
func (q *Queries) GetUserOrganizations(ctx context.Context, userID pgtype.Int4) ([]*GetUserOrganizationsRow, error) {
|
|
rows, err := q.db.Query(ctx, getUserOrganizations, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*GetUserOrganizationsRow
|
|
for rows.Next() {
|
|
var i GetUserOrganizationsRow
|
|
if err := rows.Scan(
|
|
&i.Organization.ID,
|
|
&i.Organization.Name,
|
|
&i.Organization.UserID,
|
|
&i.Organization.CreatedAt,
|
|
&i.Organization.UpdatedAt,
|
|
&i.Organization.DeletedAt,
|
|
&i.Level,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const softDeleteUserOrganization = `-- name: SoftDeleteUserOrganization :exec
|
|
UPDATE backend.organizations SET deleted_at = NOW(), updated_at = NOW(), name = name || ' deleted_' || substr(md5(random()::text), 1, 8) WHERE id = $1 AND user_id = $2
|
|
`
|
|
|
|
type SoftDeleteUserOrganizationParams struct {
|
|
ID int32 `db:"id" json:"id"`
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) SoftDeleteUserOrganization(ctx context.Context, arg *SoftDeleteUserOrganizationParams) error {
|
|
_, err := q.db.Exec(ctx, softDeleteUserOrganization, arg.ID, arg.UserID)
|
|
return err
|
|
}
|
|
|
|
const softDeleteUserOrganizations = `-- name: SoftDeleteUserOrganizations :exec
|
|
UPDATE backend.organizations SET deleted_at = NOW(), updated_at = NOW(), name = name || ' deleted_' || substr(md5(random()::text), 1, 8) WHERE user_id = $1
|
|
`
|
|
|
|
func (q *Queries) SoftDeleteUserOrganizations(ctx context.Context, userID pgtype.Int4) error {
|
|
_, err := q.db.Exec(ctx, softDeleteUserOrganizations, userID)
|
|
return err
|
|
}
|
|
|
|
const transferOrganization = `-- name: TransferOrganization :exec
|
|
UPDATE backend.organizations SET user_id = $2, updated_at = NOW() WHERE id = $1 AND user_id = $3
|
|
`
|
|
|
|
type TransferOrganizationParams struct {
|
|
ID int32 `db:"id" json:"id"`
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
UserID_2 pgtype.Int4 `db:"user_id_2" json:"user_id_2"`
|
|
}
|
|
|
|
func (q *Queries) TransferOrganization(ctx context.Context, arg *TransferOrganizationParams) error {
|
|
_, err := q.db.Exec(ctx, transferOrganization, arg.ID, arg.UserID, arg.UserID_2)
|
|
return err
|
|
}
|
|
|
|
const updateOrganization = `-- name: UpdateOrganization :one
|
|
UPDATE backend.organizations SET name = $1, updated_at = NOW()
|
|
WHERE id = $2
|
|
RETURNING id, name, user_id, created_at, updated_at, deleted_at
|
|
`
|
|
|
|
type UpdateOrganizationParams struct {
|
|
Name string `db:"name" json:"name"`
|
|
ID int32 `db:"id" json:"id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateOrganization(ctx context.Context, arg *UpdateOrganizationParams) (*Organization, error) {
|
|
row := q.db.QueryRow(ctx, updateOrganization, arg.Name, arg.ID)
|
|
var i Organization
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.UserID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.DeletedAt,
|
|
)
|
|
return &i, err
|
|
}
|