- C1-09: Create CloudPage with folder tree, file list, and /cloud route - C1-10: Create CloudUploadModal with drag-and-drop and progress - C1-11: Create CloudFilePreview mini player inline - C1-12: Add Cloud stories (loading, empty, populated, quota full) - G1-01: Add is_public toggle, public gear endpoint, GearShowcase - G1-02: Add gear image upload endpoints, GearImageGallery component - G1-03: Add gear search with ILIKE + SearchBar in toolbar - G1-04: Add stories for GearShowcase and GearImageGallery
77 lines
2.5 KiB
Go
77 lines
2.5 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
|
|
"veza-backend-api/internal/models"
|
|
)
|
|
|
|
// GearRepository defines the interface for gear item operations
|
|
type GearRepository interface {
|
|
Create(ctx context.Context, item *models.GearItem) error
|
|
GetByID(ctx context.Context, id uuid.UUID) (*models.GearItem, error)
|
|
ListByUserID(ctx context.Context, userID uuid.UUID) ([]*models.GearItem, error)
|
|
SearchByUserID(ctx context.Context, userID uuid.UUID, query string) ([]*models.GearItem, error)
|
|
ListPublicByUsername(ctx context.Context, username string) ([]*models.GearItem, error)
|
|
Update(ctx context.Context, item *models.GearItem) error
|
|
Delete(ctx context.Context, id uuid.UUID) error
|
|
}
|
|
|
|
type gearRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewGearRepository creates a new GearRepository
|
|
func NewGearRepository(db *gorm.DB) GearRepository {
|
|
return &gearRepository{db: db}
|
|
}
|
|
|
|
func (r *gearRepository) Create(ctx context.Context, item *models.GearItem) error {
|
|
return r.db.WithContext(ctx).Create(item).Error
|
|
}
|
|
|
|
func (r *gearRepository) GetByID(ctx context.Context, id uuid.UUID) (*models.GearItem, error) {
|
|
var item models.GearItem
|
|
if err := r.db.WithContext(ctx).First(&item, "id = ?", id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (r *gearRepository) ListByUserID(ctx context.Context, userID uuid.UUID) ([]*models.GearItem, error) {
|
|
var items []*models.GearItem
|
|
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).Find(&items).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (r *gearRepository) SearchByUserID(ctx context.Context, userID uuid.UUID, query string) ([]*models.GearItem, error) {
|
|
var items []*models.GearItem
|
|
pattern := "%" + query + "%"
|
|
err := r.db.WithContext(ctx).
|
|
Where("user_id = ? AND (name ILIKE ? OR brand ILIKE ? OR category ILIKE ?)", userID, pattern, pattern, pattern).
|
|
Order("updated_at DESC").
|
|
Find(&items).Error
|
|
return items, err
|
|
}
|
|
|
|
func (r *gearRepository) ListPublicByUsername(ctx context.Context, username string) ([]*models.GearItem, error) {
|
|
var items []*models.GearItem
|
|
err := r.db.WithContext(ctx).
|
|
Joins("JOIN users ON users.id = gear_items.user_id").
|
|
Where("users.username = ? AND gear_items.is_public = ?", username, true).
|
|
Find(&items).Error
|
|
return items, err
|
|
}
|
|
|
|
func (r *gearRepository) Update(ctx context.Context, item *models.GearItem) error {
|
|
return r.db.WithContext(ctx).Save(item).Error
|
|
}
|
|
|
|
func (r *gearRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
|
return r.db.WithContext(ctx).Delete(&models.GearItem{}, "id = ?", id).Error
|
|
}
|