Files
geo/server/internal/tenant/repository/image_folder_repo.go
T
root ced0c4ec0f
Frontend CI / Frontend (push) Successful in 2m59s
Backend CI / Backend (push) Successful in 14m58s
Scope knowledge and image libraries by brand
2026-06-30 12:30:10 +08:00

117 lines
3.2 KiB
Go

package repository
import (
"context"
"time"
"github.com/geo-platform/tenant-api/internal/tenant/repository/generated"
)
type ImageFolder struct {
ID int64
TenantID int64
BrandID int64
Name string
ImageCount int
CreatedAt time.Time
UpdatedAt time.Time
}
type ImageFolderRepository interface {
List(ctx context.Context, tenantID, brandID int64) ([]ImageFolder, error)
Get(ctx context.Context, tenantID, brandID, id int64) (*ImageFolder, error)
Create(ctx context.Context, tenantID, brandID int64, name string) (int64, error)
Update(ctx context.Context, tenantID, brandID, id int64, name string) error
Delete(ctx context.Context, tenantID, brandID, id int64) error
CountActiveImages(ctx context.Context, tenantID, brandID, folderID int64) (int, error)
}
type imageFolderRepository struct {
q generated.Querier
}
func NewImageFolderRepository(db generated.DBTX) ImageFolderRepository {
return &imageFolderRepository{q: newQuerier(db)}
}
func (r *imageFolderRepository) List(ctx context.Context, tenantID, brandID int64) ([]ImageFolder, error) {
rows, err := r.q.ListImageFolders(ctx, generated.ListImageFoldersParams{
TenantID: tenantID,
BrandID: brandID,
})
if err != nil {
return nil, err
}
folders := make([]ImageFolder, len(rows))
for i, row := range rows {
folders[i] = ImageFolder{
ID: row.ID,
TenantID: row.TenantID,
BrandID: row.BrandID,
Name: row.Name,
ImageCount: int(row.ImageCount),
CreatedAt: timeFromTimestamp(row.CreatedAt),
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
}
}
return folders, nil
}
func (r *imageFolderRepository) Get(ctx context.Context, tenantID, brandID, id int64) (*ImageFolder, error) {
row, err := r.q.GetImageFolder(ctx, generated.GetImageFolderParams{
ID: id,
TenantID: tenantID,
BrandID: brandID,
})
if err != nil {
return nil, err
}
return &ImageFolder{
ID: row.ID,
TenantID: row.TenantID,
BrandID: row.BrandID,
Name: row.Name,
CreatedAt: timeFromTimestamp(row.CreatedAt),
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
}, nil
}
func (r *imageFolderRepository) Create(ctx context.Context, tenantID, brandID int64, name string) (int64, error) {
return r.q.CreateImageFolder(ctx, generated.CreateImageFolderParams{
TenantID: tenantID,
BrandID: brandID,
Name: name,
})
}
func (r *imageFolderRepository) Update(ctx context.Context, tenantID, brandID, id int64, name string) error {
return r.q.UpdateImageFolder(ctx, generated.UpdateImageFolderParams{
Name: name,
ID: id,
TenantID: tenantID,
BrandID: brandID,
})
}
func (r *imageFolderRepository) Delete(ctx context.Context, tenantID, brandID, id int64) error {
return r.q.DeleteImageFolder(ctx, generated.DeleteImageFolderParams{
ID: id,
TenantID: tenantID,
BrandID: brandID,
})
}
func (r *imageFolderRepository) CountActiveImages(ctx context.Context, tenantID, brandID, folderID int64) (int, error) {
count, err := r.q.CountActiveImagesInFolder(ctx, generated.CountActiveImagesInFolderParams{
TenantID: tenantID,
BrandID: brandID,
FolderID: pgInt8(&folderID),
})
if err != nil {
return 0, err
}
return int(count), nil
}