-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplatesmodel.go
More file actions
138 lines (102 loc) · 3.75 KB
/
templatesmodel.go
File metadata and controls
138 lines (102 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package menu
import (
"fmt"
"time"
"gorm.io/gorm"
)
type TblGoTemplates struct {
Id int
TemplateName string
TemplateDescription string
TemplateImage string
IsDeleted int
TenantId string
CreatedBy int
ChannelSlugName string
TemplateModuleName string
CreatedOn time.Time
DateString string `gorm:"-:migration;<-:false"`
}
func (menu *MenuModel) ListGoTemplates(tenantid string, DB *gorm.DB) (list []TblGoTemplates, count int64, err error) {
var GoTemplatesList []TblGoTemplates
query := DB.Table("tbl_go_templates").Where("is_deleted = 0")
if tenantid != "" {
query = query.Where("tenant_id = ?", tenantid)
} else {
query = query.Where("tenant_id IS NULL")
}
if err = query.Count(&count).Error; err != nil {
return []TblGoTemplates{}, 0, err
}
if err = query.Order("id ASC").Find(&GoTemplatesList).Error; err != nil {
return []TblGoTemplates{}, 0, err
}
return GoTemplatesList, count, nil
}
func (menu *MenuModel) GetTemplateById(moduleid int, tenantid string, DB *gorm.DB) (templates TblGoTemplates, err error) {
if err := DB.Table("tbl_go_templates").Where("is_deleted = 0 and id=? ", moduleid).Order("id asc").Find(&templates).Error; err != nil {
return TblGoTemplates{}, err
}
return templates, nil
}
func (menu *MenuModel) CloneTemplatesBySlug(db *gorm.DB, slug string, tenantID string, userid int, usertype string) error {
var templates []TblGoTemplates
switch usertype {
case "new":
if err := db.Debug().Where("channel_slug_name = ? AND is_deleted = 0 AND tenant_id IS NULL", slug).Find(&templates).Error; err != nil {
return fmt.Errorf("error fetching templates for new user: %w", err)
}
case "old":
var count int64
if err := db.Model(&TblGoTemplates{}).Where("channel_slug_name = ? AND tenant_id = ? AND is_deleted = 0", slug, tenantID).Count(&count).Error; err != nil {
return fmt.Errorf("error checking existing old user templates: %w", err)
}
if count > 0 {
return nil
}
if err := db.Debug().Where("channel_slug_name = ? AND is_deleted = 0 AND tenant_id IS NULL", slug).Find(&templates).Error; err != nil {
return fmt.Errorf("error fetching global templates for old user: %w", err)
}
default:
return fmt.Errorf("invalid userType: must be 'new' or 'old'")
}
if len(templates) == 0 {
return fmt.Errorf("no templates found to clone for slug: %s", slug)
}
var newTemplates []TblGoTemplates
now := time.Now().UTC()
for _, t := range templates {
t.Id = 0
t.TenantId = tenantID
t.CreatedOn = now
t.CreatedBy = userid
newTemplates = append(newTemplates, t)
}
if err := db.Create(&newTemplates).Error; err != nil {
return fmt.Errorf("error inserting templates: %w", err)
}
var template TblGoTemplates
if err := db.Debug().Where("is_deleted = 0 AND tenant_id=? and created_by=?", tenantID, userid).First(&template).Error; err != nil {
return fmt.Errorf("error fetching templates for new user: %w", err)
}
if err := db.Debug().Table("tbl_users").Where("is_deleted = 0 AND tenant_id = ? AND id = ?", tenantID, userid).Updates(map[string]interface{}{"go_template_default": template.Id}).Error; err != nil {
return err
}
return nil
}
// createwebsite
func (menu *MenuModel) CreateTemplate(template *TblGoTemplates, DB *gorm.DB) (TblGoTemplates, error) {
var existingTemplate TblGoTemplates
err := DB.Table("tbl_go_templates").
Where("template_name = ? AND tenant_id = ?", template.TemplateName, template.TenantId).
First(&existingTemplate).Error
if err == nil {
return existingTemplate, nil
} else if err != gorm.ErrRecordNotFound {
return TblGoTemplates{}, err
}
if err := DB.Table("tbl_go_templates").Create(&template).Error; err != nil {
return TblGoTemplates{}, err
}
return *template, nil
}