chore: build
ci-api / test (push) Failing after 8m7s
ci-web / test (push) Failing after 5m5s

This commit is contained in:
Xor290
2026-09-20 12:18:33 +02:00
parent 8f4c7fa47a
commit 919c807004
174 changed files with 9669 additions and 1308 deletions
@@ -0,0 +1,5 @@
package products
import "errors"
var ErrMediaNotOwned = errors.New("media does not belong to product")
+44 -46
View File
@@ -22,30 +22,26 @@ type galleryItemResponse struct {
}
type productResponse struct {
ID uuid.UUID `json:"id"`
CategoryID *uuid.UUID `json:"category_id,omitempty"`
Name string `json:"name"`
Slug string `json:"slug"`
ShortDescription string `json:"short_description"`
Description string `json:"description"`
IsActive bool `json:"is_active"`
IsFeatured bool `json:"is_featured"`
PrimaryMediaID *uuid.UUID `json:"primary_media_id,omitempty"`
Position int `json:"position"`
ID uuid.UUID `json:"id"`
CategoryID uuid.UUID `json:"category_id"`
Name string `json:"name"`
Description string `json:"description"`
IsActive bool `json:"is_active"`
IsFeatured bool `json:"is_featured"`
PrimaryMediaID *uuid.UUID `json:"primary_media_id,omitempty"`
Position int `json:"position"`
}
func toResponse(p *Product) productResponse {
return productResponse{
ID: p.ID,
CategoryID: p.CategoryID,
Name: p.Name,
Slug: p.Slug,
ShortDescription: p.ShortDescription,
Description: p.Description,
IsActive: p.IsActive,
IsFeatured: p.IsFeatured,
PrimaryMediaID: p.PrimaryMediaID,
Position: p.Position,
ID: p.ID,
CategoryID: p.CategoryID,
Name: p.Name,
Description: p.Description,
IsActive: p.IsActive,
IsFeatured: p.IsFeatured,
PrimaryMediaID: p.PrimaryMediaID,
Position: p.Position,
}
}
@@ -101,8 +97,13 @@ func (h *Handler) GetAdmin(c *gin.Context) {
c.JSON(http.StatusOK, toResponse(p))
}
func (h *Handler) GetPublicBySlug(c *gin.Context) {
p, err := h.service.GetBySlug(c.Request.Context(), c.Param("slug"))
func (h *Handler) GetPublic(c *gin.Context) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
p, err := h.service.Get(c.Request.Context(), id)
if err != nil {
h.respondGetError(c, err)
return
@@ -123,28 +124,24 @@ func (h *Handler) respondGetError(c *gin.Context, err error) {
}
type upsertRequest struct {
CategoryID *uuid.UUID `json:"category_id"`
Name string `json:"name" binding:"required"`
Slug string `json:"slug" binding:"required"`
ShortDescription string `json:"short_description"`
Description string `json:"description"`
IsActive bool `json:"is_active"`
IsFeatured bool `json:"is_featured"`
PrimaryMediaID *uuid.UUID `json:"primary_media_id"`
Position int `json:"position"`
CategoryID uuid.UUID `json:"category_id" binding:"required"`
Name string `json:"name" binding:"required"`
Description string `json:"description"`
IsActive bool `json:"is_active"`
IsFeatured bool `json:"is_featured"`
PrimaryMediaID *uuid.UUID `json:"primary_media_id"`
Position int `json:"position"`
}
func (req upsertRequest) toInput() UpsertInput {
return UpsertInput{
CategoryID: req.CategoryID,
Name: req.Name,
Slug: req.Slug,
ShortDescription: req.ShortDescription,
Description: req.Description,
IsActive: req.IsActive,
IsFeatured: req.IsFeatured,
PrimaryMediaID: req.PrimaryMediaID,
Position: req.Position,
CategoryID: req.CategoryID,
Name: req.Name,
Description: req.Description,
IsActive: req.IsActive,
IsFeatured: req.IsFeatured,
PrimaryMediaID: req.PrimaryMediaID,
Position: req.Position,
}
}
@@ -156,11 +153,12 @@ func (h *Handler) Create(c *gin.Context) {
}
p, err := h.service.Create(c.Request.Context(), req.toInput())
if err != nil {
if errors.Is(err, ErrSlugTaken) {
c.JSON(http.StatusConflict, gin.H{"error": "slug already in use"})
return
switch {
case errors.Is(err, ErrCategoryNotFound):
c.JSON(http.StatusBadRequest, gin.H{"error": "category does not exist"})
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create product"})
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create product"})
return
}
c.JSON(http.StatusCreated, toResponse(p))
@@ -182,8 +180,8 @@ func (h *Handler) Update(c *gin.Context) {
switch {
case errors.Is(err, ErrNotFound):
c.JSON(http.StatusNotFound, gin.H{"error": "product not found"})
case errors.Is(err, ErrSlugTaken):
c.JSON(http.StatusConflict, gin.H{"error": "slug already in use"})
case errors.Is(err, ErrCategoryNotFound):
c.JSON(http.StatusBadRequest, gin.H{"error": "category does not exist"})
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update product"})
}
+10 -12
View File
@@ -11,18 +11,16 @@ import (
)
type Product struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey"`
CategoryID *uuid.UUID `gorm:"type:uuid"`
Name string `gorm:"not null"`
Slug string `gorm:"uniqueIndex;not null"`
ShortDescription string `gorm:"not null;default:''"`
Description string `gorm:"not null;default:''"`
IsActive bool `gorm:"not null;default:true"`
IsFeatured bool `gorm:"not null;default:false"`
PrimaryMediaID *uuid.UUID `gorm:"type:uuid"`
Position int `gorm:"not null;default:0"`
CreatedAt time.Time
UpdatedAt time.Time
ID uuid.UUID `gorm:"type:uuid;primaryKey"`
CategoryID uuid.UUID `gorm:"type:uuid;not null"`
Name string `gorm:"not null"`
Description string `gorm:"not null;default:''"`
IsActive bool `gorm:"not null;default:true"`
IsFeatured bool `gorm:"not null;default:false"`
PrimaryMediaID *uuid.UUID `gorm:"type:uuid"`
Position int `gorm:"not null;default:0"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (Product) TableName() string { return "products" }
@@ -5,19 +5,14 @@ import (
"errors"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgconn"
"gorm.io/gorm"
)
var (
ErrNotFound = errors.New("product not found")
ErrSlugTaken = errors.New("slug already in use")
)
var ErrNotFound = errors.New("product not found")
type Repository interface {
Create(ctx context.Context, p *Product) error
FindByID(ctx context.Context, id uuid.UUID) (*Product, error)
FindBySlug(ctx context.Context, slug string) (*Product, error)
List(ctx context.Context, activeOnly bool, categoryID *uuid.UUID) ([]*Product, error)
Update(ctx context.Context, p *Product) error
Delete(ctx context.Context, id uuid.UUID) error
@@ -26,6 +21,9 @@ type Repository interface {
RemoveGalleryItem(ctx context.Context, productID, mediaID uuid.UUID) error
ListGallery(ctx context.Context, productID uuid.UUID) ([]*GalleryItem, error)
}
type MediaOwnershipChecker interface {
BelongsToProduct(ctx context.Context, mediaID, productID uuid.UUID) (bool, error)
}
type gormRepository struct {
db *gorm.DB
@@ -36,13 +34,7 @@ func NewRepository(db *gorm.DB) Repository {
}
func (r *gormRepository) Create(ctx context.Context, p *Product) error {
if err := r.db.WithContext(ctx).Create(p).Error; err != nil {
if isUniqueViolation(err) {
return ErrSlugTaken
}
return err
}
return nil
return r.db.WithContext(ctx).Create(p).Error
}
func (r *gormRepository) FindByID(ctx context.Context, id uuid.UUID) (*Product, error) {
@@ -57,18 +49,6 @@ func (r *gormRepository) FindByID(ctx context.Context, id uuid.UUID) (*Product,
return &p, nil
}
func (r *gormRepository) FindBySlug(ctx context.Context, slug string) (*Product, error) {
var p Product
err := r.db.WithContext(ctx).Where("slug = ?", slug).First(&p).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrNotFound
}
if err != nil {
return nil, err
}
return &p, nil
}
func (r *gormRepository) List(ctx context.Context, activeOnly bool, categoryID *uuid.UUID) ([]*Product, error) {
q := r.db.WithContext(ctx).Order("position asc, created_at desc")
if activeOnly {
@@ -85,11 +65,7 @@ func (r *gormRepository) List(ctx context.Context, activeOnly bool, categoryID *
}
func (r *gormRepository) Update(ctx context.Context, p *Product) error {
err := r.db.WithContext(ctx).Save(p).Error
if isUniqueViolation(err) {
return ErrSlugTaken
}
return err
return r.db.WithContext(ctx).Save(p).Error
}
func (r *gormRepository) Delete(ctx context.Context, id uuid.UUID) error {
@@ -125,8 +101,3 @@ func (r *gormRepository) ListGallery(ctx context.Context, productID uuid.UUID) (
}
return list, nil
}
func isUniqueViolation(err error) bool {
var pgErr *pgconn.PgError
return errors.As(err, &pgErr) && pgErr.Code == "23505"
}
+5 -2
View File
@@ -17,8 +17,11 @@ func RegisterAdminRoutes(rg *gin.RouterGroup, h *Handler, requireAdmin gin.Handl
}
// RegisterPublicRoutes exposes the read-only, active-only product catalog
// used by the storefront (site vitrine / boutique).
// used by the storefront (site vitrine / boutique). The gallery lookup is
// kept as its own top-level path (like pricing's by-product route) to
// mirror the admin routes' shape.
func RegisterPublicRoutes(rg *gin.RouterGroup, h *Handler) {
rg.GET("/products", h.ListPublic)
rg.GET("/products/:slug", h.GetPublicBySlug)
rg.GET("/products/:id", h.GetPublic)
rg.GET("/product-gallery/:id", h.ListGallery)
}
+46 -28
View File
@@ -2,16 +2,38 @@ package products
import (
"context"
"errors"
"github.com/google/uuid"
"backend/internal/modules/categories"
)
// ErrCategoryNotFound is returned when a product references a category_id
// that doesn't exist -- every product must belong to a real category.
var ErrCategoryNotFound = errors.New("category not found")
type Service struct {
repo Repository
repo Repository
categoriesRepo categories.Repository
mediaOwnership MediaOwnershipChecker
}
func NewService(repo Repository) *Service {
return &Service{repo: repo}
func NewService(repo Repository, categoriesRepo categories.Repository) *Service {
return &Service{
repo: repo,
categoriesRepo: categoriesRepo,
}
}
func (s *Service) checkCategory(ctx context.Context, categoryID uuid.UUID) error {
if _, err := s.categoriesRepo.FindByID(ctx, categoryID); err != nil {
if errors.Is(err, categories.ErrNotFound) {
return ErrCategoryNotFound
}
return err
}
return nil
}
func (s *Service) List(ctx context.Context, activeOnly bool, categoryID *uuid.UUID) ([]*Product, error) {
@@ -22,34 +44,29 @@ func (s *Service) Get(ctx context.Context, id uuid.UUID) (*Product, error) {
return s.repo.FindByID(ctx, id)
}
func (s *Service) GetBySlug(ctx context.Context, slug string) (*Product, error) {
return s.repo.FindBySlug(ctx, slug)
}
type UpsertInput struct {
CategoryID *uuid.UUID
Name string
Slug string
ShortDescription string
Description string
IsActive bool
IsFeatured bool
PrimaryMediaID *uuid.UUID
Position int
CategoryID uuid.UUID
Name string
Description string
IsActive bool
IsFeatured bool
PrimaryMediaID *uuid.UUID
Position int
}
func (s *Service) Create(ctx context.Context, in UpsertInput) (*Product, error) {
if err := s.checkCategory(ctx, in.CategoryID); err != nil {
return nil, err
}
p := &Product{
ID: uuid.New(),
CategoryID: in.CategoryID,
Name: in.Name,
Slug: in.Slug,
ShortDescription: in.ShortDescription,
Description: in.Description,
IsActive: in.IsActive,
IsFeatured: in.IsFeatured,
PrimaryMediaID: in.PrimaryMediaID,
Position: in.Position,
ID: uuid.New(),
CategoryID: in.CategoryID,
Name: in.Name,
Description: in.Description,
IsActive: in.IsActive,
IsFeatured: in.IsFeatured,
PrimaryMediaID: in.PrimaryMediaID,
Position: in.Position,
}
if err := s.repo.Create(ctx, p); err != nil {
return nil, err
@@ -58,14 +75,15 @@ func (s *Service) Create(ctx context.Context, in UpsertInput) (*Product, error)
}
func (s *Service) Update(ctx context.Context, id uuid.UUID, in UpsertInput) (*Product, error) {
if err := s.checkCategory(ctx, in.CategoryID); err != nil {
return nil, err
}
p, err := s.repo.FindByID(ctx, id)
if err != nil {
return nil, err
}
p.CategoryID = in.CategoryID
p.Name = in.Name
p.Slug = in.Slug
p.ShortDescription = in.ShortDescription
p.Description = in.Description
p.IsActive = in.IsActive
p.IsFeatured = in.IsFeatured