-
Notifications
You must be signed in to change notification settings - Fork 2
/
workspace.go
176 lines (149 loc) · 5.24 KB
/
workspace.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package db
import (
"fmt"
"github.com/pyneda/sukyan/lib"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)
// Workspace is used to group projects
type Workspace struct {
BaseModel
Code string `gorm:"index,unique" json:"code"`
Title string `json:"title"`
Description string `json:"description"`
}
func (w Workspace) TableHeaders() []string {
return []string{"ID", "Code", "Title", "Description"}
}
func (w Workspace) TableRow() []string {
return []string{
fmt.Sprintf("%d", w.ID),
w.Code,
w.Title,
w.Description,
}
}
// String provides a basic textual representation of the Workspace.
func (w Workspace) String() string {
return fmt.Sprintf("ID: %d, Code: %s, Title: %s, Description: %s", w.ID, w.Code, w.Title, w.Description)
}
// Pretty provides a more formatted, user-friendly representation of the Workspace.
func (w Workspace) Pretty() string {
return fmt.Sprintf("%sID:%s %d, %sCode:%s %s, %sTitle:%s %s, %sDescription:%s %s",
lib.Blue, lib.ResetColor, w.ID,
lib.Blue, lib.ResetColor, w.Code,
lib.Blue, lib.ResetColor, w.Title,
lib.Blue, lib.ResetColor, w.Description)
}
// GetWorkspaceByID gets a workspace by ID
func (d *DatabaseConnection) GetWorkspaceByID(id uint) (*Workspace, error) {
var workspace Workspace
if err := d.db.Where("id = ?", id).First(&workspace).Error; err != nil {
log.Error().Err(err).Interface("id", id).Msg("Unable to fetch workspace by ID")
return nil, err
}
return &workspace, nil
}
// GetWorkspaceByCode gets a workspace by code
func (d *DatabaseConnection) GetWorkspaceByCode(code string) (*Workspace, error) {
var workspace Workspace
if err := d.db.Where("code = ?", code).First(&workspace).Error; err != nil {
log.Error().Err(err).Interface("code", code).Msg("Unable to fetch workspace by code")
return nil, err
}
return &workspace, nil
}
// WorkspaceExists checks if a workspace exists
func (d *DatabaseConnection) WorkspaceExists(id uint) (bool, error) {
var count int64
err := d.db.Model(&Workspace{}).Where("id = ?", id).Count(&count).Error
if err != nil {
return false, err
}
return count > 0, nil
}
type WorkspaceFilters struct {
Query string `json:"query" validate:"omitempty,dive,ascii"`
Pagination Pagination
}
// ListWorkspaces Lists workspaces
func (d *DatabaseConnection) ListWorkspaces(filters WorkspaceFilters) (items []*Workspace, count int64, err error) {
query := d.db
if filters.Pagination.Page > 0 && filters.Pagination.PageSize > 0 {
query = query.Scopes(Paginate(&filters.Pagination))
}
if filters.Query != "" {
likeQuery := "%" + filters.Query + "%"
query = query.Where("code LIKE ? OR title LIKE ? OR description LIKE ?", likeQuery, likeQuery, likeQuery)
}
result := query.Find(&items).Count(&count)
if result.Error != nil {
err = result.Error
}
return items, count, err
}
func (d *DatabaseConnection) CreateDefaultWorkspace() (*Workspace, error) {
workspace := Workspace{
Code: "default",
Title: "Default workspace",
Description: "Default workspace",
}
return d.GetOrCreateWorkspace(&workspace)
}
// CreateWorkspace saves a workspace to the database
func (d *DatabaseConnection) CreateWorkspace(workspace *Workspace) (*Workspace, error) {
result := d.db.Create(&workspace)
if result.Error != nil {
log.Error().Err(result.Error).Interface("workspace", workspace).Msg("Workspace creation failed")
}
return workspace, result.Error
}
// GetOrCreateWorkspace gets a workspace with the given code, or creates it if it doesn't exist
func (d *DatabaseConnection) GetOrCreateWorkspace(workspace *Workspace) (*Workspace, error) {
var existingWorkspace Workspace
if err := d.db.Where("code = ?", workspace.Code).First(&existingWorkspace).Error; err == nil {
return &existingWorkspace, nil
} else if err != gorm.ErrRecordNotFound {
log.Error().Err(err).Interface("workspace", workspace).Msg("Error checking workspace by code")
return nil, err
}
if err := d.db.Create(&workspace).Error; err != nil {
log.Error().Err(err).Interface("workspace", workspace).Msg("Workspace creation failed")
return nil, err
}
return workspace, nil
}
// DeleteWorkspace deletes a workspace by ID
func (d *DatabaseConnection) DeleteWorkspace(id uint) error {
var workspace Workspace
if err := d.db.Unscoped().Where("id = ?", id).Delete(&workspace).Error; err != nil {
log.Error().Err(err).Interface("id", id).Msg("Unable to delete workspace by ID")
return err
}
return nil
}
// UpdateWorkspace updates a workspace by its ID with the provided fields
func (d *DatabaseConnection) UpdateWorkspace(id uint, updatedWorkspace *Workspace) error {
var workspace Workspace
// Fetch the workspace by ID
if err := d.db.Where("id = ?", id).First(&workspace).Error; err != nil {
log.Error().Err(err).Interface("id", id).Msg("Unable to fetch workspace by ID for updating")
return err
}
// Update the relevant fields
if updatedWorkspace.Code != "" {
workspace.Code = updatedWorkspace.Code
}
if updatedWorkspace.Title != "" {
workspace.Title = updatedWorkspace.Title
}
if updatedWorkspace.Description != "" {
workspace.Description = updatedWorkspace.Description
}
// Save the updated workspace
if err := d.db.Save(&workspace).Error; err != nil {
log.Error().Err(err).Interface("workspace", workspace).Msg("Unable to update workspace")
return err
}
return nil
}