27389164b0
- Implemented image folder repository with CRUD operations. - Added image reference repository for managing image references to articles. - Created image repository for handling image assets, including listing, inserting, updating, and deleting images. - Introduced image usage repository to track storage usage and quotas for tenants. - Added SQL queries for image assets, folders, references, and usage. - Developed image handler for HTTP endpoints to manage images and folders. - Created database migration scripts for image-related tables and structures.
329 lines
8.9 KiB
Go
329 lines
8.9 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: image.sql
|
|
|
|
package generated
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const countImageAssets = `-- name: CountImageAssets :one
|
|
SELECT COUNT(*)::INT AS total
|
|
FROM image_assets
|
|
WHERE tenant_id = $1
|
|
AND status = 'active'
|
|
AND deleted_at IS NULL
|
|
AND (
|
|
$2::BIGINT IS NULL
|
|
OR ($2::BIGINT = 0 AND folder_id IS NULL)
|
|
OR folder_id = $2::BIGINT
|
|
)
|
|
AND ($3::TEXT IS NULL OR name ILIKE '%' || $3::TEXT || '%')
|
|
`
|
|
|
|
type CountImageAssetsParams struct {
|
|
TenantID int64 `json:"tenant_id"`
|
|
FolderID pgtype.Int8 `json:"folder_id"`
|
|
SearchQuery pgtype.Text `json:"search_query"`
|
|
}
|
|
|
|
func (q *Queries) CountImageAssets(ctx context.Context, arg CountImageAssetsParams) (int32, error) {
|
|
row := q.db.QueryRow(ctx, countImageAssets, arg.TenantID, arg.FolderID, arg.SearchQuery)
|
|
var total int32
|
|
err := row.Scan(&total)
|
|
return total, err
|
|
}
|
|
|
|
const countUncategorizedImages = `-- name: CountUncategorizedImages :one
|
|
SELECT COUNT(*)::INT AS count
|
|
FROM image_assets
|
|
WHERE tenant_id = $1
|
|
AND folder_id IS NULL
|
|
AND status = 'active'
|
|
AND deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) CountUncategorizedImages(ctx context.Context, tenantID int64) (int32, error) {
|
|
row := q.db.QueryRow(ctx, countUncategorizedImages, tenantID)
|
|
var count int32
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const getImageAsset = `-- name: GetImageAsset :one
|
|
SELECT id, tenant_id, folder_id, object_key, name, size_bytes, status, created_by, created_at, updated_at
|
|
FROM image_assets
|
|
WHERE id = $1 AND tenant_id = $2
|
|
`
|
|
|
|
type GetImageAssetParams struct {
|
|
ID int64 `json:"id"`
|
|
TenantID int64 `json:"tenant_id"`
|
|
}
|
|
|
|
type GetImageAssetRow struct {
|
|
ID int64 `json:"id"`
|
|
TenantID int64 `json:"tenant_id"`
|
|
FolderID pgtype.Int8 `json:"folder_id"`
|
|
ObjectKey string `json:"object_key"`
|
|
Name string `json:"name"`
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
Status string `json:"status"`
|
|
CreatedBy int64 `json:"created_by"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) GetImageAsset(ctx context.Context, arg GetImageAssetParams) (GetImageAssetRow, error) {
|
|
row := q.db.QueryRow(ctx, getImageAsset, arg.ID, arg.TenantID)
|
|
var i GetImageAssetRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TenantID,
|
|
&i.FolderID,
|
|
&i.ObjectKey,
|
|
&i.Name,
|
|
&i.SizeBytes,
|
|
&i.Status,
|
|
&i.CreatedBy,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const insertImageAsset = `-- name: InsertImageAsset :one
|
|
INSERT INTO image_assets (tenant_id, folder_id, object_key, name, size_bytes, status, created_by)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
RETURNING id
|
|
`
|
|
|
|
type InsertImageAssetParams struct {
|
|
TenantID int64 `json:"tenant_id"`
|
|
FolderID pgtype.Int8 `json:"folder_id"`
|
|
ObjectKey string `json:"object_key"`
|
|
Name string `json:"name"`
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
Status string `json:"status"`
|
|
CreatedBy int64 `json:"created_by"`
|
|
}
|
|
|
|
func (q *Queries) InsertImageAsset(ctx context.Context, arg InsertImageAssetParams) (int64, error) {
|
|
row := q.db.QueryRow(ctx, insertImageAsset,
|
|
arg.TenantID,
|
|
arg.FolderID,
|
|
arg.ObjectKey,
|
|
arg.Name,
|
|
arg.SizeBytes,
|
|
arg.Status,
|
|
arg.CreatedBy,
|
|
)
|
|
var id int64
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const listActiveImagesByFolder = `-- name: ListActiveImagesByFolder :many
|
|
SELECT id, object_key, size_bytes
|
|
FROM image_assets
|
|
WHERE tenant_id = $1
|
|
AND folder_id = $2
|
|
AND status = 'active'
|
|
AND deleted_at IS NULL
|
|
`
|
|
|
|
type ListActiveImagesByFolderParams struct {
|
|
TenantID int64 `json:"tenant_id"`
|
|
FolderID pgtype.Int8 `json:"folder_id"`
|
|
}
|
|
|
|
type ListActiveImagesByFolderRow struct {
|
|
ID int64 `json:"id"`
|
|
ObjectKey string `json:"object_key"`
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
}
|
|
|
|
func (q *Queries) ListActiveImagesByFolder(ctx context.Context, arg ListActiveImagesByFolderParams) ([]ListActiveImagesByFolderRow, error) {
|
|
rows, err := q.db.Query(ctx, listActiveImagesByFolder, arg.TenantID, arg.FolderID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListActiveImagesByFolderRow
|
|
for rows.Next() {
|
|
var i ListActiveImagesByFolderRow
|
|
if err := rows.Scan(&i.ID, &i.ObjectKey, &i.SizeBytes); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listImageAssets = `-- name: ListImageAssets :many
|
|
SELECT id, tenant_id, folder_id, object_key, name, size_bytes, status, created_by, created_at, updated_at
|
|
FROM image_assets
|
|
WHERE tenant_id = $1
|
|
AND status = 'active'
|
|
AND deleted_at IS NULL
|
|
AND (
|
|
$2::BIGINT IS NULL
|
|
OR ($2::BIGINT = 0 AND folder_id IS NULL)
|
|
OR folder_id = $2::BIGINT
|
|
)
|
|
AND ($3::TEXT IS NULL OR name ILIKE '%' || $3::TEXT || '%')
|
|
ORDER BY created_at DESC
|
|
LIMIT $5 OFFSET $4
|
|
`
|
|
|
|
type ListImageAssetsParams struct {
|
|
TenantID int64 `json:"tenant_id"`
|
|
FolderID pgtype.Int8 `json:"folder_id"`
|
|
SearchQuery pgtype.Text `json:"search_query"`
|
|
PageOffset int32 `json:"page_offset"`
|
|
PageSize int32 `json:"page_size"`
|
|
}
|
|
|
|
type ListImageAssetsRow struct {
|
|
ID int64 `json:"id"`
|
|
TenantID int64 `json:"tenant_id"`
|
|
FolderID pgtype.Int8 `json:"folder_id"`
|
|
ObjectKey string `json:"object_key"`
|
|
Name string `json:"name"`
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
Status string `json:"status"`
|
|
CreatedBy int64 `json:"created_by"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) ListImageAssets(ctx context.Context, arg ListImageAssetsParams) ([]ListImageAssetsRow, error) {
|
|
rows, err := q.db.Query(ctx, listImageAssets,
|
|
arg.TenantID,
|
|
arg.FolderID,
|
|
arg.SearchQuery,
|
|
arg.PageOffset,
|
|
arg.PageSize,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListImageAssetsRow
|
|
for rows.Next() {
|
|
var i ListImageAssetsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.TenantID,
|
|
&i.FolderID,
|
|
&i.ObjectKey,
|
|
&i.Name,
|
|
&i.SizeBytes,
|
|
&i.Status,
|
|
&i.CreatedBy,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const markImagesInFolderPendingDelete = `-- name: MarkImagesInFolderPendingDelete :exec
|
|
UPDATE image_assets
|
|
SET status = 'pending_delete', folder_id = NULL, updated_at = NOW()
|
|
WHERE tenant_id = $1
|
|
AND folder_id = $2
|
|
AND status = 'active'
|
|
AND deleted_at IS NULL
|
|
`
|
|
|
|
type MarkImagesInFolderPendingDeleteParams struct {
|
|
TenantID int64 `json:"tenant_id"`
|
|
FolderID pgtype.Int8 `json:"folder_id"`
|
|
}
|
|
|
|
func (q *Queries) MarkImagesInFolderPendingDelete(ctx context.Context, arg MarkImagesInFolderPendingDeleteParams) error {
|
|
_, err := q.db.Exec(ctx, markImagesInFolderPendingDelete, arg.TenantID, arg.FolderID)
|
|
return err
|
|
}
|
|
|
|
const updateImageAssetDeleted = `-- name: UpdateImageAssetDeleted :exec
|
|
UPDATE image_assets
|
|
SET status = 'deleted', deleted_at = NOW(), updated_at = NOW()
|
|
WHERE id = $1 AND tenant_id = $2
|
|
`
|
|
|
|
type UpdateImageAssetDeletedParams struct {
|
|
ID int64 `json:"id"`
|
|
TenantID int64 `json:"tenant_id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateImageAssetDeleted(ctx context.Context, arg UpdateImageAssetDeletedParams) error {
|
|
_, err := q.db.Exec(ctx, updateImageAssetDeleted, arg.ID, arg.TenantID)
|
|
return err
|
|
}
|
|
|
|
const updateImageAssetFolder = `-- name: UpdateImageAssetFolder :exec
|
|
UPDATE image_assets
|
|
SET folder_id = $1, updated_at = NOW()
|
|
WHERE id = $2 AND tenant_id = $3
|
|
`
|
|
|
|
type UpdateImageAssetFolderParams struct {
|
|
FolderID pgtype.Int8 `json:"folder_id"`
|
|
ID int64 `json:"id"`
|
|
TenantID int64 `json:"tenant_id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateImageAssetFolder(ctx context.Context, arg UpdateImageAssetFolderParams) error {
|
|
_, err := q.db.Exec(ctx, updateImageAssetFolder, arg.FolderID, arg.ID, arg.TenantID)
|
|
return err
|
|
}
|
|
|
|
const updateImageAssetName = `-- name: UpdateImageAssetName :exec
|
|
UPDATE image_assets
|
|
SET name = $1, updated_at = NOW()
|
|
WHERE id = $2 AND tenant_id = $3
|
|
`
|
|
|
|
type UpdateImageAssetNameParams struct {
|
|
Name string `json:"name"`
|
|
ID int64 `json:"id"`
|
|
TenantID int64 `json:"tenant_id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateImageAssetName(ctx context.Context, arg UpdateImageAssetNameParams) error {
|
|
_, err := q.db.Exec(ctx, updateImageAssetName, arg.Name, arg.ID, arg.TenantID)
|
|
return err
|
|
}
|
|
|
|
const updateImageAssetStatus = `-- name: UpdateImageAssetStatus :exec
|
|
UPDATE image_assets
|
|
SET status = $1, updated_at = NOW()
|
|
WHERE id = $2 AND tenant_id = $3
|
|
`
|
|
|
|
type UpdateImageAssetStatusParams struct {
|
|
Status string `json:"status"`
|
|
ID int64 `json:"id"`
|
|
TenantID int64 `json:"tenant_id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateImageAssetStatus(ctx context.Context, arg UpdateImageAssetStatusParams) error {
|
|
_, err := q.db.Exec(ctx, updateImageAssetStatus, arg.Status, arg.ID, arg.TenantID)
|
|
return err
|
|
}
|