deployments.go 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package dbstore
  2. import (
  3. "context"
  4. "fmt"
  5. "git.linuxforward.com/byop/byop-engine/models"
  6. "gorm.io/gorm"
  7. )
  8. // CreateDeployment creates a new deployment using GORM
  9. func (s *SQLiteStore) CreateDeployment(ctx context.Context, deployment *models.Deployment) error {
  10. result := s.db.WithContext(ctx).Create(deployment)
  11. if result.Error != nil {
  12. return fmt.Errorf("failed to create deployment: %w", result.Error)
  13. }
  14. return nil
  15. }
  16. // GetDeploymentByID retrieves a deployment by ID using GORM
  17. func (s *SQLiteStore) GetDeploymentByID(ctx context.Context, id uint) (*models.Deployment, error) {
  18. var deployment models.Deployment
  19. result := s.db.WithContext(ctx).First(&deployment, id)
  20. if result.Error != nil {
  21. if result.Error == gorm.ErrRecordNotFound {
  22. return nil, models.NewErrNotFound(fmt.Sprintf("deployment with ID %d not found", id), result.Error)
  23. }
  24. return nil, fmt.Errorf("failed to get deployment by ID: %w", result.Error)
  25. }
  26. return &deployment, nil
  27. }
  28. // GetDeploymentsByClientID retrieves all deployments for a specific client using GORM
  29. func (s *SQLiteStore) GetDeploymentsByClientID(ctx context.Context, clientID uint) ([]*models.Deployment, error) {
  30. var deployments []*models.Deployment
  31. result := s.db.WithContext(ctx).Where("client_id = ?", clientID).Find(&deployments)
  32. if result.Error != nil {
  33. return nil, fmt.Errorf("failed to get deployments by client ID: %w", result.Error)
  34. }
  35. return deployments, nil
  36. }
  37. // GetDeploymentsByAppID retrieves all deployments for a specific app using GORM
  38. func (s *SQLiteStore) GetDeploymentsByAppID(ctx context.Context, appID uint) ([]*models.Deployment, error) {
  39. var deployments []*models.Deployment
  40. result := s.db.WithContext(ctx).Where("app_id = ?", appID).Find(&deployments)
  41. if result.Error != nil {
  42. return nil, fmt.Errorf("failed to get deployments by app ID: %w", result.Error)
  43. }
  44. return deployments, nil
  45. }
  46. // GetAllDeployments retrieves all deployments using GORM
  47. func (s *SQLiteStore) GetAllDeployments(ctx context.Context) ([]*models.Deployment, error) {
  48. var deployments []*models.Deployment
  49. result := s.db.WithContext(ctx).Find(&deployments)
  50. if result.Error != nil {
  51. return nil, fmt.Errorf("failed to get all deployments: %w", result.Error)
  52. }
  53. return deployments, nil
  54. }
  55. // UpdateDeployment updates an existing deployment using GORM
  56. func (s *SQLiteStore) UpdateDeployment(ctx context.Context, deployment *models.Deployment) error {
  57. result := s.db.WithContext(ctx).Save(deployment)
  58. if result.Error != nil {
  59. return fmt.Errorf("failed to update deployment: %w", result.Error)
  60. }
  61. if result.RowsAffected == 0 {
  62. return models.NewErrNotFound(fmt.Sprintf("deployment with ID %d not found for update", deployment.ID), nil)
  63. }
  64. return nil
  65. }
  66. // DeleteDeployment deletes a deployment by ID using GORM
  67. func (s *SQLiteStore) DeleteDeployment(ctx context.Context, id uint) error {
  68. result := s.db.WithContext(ctx).Delete(&models.Deployment{}, id)
  69. if result.Error != nil {
  70. return fmt.Errorf("failed to delete deployment: %w", result.Error)
  71. }
  72. if result.RowsAffected == 0 {
  73. return models.NewErrNotFound(fmt.Sprintf("deployment with ID %d not found for deletion", id), nil)
  74. }
  75. return nil
  76. }
  77. // GetDeploymentsByUserID retrieves all deployments for a specific user via their apps using GORM
  78. func (s *SQLiteStore) GetDeploymentsByUserID(ctx context.Context, userID uint) ([]*models.Deployment, error) {
  79. var deployments []*models.Deployment
  80. result := s.db.WithContext(ctx).
  81. Joins("JOIN apps ON deployments.app_id = apps.id").
  82. Where("apps.user_id = ?", userID).
  83. Find(&deployments)
  84. if result.Error != nil {
  85. return nil, fmt.Errorf("failed to get deployments by user ID: %w", result.Error)
  86. }
  87. return deployments, nil
  88. }