preview.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package dbstore
  2. import (
  3. "context"
  4. "fmt"
  5. "git.linuxforward.com/byop/byop-engine/models"
  6. "gorm.io/gorm"
  7. )
  8. // CreatePreview creates a new preview using GORM
  9. func (s *SQLiteStore) CreatePreview(ctx context.Context, preview *models.Preview) error {
  10. result := s.db.WithContext(ctx).Create(preview)
  11. if result.Error != nil {
  12. return fmt.Errorf("failed to create preview: %w", result.Error)
  13. }
  14. return nil
  15. }
  16. // GetPreviewByID retrieves a preview by ID using GORM
  17. func (s *SQLiteStore) GetPreviewByID(ctx context.Context, id uint) (*models.Preview, error) {
  18. var preview models.Preview
  19. result := s.db.WithContext(ctx).First(&preview, id)
  20. if result.Error != nil {
  21. if result.Error == gorm.ErrRecordNotFound {
  22. return nil, models.NewErrNotFound(fmt.Sprintf("preview with ID %d not found", id), result.Error)
  23. }
  24. return nil, fmt.Errorf("failed to get preview by ID: %w", result.Error)
  25. }
  26. return &preview, nil
  27. }
  28. // GetPreviewByAppID retrieves a preview by app ID using GORM
  29. func (s *SQLiteStore) GetPreviewByAppID(ctx context.Context, appID uint) (*models.Preview, error) {
  30. var preview models.Preview
  31. result := s.db.WithContext(ctx).Where("app_id = ?", appID).First(&preview)
  32. if result.Error != nil {
  33. if result.Error == gorm.ErrRecordNotFound {
  34. return nil, models.NewErrNotFound(fmt.Sprintf("preview for app ID %d not found", appID), result.Error)
  35. }
  36. return nil, fmt.Errorf("failed to get preview by app ID: %w", result.Error)
  37. }
  38. return &preview, nil
  39. }
  40. // GetPreviewsByAppID retrieves all previews for an app using GORM
  41. func (s *SQLiteStore) GetPreviewsByAppID(ctx context.Context, appID uint) ([]*models.Preview, error) {
  42. var previews []*models.Preview
  43. result := s.db.WithContext(ctx).Where("app_id = ?", appID).Find(&previews)
  44. if result.Error != nil {
  45. return nil, fmt.Errorf("failed to get previews by app ID: %w", result.Error)
  46. }
  47. return previews, nil
  48. }
  49. // GetAllPreviews retrieves all previews using GORM
  50. func (s *SQLiteStore) GetAllPreviews(ctx context.Context) ([]*models.Preview, error) {
  51. var previews []*models.Preview
  52. result := s.db.WithContext(ctx).Find(&previews)
  53. if result.Error != nil {
  54. return nil, fmt.Errorf("failed to get all previews: %w", result.Error)
  55. }
  56. return previews, nil
  57. }
  58. // GetPreviewsByStatus retrieves previews by status using GORM
  59. func (s *SQLiteStore) GetPreviewsByStatus(ctx context.Context, status string) ([]*models.Preview, error) {
  60. var previews []*models.Preview
  61. result := s.db.WithContext(ctx).Where("status = ?", status).Find(&previews)
  62. if result.Error != nil {
  63. return nil, fmt.Errorf("failed to get previews by status: %w", result.Error)
  64. }
  65. return previews, nil
  66. }
  67. // UpdatePreview updates an existing preview using GORM
  68. func (s *SQLiteStore) UpdatePreview(ctx context.Context, preview *models.Preview) error {
  69. result := s.db.WithContext(ctx).Save(preview)
  70. if result.Error != nil {
  71. return fmt.Errorf("failed to update preview: %w", result.Error)
  72. }
  73. if result.RowsAffected == 0 {
  74. return models.NewErrNotFound(fmt.Sprintf("preview with ID %d not found for update", preview.ID), nil)
  75. }
  76. return nil
  77. }
  78. // DeletePreview deletes a preview by ID using GORM
  79. func (s *SQLiteStore) DeletePreview(ctx context.Context, id uint) error {
  80. result := s.db.WithContext(ctx).Delete(&models.Preview{}, id)
  81. if result.Error != nil {
  82. return fmt.Errorf("failed to delete preview: %w", result.Error)
  83. }
  84. if result.RowsAffected == 0 {
  85. return models.NewErrNotFound(fmt.Sprintf("preview with ID %d not found for deletion", id), nil)
  86. }
  87. return nil
  88. }
  89. // UpdatePreviewVPS updates VPS information for a preview using GORM
  90. func (s *SQLiteStore) UpdatePreviewVPS(ctx context.Context, previewID uint, vpsID string, ipAddress string, previewURL string) error {
  91. result := s.db.WithContext(ctx).Model(&models.Preview{}).Where("id = ?", previewID).Updates(map[string]interface{}{
  92. "vps_id": vpsID,
  93. "ip_address": ipAddress,
  94. "url": previewURL,
  95. })
  96. if result.Error != nil {
  97. return fmt.Errorf("failed to update preview VPS: %w", result.Error)
  98. }
  99. if result.RowsAffected == 0 {
  100. return models.NewErrNotFound(fmt.Sprintf("preview with ID %d not found for VPS update", previewID), nil)
  101. }
  102. return nil
  103. }
  104. // UpdatePreviewStatus updates the status and error message of a preview using GORM
  105. func (s *SQLiteStore) UpdatePreviewStatus(ctx context.Context, previewID uint, status string, errorMsg string) error {
  106. result := s.db.WithContext(ctx).Model(&models.Preview{}).Where("id = ?", previewID).Updates(map[string]interface{}{
  107. "status": status,
  108. "error_msg": errorMsg,
  109. })
  110. if result.Error != nil {
  111. return fmt.Errorf("failed to update preview status: %w", result.Error)
  112. }
  113. if result.RowsAffected == 0 {
  114. return models.NewErrNotFound(fmt.Sprintf("preview with ID %d not found for status update", previewID), nil)
  115. }
  116. return nil
  117. }
  118. // UpdatePreviewBuildLogs updates the build logs of a preview using GORM
  119. func (s *SQLiteStore) UpdatePreviewBuildLogs(ctx context.Context, previewID uint, logs string) error {
  120. result := s.db.WithContext(ctx).Model(&models.Preview{}).Where("id = ?", previewID).Update("build_logs", logs)
  121. if result.Error != nil {
  122. return fmt.Errorf("failed to update preview build logs: %w", result.Error)
  123. }
  124. if result.RowsAffected == 0 {
  125. return models.NewErrNotFound(fmt.Sprintf("preview with ID %d not found for build logs update", previewID), nil)
  126. }
  127. return nil
  128. }
  129. // UpdatePreviewDeployLogs updates the deploy logs of a preview using GORM
  130. func (s *SQLiteStore) UpdatePreviewDeployLogs(ctx context.Context, previewID uint, logs string) error {
  131. result := s.db.WithContext(ctx).Model(&models.Preview{}).Where("id = ?", previewID).Update("deploy_logs", logs)
  132. if result.Error != nil {
  133. return fmt.Errorf("failed to update preview deploy logs: %w", result.Error)
  134. }
  135. if result.RowsAffected == 0 {
  136. return models.NewErrNotFound(fmt.Sprintf("preview with ID %d not found for deploy logs update", previewID), nil)
  137. }
  138. return nil
  139. }