chore: build
This commit is contained in:
@@ -7,13 +7,25 @@ import (
|
||||
"io"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"backend/internal/modules/categories"
|
||||
"backend/internal/modules/products"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrFileTooLarge = errors.New("file exceeds the maximum allowed size")
|
||||
ErrUnsupportedType = errors.New("unsupported file type")
|
||||
ErrProductNotFound = errors.New("product not found")
|
||||
)
|
||||
|
||||
// ProductFinder is the minimal slice of products.Service this module needs,
|
||||
// defined on the consumer side so media never imports the products
|
||||
// module's handler/repository/service internals -- only used to validate
|
||||
// that a product_id passed on upload actually exists.
|
||||
type ProductFinder interface {
|
||||
Get(ctx context.Context, id uuid.UUID) (*products.Product, error)
|
||||
}
|
||||
|
||||
// allowedTypes maps accepted MIME types to a safe file extension. Anything
|
||||
// not in this list is rejected -- uploads are never trusted to declare
|
||||
// their own extension.
|
||||
@@ -27,24 +39,38 @@ var allowedTypes = map[string]string{
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
repo Repository
|
||||
storage Storage
|
||||
maxSizeByte int64
|
||||
repo Repository
|
||||
storage Storage
|
||||
maxSizeByte int64
|
||||
products ProductFinder
|
||||
categoriesRepo categories.Repository
|
||||
}
|
||||
|
||||
func NewService(repo Repository, storage Storage, maxSizeBytes int64) *Service {
|
||||
return &Service{repo: repo, storage: storage, maxSizeByte: maxSizeBytes}
|
||||
func NewService(
|
||||
repo Repository,
|
||||
storage Storage,
|
||||
products ProductFinder,
|
||||
categoriesRepo categories.Repository,
|
||||
maxSizeByte int64,
|
||||
) *Service {
|
||||
return &Service{
|
||||
repo: repo,
|
||||
storage: storage,
|
||||
products: products,
|
||||
categoriesRepo: categoriesRepo,
|
||||
maxSizeByte: maxSizeByte,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context) ([]*Media, error) {
|
||||
return s.repo.List(ctx)
|
||||
func (s *Service) List(ctx context.Context, productID *uuid.UUID) ([]*Media, error) {
|
||||
return s.repo.List(ctx, productID)
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, id uuid.UUID) (*Media, error) {
|
||||
return s.repo.FindByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Service) Upload(ctx context.Context, filename string, reader io.Reader, size int64, contentType, altText string) (*Media, error) {
|
||||
func (s *Service) Upload(ctx context.Context, filename string, reader io.Reader, size int64, contentType, altText string, productID *uuid.UUID) (*Media, error) {
|
||||
if size > s.maxSizeByte {
|
||||
return nil, ErrFileTooLarge
|
||||
}
|
||||
@@ -52,6 +78,14 @@ func (s *Service) Upload(ctx context.Context, filename string, reader io.Reader,
|
||||
if !ok {
|
||||
return nil, ErrUnsupportedType
|
||||
}
|
||||
if productID != nil {
|
||||
if _, err := s.products.Get(ctx, *productID); err != nil {
|
||||
if errors.Is(err, products.ErrNotFound) {
|
||||
return nil, ErrProductNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("check product: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
key := uuid.NewString() + ext
|
||||
url, err := s.storage.Save(ctx, key, reader, size, contentType)
|
||||
@@ -67,6 +101,7 @@ func (s *Service) Upload(ctx context.Context, filename string, reader io.Reader,
|
||||
MimeType: contentType,
|
||||
SizeBytes: size,
|
||||
AltText: altText,
|
||||
ProductID: productID,
|
||||
}
|
||||
if err := s.repo.Create(ctx, m); err != nil {
|
||||
_ = s.storage.Delete(ctx, key)
|
||||
@@ -97,3 +132,23 @@ func (s *Service) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
}
|
||||
return s.repo.Delete(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Service) BelongsToProduct(
|
||||
ctx context.Context,
|
||||
mediaID uuid.UUID,
|
||||
productID uuid.UUID,
|
||||
) (bool, error) {
|
||||
m, err := s.repo.FindByID(ctx, mediaID)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
if m.ProductID == nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return *m.ProductID == productID, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user