turash/bugulma/backend/internal/domain/product.go
Damir Mukimov 000eab4740
Major repository reorganization and missing backend endpoints implementation
Repository Structure:
- Move files from cluttered root directory into organized structure
- Create archive/ for archived data and scraper results
- Create bugulma/ for the complete application (frontend + backend)
- Create data/ for sample datasets and reference materials
- Create docs/ for comprehensive documentation structure
- Create scripts/ for utility scripts and API tools

Backend Implementation:
- Implement 3 missing backend endpoints identified in gap analysis:
  * GET /api/v1/organizations/{id}/matching/direct - Direct symbiosis matches
  * GET /api/v1/users/me/organizations - User organizations
  * POST /api/v1/proposals/{id}/status - Update proposal status
- Add complete proposal domain model, repository, and service layers
- Create database migration for proposals table
- Fix CLI server command registration issue

API Documentation:
- Add comprehensive proposals.md API documentation
- Update README.md with Users and Proposals API sections
- Document all request/response formats, error codes, and business rules

Code Quality:
- Follow existing Go backend architecture patterns
- Add proper error handling and validation
- Match frontend expected response schemas
- Maintain clean separation of concerns (handler -> service -> repository)
2025-11-25 06:01:16 +01:00

83 lines
3.1 KiB
Go

package domain
import (
"context"
"fmt"
"time"
"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"`
// 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
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)
}
// 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
}