turash/bugulma/backend/internal/service/shared_asset_service.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

127 lines
3.7 KiB
Go

package service
import (
"bugulma/backend/internal/domain"
"context"
"encoding/json"
"time"
"github.com/google/uuid"
"gorm.io/datatypes"
)
// SharedAssetService handles business logic for shared assets
type SharedAssetService struct {
repo domain.SharedAssetRepository
}
// NewSharedAssetService creates a new shared asset service
func NewSharedAssetService(repo domain.SharedAssetRepository) *SharedAssetService {
return &SharedAssetService{repo: repo}
}
// CreateSharedAssetRequest represents the request to create a shared asset
type CreateSharedAssetRequest struct {
OwnerBusinessID string
SiteID string
Type domain.AssetType
Description string
Capacity float64
CapacityUnit string
UtilizationRate float64
AvailabilityPeriod string
OperationalStatus domain.OperationalStatus
CostSharingModel string
MaintenanceSchedule string
}
// Create creates a new shared asset
func (s *SharedAssetService) Create(ctx context.Context, req CreateSharedAssetRequest) (*domain.SharedAsset, error) {
currentUsers := []string{}
currentUsersJSON, _ := json.Marshal(currentUsers)
asset := &domain.SharedAsset{
ID: uuid.New().String(),
OwnerBusinessID: req.OwnerBusinessID,
SiteID: req.SiteID,
Type: req.Type,
Description: req.Description,
Capacity: req.Capacity,
CapacityUnit: req.CapacityUnit,
UtilizationRate: req.UtilizationRate,
AvailabilityPeriod: req.AvailabilityPeriod,
CostSharingModel: req.CostSharingModel,
CurrentUsers: datatypes.JSON(currentUsersJSON),
MaintenanceSchedule: req.MaintenanceSchedule,
OperationalStatus: req.OperationalStatus,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if err := s.repo.Create(ctx, asset); err != nil {
return nil, err
}
return asset, nil
}
// GetByID retrieves a shared asset by ID
func (s *SharedAssetService) GetByID(ctx context.Context, id string) (*domain.SharedAsset, error) {
return s.repo.GetByID(ctx, id)
}
// GetBySiteID retrieves shared assets at a specific site
func (s *SharedAssetService) GetBySiteID(ctx context.Context, siteID string) ([]*domain.SharedAsset, error) {
return s.repo.GetBySiteID(ctx, siteID)
}
// GetByOwnerBusinessID retrieves shared assets owned by a business
func (s *SharedAssetService) GetByOwnerBusinessID(ctx context.Context, businessID string) ([]*domain.SharedAsset, error) {
return s.repo.GetByOwnerID(ctx, businessID)
}
// GetAvailableAssets retrieves assets with available capacity
func (s *SharedAssetService) GetAvailableAssets(ctx context.Context) ([]*domain.SharedAsset, error) {
return s.repo.GetAvailable(ctx)
}
// Update updates a shared asset
func (s *SharedAssetService) Update(ctx context.Context, asset *domain.SharedAsset) error {
asset.UpdatedAt = time.Now()
return s.repo.Update(ctx, asset)
}
// Delete deletes a shared asset
func (s *SharedAssetService) Delete(ctx context.Context, id string) error {
return s.repo.Delete(ctx, id)
}
// Helper functions to convert strings to domain types
func AssetTypeFromString(s string) domain.AssetType {
switch s {
case "infrastructure":
return domain.AssetTypeInfrastructure
case "utilities":
return domain.AssetTypeUtilities
case "equipment":
return domain.AssetTypeEquipment
case "space":
return domain.AssetTypeSpace
default:
return domain.AssetTypeEquipment
}
}
func OperationalStatusFromString(s string) domain.OperationalStatus {
switch s {
case "operational":
return domain.StatusOperational
case "maintenance":
return domain.StatusMaintenance
case "out_of_service":
return domain.StatusOutOfService
default:
return domain.StatusOperational
}
}