template.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // TemplateStore handles database operations for deployment templates
  10. type TemplateStore struct {
  11. db *gorm.DB
  12. }
  13. // NewTemplateStore creates a new TemplateStore
  14. func NewTemplateStore(dbManager dbmanager.DbManager) *TemplateStore {
  15. return &TemplateStore{
  16. db: dbManager.GetDB(),
  17. }
  18. }
  19. // Create creates a new template
  20. func (ts *TemplateStore) Create(template *models.Template) error {
  21. // Generate ID if not provided
  22. if template.ID == "" {
  23. template.ID = uuid.New().String()
  24. }
  25. // GORM will handle created_at and updated_at automatically
  26. return ts.db.Create(template).Error
  27. }
  28. // GetByID retrieves a template by ID
  29. func (ts *TemplateStore) GetByID(id string) (*models.Template, error) {
  30. var template models.Template
  31. result := ts.db.First(&template, "id = ?", id)
  32. if result.Error != nil {
  33. if result.Error == gorm.ErrRecordNotFound {
  34. return nil, nil // No template found
  35. }
  36. return nil, fmt.Errorf("failed to get template: %w", result.Error)
  37. }
  38. return &template, nil
  39. }
  40. // Update updates an existing template
  41. func (ts *TemplateStore) Update(template *models.Template) error {
  42. return ts.db.Save(template).Error
  43. }
  44. // Delete deletes a template by ID
  45. func (ts *TemplateStore) Delete(id string) error {
  46. return ts.db.Delete(&models.Template{}, "id = ?", id).Error
  47. }
  48. // List retrieves all templates with optional filtering
  49. func (ts *TemplateStore) List(filter map[string]interface{}) ([]*models.Template, error) {
  50. var templates []*models.Template
  51. // Build query from filters
  52. query := ts.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(&templates).Error; err != nil {
  60. return nil, fmt.Errorf("failed to list templates: %w", err)
  61. }
  62. return templates, nil
  63. }
  64. // GetTemplateWithDeployments retrieves a template by ID with associated deployments
  65. func (ts *TemplateStore) GetTemplateWithDeployments(id string) (*models.Template, error) {
  66. var template models.Template
  67. result := ts.db.Preload("Deployments").First(&template, "id = ?", id)
  68. if result.Error != nil {
  69. if result.Error == gorm.ErrRecordNotFound {
  70. return nil, nil // No template found
  71. }
  72. return nil, fmt.Errorf("failed to get template: %w", result.Error)
  73. }
  74. return &template, nil
  75. }
  76. // GetByVersion retrieves a template by name and version
  77. func (ts *TemplateStore) GetByVersion(name string, version string) (*models.Template, error) {
  78. var template models.Template
  79. result := ts.db.Where("name = ? AND version = ?", name, version).First(&template)
  80. if result.Error != nil {
  81. if result.Error == gorm.ErrRecordNotFound {
  82. return nil, nil // No template found
  83. }
  84. return nil, fmt.Errorf("failed to get template: %w", result.Error)
  85. }
  86. return &template, nil
  87. }