Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a workspace struct for WorkspaceCodeGraph with DB functions #2150

Closed
5 tasks
Tracked by #2149
humansinstitute opened this issue Dec 11, 2024 · 4 comments · Fixed by #2155
Closed
5 tasks
Tracked by #2149

Create a workspace struct for WorkspaceCodeGraph with DB functions #2150

humansinstitute opened this issue Dec 11, 2024 · 4 comments · Fixed by #2155
Assignees

Comments

@humansinstitute
Copy link
Contributor

humansinstitute commented Dec 11, 2024

Description

Implement database functions for managing WorkspaceCodeGraph entities, following the existing patterns used for WorkspaceRepositories. The WorkspaceCodeGraph struct represents a code graph associated with a workspace, storing metadata about the graph's name and URL.

Requirements

  1. Add three new database functions in the db package:

    • GetCodeGraphByUUID
    • UpdateCodeGraphByUUID
    • DeleteCodeGraphByUUID
  2. Functions should handle:

    • Error cases for not found items
    • Timestamps for updates
    • Return appropriate errors for invalid operations

Implementation Details

1. Database Interface

Add the following methods to the Database interface:

type Database interface {
    // ... existing methods ...
    GetCodeGraphByUUID(uuid string) (WorkspaceCodeGraph, error)
    UpdateCodeGraphByUUID(codeGraph WorkspaceCodeGraph) (WorkspaceCodeGraph, error)
    DeleteCodeGraphByUUID(uuid string) error
}

2. Example Implementation Code

// GetCodeGraphByUUID retrieves a code graph by its UUID
func (db database) GetCodeGraphByUUID(uuid string) (WorkspaceCodeGraph, error) {
    var codeGraph WorkspaceCodeGraph
    result := db.db.Where("uuid = ?", uuid).First(&codeGraph)
    
    if result.Error != nil {
        if errors.Is(result.Error, gorm.ErrRecordNotFound) {
            return WorkspaceCodeGraph{}, fmt.Errorf("code graph not found")
        }
        return WorkspaceCodeGraph{}, fmt.Errorf("database error: %w", result.Error)
    }
    
    return codeGraph, nil
}

// UpdateCodeGraphByUUID updates an existing code graph
func (db database) UpdateCodeGraphByUUID(codeGraph WorkspaceCodeGraph) (WorkspaceCodeGraph, error) {
    if codeGraph.Uuid == "" {
        return WorkspaceCodeGraph{}, fmt.Errorf("uuid is required")
    }
    
    var existing WorkspaceCodeGraph
    result := db.db.Where("uuid = ?", codeGraph.Uuid).First(&existing)
    if result.Error != nil {
        if errors.Is(result.Error, gorm.ErrRecordNotFound) {
            return WorkspaceCodeGraph{}, fmt.Errorf("code graph not found")
        }
        return WorkspaceCodeGraph{}, fmt.Errorf("database error: %w", result.Error)
    }
    
    now := time.Now()
    codeGraph.Updated = &now
    codeGraph.Created = existing.Created // Preserve original creation time
    
    if err := db.db.Model(&existing).Updates(codeGraph).Error; err != nil {
        return WorkspaceCodeGraph{}, fmt.Errorf("failed to update code graph: %w", err)
    }
    
    // Fetch the updated record
    var updated WorkspaceCodeGraph
    if err := db.db.Where("uuid = ?", codeGraph.Uuid).First(&updated).Error; err != nil {
        return WorkspaceCodeGraph{}, fmt.Errorf("failed to fetch updated code graph: %w", err)
    }
    
    return updated, nil
}

// DeleteCodeGraphByUUID deletes a code graph by its UUID
func (db database) DeleteCodeGraphByUUID(uuid string) error {
    if uuid == "" {
        return fmt.Errorf("uuid is required")
    }
    
    result := db.db.Where("uuid = ?", uuid).Delete(&WorkspaceCodeGraph{})
    if result.Error != nil {
        return fmt.Errorf("failed to delete code graph: %w", result.Error)
    }
    
    if result.RowsAffected == 0 {
        return fmt.Errorf("code graph not found")
    }
    
    return nil
}

3. Tests

Add unit tests for each new function:

func TestGetCodeGraphByUUID(t *testing.T) {
    // Setup test database
    db := setupTestDB(t)
    
    // Test cases:
    // 1. Successfully retrieve existing code graph
    // 2. Return error for non-existent UUID
    // 3. Handle invalid UUID format
}

func TestUpdateCodeGraphByUUID(t *testing.T) {
    // Setup test database
    db := setupTestDB(t)
    
    // Test cases:
    // 1. Successfully update existing code graph
    // 2. Return error for non-existent UUID
    // 3. Verify timestamp updates
    // 4. Verify field updates
}

func TestDeleteCodeGraphByUUID(t *testing.T) {
    // Setup test database
    db := setupTestDB(t)
    
    // Test cases:
    // 1. Successfully delete existing code graph
    // 2. Return error for non-existent UUID
    // 3. Verify record is actually deleted
}

Task

  1. Implement all database functions
  2. Write comprehensive unit tests
  3. Test integration with existing workspace functionality
  4. Verify error handling and edge cases
  5. Check proper timestamp handling
  6. Verify validation

Acceptance Criteria

  • Struct with CRUD functions is available for endpoint integration
  • All three database functions are implemented
  • Functions follow existing codebase patterns
  • Proper error handling is implemented
  • Unit tests are written and passing
@saithsab877
Copy link
Contributor

saithsab877 commented Dec 11, 2024

@humansinstitute Please assign me

@MuhammadUmer44
Copy link
Contributor

MuhammadUmer44 commented Dec 11, 2024

@humansinstitute assign me?

@Shoaibdev7
Copy link
Contributor

@humansinstitute I can help?

@aliraza556
Copy link
Contributor

@humansinstitute Please assign me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants