veza/veza-backend-api/internal/services/gear_service.go
senke edde637c8e feat(v0.501): Sprint 4 -- Cloud frontend + Gear advanced
- 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
2026-02-22 18:30:49 +01:00

92 lines
2.7 KiB
Go

package services
import (
"context"
"errors"
"fmt"
"github.com/google/uuid"
"go.uber.org/zap"
"veza-backend-api/internal/models"
"veza-backend-api/internal/repositories"
)
// GearService handles gear inventory business logic
type GearService struct {
repo repositories.GearRepository
logger *zap.Logger
}
// NewGearService creates a new GearService
func NewGearService(repo repositories.GearRepository, logger *zap.Logger) *GearService {
return &GearService{repo: repo, logger: logger}
}
// List returns all gear items for a user
func (s *GearService) List(ctx context.Context, userID uuid.UUID) ([]*models.GearItem, error) {
return s.repo.ListByUserID(ctx, userID)
}
// ListPublicGearByUsername returns public gear items for a given username
func (s *GearService) ListPublicGearByUsername(ctx context.Context, username string) ([]*models.GearItem, error) {
return s.repo.ListPublicByUsername(ctx, username)
}
// Search returns gear items matching a query for a user
func (s *GearService) Search(ctx context.Context, userID uuid.UUID, query string) ([]*models.GearItem, error) {
return s.repo.SearchByUserID(ctx, userID, query)
}
// Get returns a single gear item by ID (with ownership check)
func (s *GearService) Get(ctx context.Context, id, userID uuid.UUID) (*models.GearItem, error) {
item, err := s.repo.GetByID(ctx, id)
if err != nil {
return nil, err
}
if item.UserID != userID {
return nil, errors.New("gear item not found")
}
return item, nil
}
// Create creates a new gear item for a user
func (s *GearService) Create(ctx context.Context, userID uuid.UUID, item *models.GearItem) (*models.GearItem, error) {
if item.Name == "" {
return nil, errors.New("name is required")
}
item.UserID = userID
if err := s.repo.Create(ctx, item); err != nil {
return nil, fmt.Errorf("failed to create gear item: %w", err)
}
return item, nil
}
// Update updates an existing gear item (with ownership check)
func (s *GearService) Update(ctx context.Context, id, userID uuid.UUID, item *models.GearItem) (*models.GearItem, error) {
existing, err := s.repo.GetByID(ctx, id)
if err != nil {
return nil, err
}
if existing.UserID != userID {
return nil, errors.New("gear item not found")
}
item.ID = id
item.UserID = userID
if err := s.repo.Update(ctx, item); err != nil {
return nil, fmt.Errorf("failed to update gear item: %w", err)
}
return item, nil
}
// Delete deletes a gear item (with ownership check)
func (s *GearService) Delete(ctx context.Context, id, userID uuid.UUID) error {
existing, err := s.repo.GetByID(ctx, id)
if err != nil {
return err
}
if existing.UserID != userID {
return errors.New("gear item not found")
}
return s.repo.Delete(ctx, id)
}