Files
geo/server/internal/tenant/repository/image_folder_repo.go
T

117 lines
3.2 KiB
Go
Raw Normal View History

2026-04-16 20:40:41 +08:00
package repository
import (
"context"
"time"
"github.com/geo-platform/tenant-api/internal/tenant/repository/generated"
)
type ImageFolder struct {
ID int64
TenantID int64
BrandID int64
2026-04-16 20:40:41 +08:00
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)
2026-04-16 20:40:41 +08:00
}
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,
})
2026-04-16 20:40:41 +08:00
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,
2026-04-16 20:40:41 +08:00
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) {
2026-04-16 20:40:41 +08:00
row, err := r.q.GetImageFolder(ctx, generated.GetImageFolderParams{
ID: id,
TenantID: tenantID,
BrandID: brandID,
2026-04-16 20:40:41 +08:00
})
if err != nil {
return nil, err
}
return &ImageFolder{
ID: row.ID,
TenantID: row.TenantID,
BrandID: row.BrandID,
2026-04-16 20:40:41 +08:00
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) {
2026-04-16 20:40:41 +08:00
return r.q.CreateImageFolder(ctx, generated.CreateImageFolderParams{
TenantID: tenantID,
BrandID: brandID,
2026-04-16 20:40:41 +08:00
Name: name,
})
}
func (r *imageFolderRepository) Update(ctx context.Context, tenantID, brandID, id int64, name string) error {
2026-04-16 20:40:41 +08:00
return r.q.UpdateImageFolder(ctx, generated.UpdateImageFolderParams{
Name: name,
ID: id,
TenantID: tenantID,
BrandID: brandID,
2026-04-16 20:40:41 +08:00
})
}
func (r *imageFolderRepository) Delete(ctx context.Context, tenantID, brandID, id int64) error {
2026-04-16 20:40:41 +08:00
return r.q.DeleteImageFolder(ctx, generated.DeleteImageFolderParams{
ID: id,
TenantID: tenantID,
BrandID: brandID,
2026-04-16 20:40:41 +08:00
})
}
func (r *imageFolderRepository) CountActiveImages(ctx context.Context, tenantID, brandID, folderID int64) (int, error) {
2026-04-16 20:40:41 +08:00
count, err := r.q.CountActiveImagesInFolder(ctx, generated.CountActiveImagesInFolderParams{
TenantID: tenantID,
BrandID: brandID,
2026-04-16 20:40:41 +08:00
FolderID: pgInt8(&folderID),
})
if err != nil {
return 0, err
}
return int(count), nil
}