component.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package dbstore
  2. import (
  3. "fmt"
  4. "git.linuxforward.com/byop/byop-engine/dbmanager"
  5. "git.linuxforward.com/byop/byop-engine/models"
  6. "github.com/google/uuid"
  7. "gorm.io/gorm"
  8. )
  9. // ComponentStore handles database operations for components
  10. type ComponentStore struct {
  11. db *gorm.DB
  12. }
  13. // NewComponentStore creates a new ComponentStore
  14. func NewComponentStore(dbManager dbmanager.DbManager) *ComponentStore {
  15. return &ComponentStore{
  16. db: dbManager.GetDB(),
  17. }
  18. }
  19. // Create creates a new component
  20. func (cs *ComponentStore) Create(component *models.Component) error {
  21. // Generate ID if not provided
  22. if component.ID == "" {
  23. component.ID = uuid.New().String()
  24. }
  25. // GORM will handle created_at and updated_at automatically
  26. return cs.db.Create(component).Error
  27. }
  28. // GetByID retrieves a component by ID
  29. func (cs *ComponentStore) GetByID(id string) (*models.Component, error) {
  30. var component models.Component
  31. result := cs.db.First(&component, "id = ?", id)
  32. if result.Error != nil {
  33. if result.Error == gorm.ErrRecordNotFound {
  34. return nil, nil // No component found
  35. }
  36. return nil, fmt.Errorf("failed to get component: %w", result.Error)
  37. }
  38. return &component, nil
  39. }
  40. // Update updates an existing component
  41. func (cs *ComponentStore) Update(component *models.Component) error {
  42. return cs.db.Save(component).Error
  43. }
  44. // Delete deletes a component by ID
  45. func (cs *ComponentStore) Delete(id string) error {
  46. return cs.db.Delete(&models.Component{}, "id = ?", id).Error
  47. }
  48. // List retrieves all components with optional filtering
  49. func (cs *ComponentStore) List(filter map[string]interface{}) ([]*models.Component, error) {
  50. var components []*models.Component
  51. // Build query from filters
  52. query := cs.db
  53. if filter != nil {
  54. for key, value := range filter {
  55. query = query.Where(key+" = ?", value)
  56. }
  57. }
  58. // Execute query
  59. if err := query.Find(&components).Error; err != nil {
  60. return nil, fmt.Errorf("failed to list components: %w", err)
  61. }
  62. return components, nil
  63. }
  64. // GetComponentWithDeployments retrieves a component by ID with associated deployments
  65. func (cs *ComponentStore) GetComponentWithDeployments(id string) (*models.Component, error) {
  66. var component models.Component
  67. result := cs.db.Preload("Deployments").First(&component, "id = ?", id)
  68. if result.Error != nil {
  69. if result.Error == gorm.ErrRecordNotFound {
  70. return nil, nil // No component found
  71. }
  72. return nil, fmt.Errorf("failed to get component: %w", result.Error)
  73. }
  74. return &component, nil
  75. }