apps.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dbstore
  2. import (
  3. "context"
  4. "fmt"
  5. "git.linuxforward.com/byop/byop-engine/models"
  6. "gorm.io/gorm"
  7. )
  8. // CreateApp creates a new app using GORM
  9. func (s *SQLiteStore) CreateApp(ctx context.Context, app *models.App) error {
  10. result := s.db.WithContext(ctx).Create(app)
  11. if result.Error != nil {
  12. return fmt.Errorf("failed to create app: %w", result.Error)
  13. }
  14. return nil
  15. }
  16. // GetAllApps retrieves all apps using GORM
  17. func (s *SQLiteStore) GetAllApps(ctx context.Context) ([]*models.App, error) {
  18. var apps []*models.App
  19. result := s.db.WithContext(ctx).Find(&apps)
  20. if result.Error != nil {
  21. return nil, fmt.Errorf("failed to get all apps: %w", result.Error)
  22. }
  23. return apps, nil
  24. }
  25. // GetAppByID retrieves an app by ID using GORM
  26. func (s *SQLiteStore) GetAppByID(ctx context.Context, id uint) (*models.App, error) {
  27. var app models.App
  28. result := s.db.WithContext(ctx).First(&app, id)
  29. if result.Error != nil {
  30. if result.Error == gorm.ErrRecordNotFound {
  31. return nil, models.NewErrNotFound(fmt.Sprintf("app with ID %d not found", id), result.Error)
  32. }
  33. return nil, fmt.Errorf("failed to get app by ID: %w", result.Error)
  34. }
  35. return &app, nil
  36. }
  37. // GetAppsByUserID retrieves all apps for a specific user using GORM
  38. func (s *SQLiteStore) GetAppsByUserID(ctx context.Context, userID uint) ([]*models.App, error) {
  39. var apps []*models.App
  40. result := s.db.WithContext(ctx).Where("user_id = ?", userID).Find(&apps)
  41. if result.Error != nil {
  42. return nil, fmt.Errorf("failed to get apps by user ID: %w", result.Error)
  43. }
  44. return apps, nil
  45. }
  46. // UpdateApp updates an existing app using GORM
  47. func (s *SQLiteStore) UpdateApp(ctx context.Context, app *models.App) error {
  48. result := s.db.WithContext(ctx).Save(app)
  49. if result.Error != nil {
  50. return fmt.Errorf("failed to update app: %w", result.Error)
  51. }
  52. if result.RowsAffected == 0 {
  53. return models.NewErrNotFound(fmt.Sprintf("app with ID %d not found for update", app.ID), nil)
  54. }
  55. return nil
  56. }
  57. // DeleteApp deletes an app by ID using GORM
  58. func (s *SQLiteStore) DeleteApp(ctx context.Context, id uint) error {
  59. result := s.db.WithContext(ctx).Delete(&models.App{}, id)
  60. if result.Error != nil {
  61. return fmt.Errorf("failed to delete app: %w", result.Error)
  62. }
  63. if result.RowsAffected == 0 {
  64. return models.NewErrNotFound(fmt.Sprintf("app with ID %d not found for deletion", id), nil)
  65. }
  66. return nil
  67. }
  68. // UpdateAppStatus updates the status and error message of an app using GORM
  69. func (s *SQLiteStore) UpdateAppStatus(ctx context.Context, appID uint, status string, message string) error {
  70. result := s.db.WithContext(ctx).Model(&models.App{}).Where("id = ?", appID).Updates(map[string]interface{}{
  71. "status": status,
  72. "error_msg": message,
  73. })
  74. if result.Error != nil {
  75. return fmt.Errorf("failed to update app status: %w", result.Error)
  76. }
  77. if result.RowsAffected == 0 {
  78. return models.NewErrNotFound(fmt.Sprintf("app with ID %d not found for status update", appID), nil)
  79. }
  80. return nil
  81. }
  82. // UpdateAppPreview updates the preview information of an app using GORM
  83. func (s *SQLiteStore) UpdateAppPreview(ctx context.Context, appID uint, previewID uint, previewURL string) error {
  84. result := s.db.WithContext(ctx).Model(&models.App{}).Where("id = ?", appID).Updates(map[string]interface{}{
  85. "preview_id": previewID,
  86. "preview_url": previewURL,
  87. })
  88. if result.Error != nil {
  89. return fmt.Errorf("failed to update app preview: %w", result.Error)
  90. }
  91. if result.RowsAffected == 0 {
  92. return models.NewErrNotFound(fmt.Sprintf("app with ID %d not found for preview update", appID), nil)
  93. }
  94. return nil
  95. }
  96. // UpdateAppCurrentImage updates the current image information of an app using GORM
  97. func (s *SQLiteStore) UpdateAppCurrentImage(ctx context.Context, appID uint, imageTag string, imageURI string) error {
  98. result := s.db.WithContext(ctx).Model(&models.App{}).Where("id = ?", appID).Updates(map[string]interface{}{
  99. "current_image_tag": imageTag,
  100. "current_image_uri": imageURI,
  101. })
  102. if result.Error != nil {
  103. return fmt.Errorf("failed to update app current image: %w", result.Error)
  104. }
  105. if result.RowsAffected == 0 {
  106. return models.NewErrNotFound(fmt.Sprintf("app with ID %d not found for image update", appID), nil)
  107. }
  108. return nil
  109. }