mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
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)
123 lines
4.0 KiB
Go
123 lines
4.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"bugulma/backend/internal/domain"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ResourceFlowRepository implements domain.ResourceFlowRepository with GORM
|
|
type ResourceFlowRepository struct {
|
|
*BaseRepository[domain.ResourceFlow]
|
|
}
|
|
|
|
// NewResourceFlowRepository creates a new GORM-based resource flow repository
|
|
func NewResourceFlowRepository(db *gorm.DB) domain.ResourceFlowRepository {
|
|
return &ResourceFlowRepository{
|
|
BaseRepository: NewBaseRepository[domain.ResourceFlow](db),
|
|
}
|
|
}
|
|
|
|
// GetBySiteID retrieves resource flows at a specific site
|
|
func (r *ResourceFlowRepository) GetBySiteID(ctx context.Context, siteID string) ([]*domain.ResourceFlow, error) {
|
|
return r.FindWhereWithContext(ctx, "site_id = ?", siteID)
|
|
}
|
|
|
|
// GetByOrganizationID retrieves resource flows owned by a specific organization
|
|
func (r *ResourceFlowRepository) GetByOrganizationID(ctx context.Context, organizationID string) ([]*domain.ResourceFlow, error) {
|
|
return r.FindWhereWithContext(ctx, "organization_id = ?", organizationID)
|
|
}
|
|
|
|
// GetByTypeAndDirection retrieves resource flows by type and direction
|
|
func (r *ResourceFlowRepository) GetByTypeAndDirection(ctx context.Context, resType domain.ResourceType, direction domain.ResourceDirection) ([]*domain.ResourceFlow, error) {
|
|
return r.FindWhereWithContext(ctx, "type = ? AND direction = ?", resType, direction)
|
|
}
|
|
|
|
// GetOutputsInRadius retrieves output resource flows within a geographic radius
|
|
func (r *ResourceFlowRepository) GetOutputsInRadius(ctx context.Context, lat, lng, radiusKm float64, resType domain.ResourceType) ([]*domain.ResourceFlow, error) {
|
|
var flows []*domain.ResourceFlow
|
|
dialector := r.DB().Dialector.Name()
|
|
|
|
if dialector == "postgres" {
|
|
query := `
|
|
SELECT rf.* FROM resource_flows rf
|
|
JOIN sites s ON rf.site_id = s.id
|
|
WHERE rf.direction = 'output'
|
|
AND rf.type = ?
|
|
AND s.latitude IS NOT NULL AND s.longitude IS NOT NULL
|
|
AND (
|
|
6371 * acos(
|
|
cos(radians(?)) * cos(radians(s.latitude)) *
|
|
cos(radians(s.longitude) - radians(?)) +
|
|
sin(radians(?)) * sin(radians(s.latitude))
|
|
)
|
|
) <= ?
|
|
`
|
|
result := r.DB().WithContext(ctx).Raw(query, resType, lat, lng, lat, radiusKm).Scan(&flows)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
} else {
|
|
// Fallback to simple bounding box for SQLite
|
|
query := `
|
|
SELECT rf.* FROM resource_flows rf
|
|
JOIN sites s ON rf.site_id = s.id
|
|
WHERE rf.direction = 'output'
|
|
AND rf.type = ?
|
|
AND s.latitude IS NOT NULL AND s.longitude IS NOT NULL
|
|
AND abs(s.latitude - ?) <= 0.09
|
|
AND abs(s.longitude - ?) <= 0.15
|
|
`
|
|
result := r.DB().WithContext(ctx).Raw(query, resType, lat, lng).Scan(&flows)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
}
|
|
return flows, nil
|
|
}
|
|
|
|
// GetInputsInRadius retrieves input resource flows within a geographic radius
|
|
func (r *ResourceFlowRepository) GetInputsInRadius(ctx context.Context, lat, lng, radiusKm float64, resType domain.ResourceType) ([]*domain.ResourceFlow, error) {
|
|
var flows []*domain.ResourceFlow
|
|
dialector := r.DB().Dialector.Name()
|
|
|
|
if dialector == "postgres" {
|
|
query := `
|
|
SELECT rf.* FROM resource_flows rf
|
|
JOIN sites s ON rf.site_id = s.id
|
|
WHERE rf.direction = 'input'
|
|
AND rf.type = ?
|
|
AND s.latitude IS NOT NULL AND s.longitude IS NOT NULL
|
|
AND (
|
|
6371 * acos(
|
|
cos(radians(?)) * cos(radians(s.latitude)) *
|
|
cos(radians(s.longitude) - radians(?)) +
|
|
sin(radians(?)) * sin(radians(s.latitude))
|
|
)
|
|
) <= ?
|
|
`
|
|
result := r.DB().WithContext(ctx).Raw(query, resType, lat, lng, lat, radiusKm).Scan(&flows)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
} else {
|
|
// Fallback to simple bounding box for SQLite
|
|
query := `
|
|
SELECT rf.* FROM resource_flows rf
|
|
JOIN sites s ON rf.site_id = s.id
|
|
WHERE rf.direction = 'input'
|
|
AND rf.type = ?
|
|
AND s.latitude IS NOT NULL AND s.longitude IS NOT NULL
|
|
AND abs(s.latitude - ?) <= 0.09
|
|
AND abs(s.longitude - ?) <= 0.15
|
|
`
|
|
result := r.DB().WithContext(ctx).Raw(query, resType, lat, lng).Scan(&flows)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
}
|
|
return flows, nil
|
|
}
|