turash/bugulma/backend/internal/domain/product.go

101 lines
4.1 KiB
Go

package domain
import (
"context"
"fmt"
"time"
"github.com/lib/pq"
"gorm.io/datatypes"
)
// ProductCategory defines the category of a product
type ProductCategory string
const (
ProductCategoryChemicals ProductCategory = "chemicals"
ProductCategoryEquipment ProductCategory = "equipment"
ProductCategoryMaterials ProductCategory = "materials"
ProductCategoryFood ProductCategory = "food"
ProductCategoryPackaging ProductCategory = "packaging"
ProductCategoryOilGas ProductCategory = "oil_gas"
ProductCategoryConstruction ProductCategory = "construction"
ProductCategoryManufacturing ProductCategory = "manufacturing"
ProductCategoryOther ProductCategory = "other"
)
// Product represents a product offered by an organization (database entity)
// Note: There's also a ProductJSON struct in organization.go for API serialization
type Product struct {
ID string `gorm:"primaryKey;type:text"`
Name string `gorm:"not null;type:text;index"`
Category ProductCategory `gorm:"not null;type:varchar(50);index"`
Description string `gorm:"type:text"`
UnitPrice float64 `gorm:"type:decimal(10,2);index"` // € per unit
MOQ int `gorm:"type:integer"` // Minimum Order Quantity
Certifications datatypes.JSON `gorm:"default:'[]'"` // []string
// Business information
OrganizationID string `gorm:"not null;type:text;index"`
Organization *Organization `gorm:"foreignKey:OrganizationID"`
// Site information (for location-based matching)
SiteID *string `gorm:"type:text;index"`
Site *Site `gorm:"foreignKey:SiteID"`
// Location (PostGIS Point for spatial queries)
Location Point `gorm:"type:geometry(Point,4326)"` // PostGIS geometry point
// Discovery and search fields
SearchKeywords string `gorm:"type:text"` // Full-text search keywords
Tags pq.StringArray `gorm:"type:text[]"` // Searchable tags array
AvailabilityStatus string `gorm:"type:varchar(20);default:'available';index"` // available, limited, out_of_stock
Images pq.StringArray `gorm:"type:text[]"` // Array of image URLs
// Additional metadata
Capacity string `gorm:"type:text"` // Production capacity info
Specifications datatypes.JSON `gorm:"type:jsonb;default:'{}'"` // Technical specifications
Availability string `gorm:"type:text"` // Availability status (legacy field, kept for compatibility)
Sources datatypes.JSON `gorm:"type:jsonb"` // Data sources
// Timestamps
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}
// TableName specifies the table name for GORM
func (Product) TableName() string {
return "products"
}
// ProductRepository defines operations for product management
type ProductRepository interface {
Create(ctx context.Context, product *Product) error
GetByID(ctx context.Context, id string) (*Product, error)
GetByOrganization(ctx context.Context, organizationID string) ([]*Product, error)
GetByCategory(ctx context.Context, category ProductCategory) ([]*Product, error)
SearchByName(ctx context.Context, name string) ([]*Product, error)
GetByPriceRange(ctx context.Context, minPrice, maxPrice float64) ([]*Product, error)
Update(ctx context.Context, product *Product) error
Delete(ctx context.Context, id string) error
GetAll(ctx context.Context) ([]*Product, error)
// Discovery methods
SearchWithLocation(ctx context.Context, query string, location *Point, radiusKm float64) ([]*Product, error)
GetBySite(ctx context.Context, siteID string) ([]*Product, error)
GetNearby(ctx context.Context, lat, lng, radiusKm float64) ([]*Product, error)
}
// Validate performs business rule validation
func (p *Product) Validate() error {
if p.Name == "" {
return fmt.Errorf("product name cannot be empty")
}
if p.UnitPrice < 0 {
return fmt.Errorf("unit price cannot be negative")
}
if p.MOQ < 0 {
return fmt.Errorf("minimum order quantity cannot be negative")
}
return nil
}