turash/bugulma/backend/internal/graph/session_manager.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

80 lines
2.3 KiB
Go

package graph
import (
"context"
"fmt"
"time"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
// SessionManager manages Neo4j sessions with proper lifecycle management
type SessionManager struct {
driver neo4j.DriverWithContext
database string
config *Config
}
// NewSessionManager creates a new session manager
func NewSessionManager(driver neo4j.DriverWithContext, database string, config *Config) *SessionManager {
return &SessionManager{
driver: driver,
database: database,
config: config,
}
}
// ExecuteRead executes a read transaction with proper error handling
func (sm *SessionManager) ExecuteRead(ctx context.Context, fn func(neo4j.ManagedTransaction) (interface{}, error)) (interface{}, error) {
// Create context with timeout
queryCtx, cancel := context.WithTimeout(ctx, time.Duration(sm.config.QueryTimeoutSeconds)*time.Second)
defer cancel()
session := sm.driver.NewSession(queryCtx, neo4j.SessionConfig{
AccessMode: neo4j.AccessModeRead,
DatabaseName: sm.database,
})
defer session.Close(queryCtx)
result, err := session.ExecuteRead(queryCtx, func(tx neo4j.ManagedTransaction) (interface{}, error) {
return fn(tx)
})
if err != nil {
return nil, fmt.Errorf("read transaction failed: %w", err)
}
return result, nil
}
// ExecuteWrite executes a write transaction with proper error handling
func (sm *SessionManager) ExecuteWrite(ctx context.Context, fn func(neo4j.ManagedTransaction) (interface{}, error)) (interface{}, error) {
// Create context with timeout
queryCtx, cancel := context.WithTimeout(ctx, time.Duration(sm.config.QueryTimeoutSeconds)*time.Second)
defer cancel()
session := sm.driver.NewSession(queryCtx, neo4j.SessionConfig{
AccessMode: neo4j.AccessModeWrite,
DatabaseName: sm.database,
})
defer session.Close(queryCtx)
result, err := session.ExecuteWrite(queryCtx, func(tx neo4j.ManagedTransaction) (interface{}, error) {
return fn(tx)
})
if err != nil {
return nil, fmt.Errorf("write transaction failed: %w", err)
}
return result, nil
}
// GetSession creates a new session (for advanced use cases)
func (sm *SessionManager) GetSession(ctx context.Context, accessMode neo4j.AccessMode) neo4j.SessionWithContext {
return sm.driver.NewSession(ctx, neo4j.SessionConfig{
AccessMode: accessMode,
DatabaseName: sm.database,
})
}