git_action_config.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package gorm
  2. import (
  3. "github.com/porter-dev/porter/internal/models"
  4. "github.com/porter-dev/porter/internal/repository"
  5. "gorm.io/gorm"
  6. )
  7. // GitActionConfigRepository uses gorm.DB for querying the database
  8. type GitActionConfigRepository struct {
  9. db *gorm.DB
  10. }
  11. // NewGitActionConfigRepository returns a GitActionConfigRepository which uses
  12. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  13. // sensitive data
  14. func NewGitActionConfigRepository(db *gorm.DB) repository.GitActionConfigRepository {
  15. return &GitActionConfigRepository{db}
  16. }
  17. // CreateGitActionConfig creates a new git repo
  18. func (repo *GitActionConfigRepository) CreateGitActionConfig(ga *models.GitActionConfig) (*models.GitActionConfig, error) {
  19. release := &models.Release{}
  20. if err := repo.db.Where("id = ?", ga.ReleaseID).First(&release).Error; err != nil {
  21. return nil, err
  22. }
  23. assoc := repo.db.Model(&release).Association("GitActionConfig")
  24. if assoc.Error != nil {
  25. return nil, assoc.Error
  26. }
  27. if err := assoc.Append(ga); err != nil {
  28. return nil, err
  29. }
  30. return ga, nil
  31. }
  32. // ReadGitActionConfig gets a git repo specified by a unique id
  33. func (repo *GitActionConfigRepository) ReadGitActionConfig(id uint) (*models.GitActionConfig, error) {
  34. ga := &models.GitActionConfig{}
  35. if err := repo.db.Where("id = ?", id).First(&ga).Error; err != nil {
  36. return nil, err
  37. }
  38. return ga, nil
  39. }
  40. func (repo *GitActionConfigRepository) UpdateGitActionConfig(ga *models.GitActionConfig) error {
  41. return repo.db.Save(ga).Error
  42. }