Skip to content

Commit 3127845

Browse files
authored
Merge branch 'casibase:master' into feat-records
2 parents ecdee58 + b0dd52e commit 3127845

40 files changed

+2101
-1167
lines changed

controllers/application.go

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package controllers
1616

1717
import (
1818
"encoding/json"
19+
"fmt"
1920

21+
"github.com/beego/beego/utils/pagination"
2022
"github.com/casibase/casibase/object"
2123
"github.com/casibase/casibase/util"
2224
)
@@ -38,14 +40,37 @@ type ApplicationDeploymentRequest struct {
3840
// @router /get-applications [get]
3941
func (c *ApiController) GetApplications() {
4042
owner := c.Input().Get("owner")
41-
42-
res, err := object.GetApplications(owner)
43-
if err != nil {
44-
c.ResponseError(err.Error())
45-
return
43+
limit := c.Input().Get("pageSize")
44+
page := c.Input().Get("p")
45+
field := c.Input().Get("field")
46+
value := c.Input().Get("value")
47+
sortField := c.Input().Get("sortField")
48+
sortOrder := c.Input().Get("sortOrder")
49+
50+
if limit == "" || page == "" {
51+
applications, err := object.GetApplications(owner)
52+
if err != nil {
53+
c.ResponseError(err.Error())
54+
return
55+
}
56+
c.ResponseOk(applications)
57+
} else {
58+
limit := util.ParseInt(limit)
59+
count, err := object.GetApplicationCount(owner, field, value)
60+
if err != nil {
61+
c.ResponseError(err.Error())
62+
return
63+
}
64+
65+
paginator := pagination.SetPaginator(c.Ctx, limit, count)
66+
applications, err := object.GetPaginationApplications(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
67+
if err != nil {
68+
c.ResponseError(err.Error())
69+
return
70+
}
71+
72+
c.ResponseOk(applications, paginator.Nums())
4673
}
47-
48-
c.ResponseOk(res)
4974
}
5075

5176
// GetApplication
@@ -58,7 +83,7 @@ func (c *ApiController) GetApplications() {
5883
func (c *ApiController) GetApplication() {
5984
id := c.Input().Get("id")
6085

61-
res, err := object.GetApplication(util.GetOwnerAndNameFromId(id))
86+
res, err := object.GetApplication(id)
6287
if err != nil {
6388
c.ResponseError(err.Error())
6489
return
@@ -160,7 +185,7 @@ func (c *ApiController) DeleteApplication() {
160185
return
161186
}
162187

163-
success, err := object.DeleteApplication(application.Owner, application.Name)
188+
success, err := object.DeleteApplication(&application)
164189
if err != nil {
165190
c.ResponseError(err.Error())
166191
return
@@ -172,41 +197,32 @@ func (c *ApiController) DeleteApplication() {
172197
// DeployApplication
173198
// @Title DeployApplication
174199
// @Tag Application API
175-
// @Description deploy application
200+
// @Description deploy application synchronously
176201
// @Param body body ApplicationDeploymentRequest true "The deployment request details"
177202
// @Success 200 {object} controllers.Response The Response object
178203
// @router /deploy-application [post]
179204
func (c *ApiController) DeployApplication() {
180-
var req ApplicationDeploymentRequest
181-
err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
205+
id := c.Input().Get("id")
206+
207+
var application object.Application
208+
err := json.Unmarshal(c.Ctx.Input.RequestBody, &application)
182209
if err != nil {
183210
c.ResponseError(err.Error())
184211
return
185212
}
186213

187-
if req.Owner == "" || req.Name == "" || req.Template == "" {
188-
c.ResponseError("Missing required parameters")
189-
return
190-
}
191-
192-
// Get the existing application
193-
application, err := object.GetApplication(req.Owner, req.Name)
214+
originalApplication, err := object.GetApplication(id)
194215
if err != nil {
195216
c.ResponseError(err.Error())
196217
return
197218
}
198219

199-
// Application must exist before deployment
200-
if application == nil {
201-
c.ResponseError("Application not found. Please create the application first.")
220+
if originalApplication == nil {
221+
c.ResponseError(fmt.Sprintf("The application: %s is not found", id))
202222
return
203223
}
204224

205-
// Update application with new template and parameters
206-
application.Template = req.Template
207-
application.Parameters = req.Parameters
208-
209-
success, err := object.UpdateApplication(req.Owner+"/"+req.Name, application)
225+
success, err := object.UpdateApplication(id, &application)
210226
if err != nil {
211227
c.ResponseError(err.Error())
212228
return
@@ -216,8 +232,8 @@ func (c *ApiController) DeployApplication() {
216232
return
217233
}
218234

219-
// Deploy the application
220-
success, err = object.DeployApplication(application)
235+
// Deploy the application synchronously and wait for completion
236+
success, err = object.DeployApplicationSync(&application)
221237
if err != nil {
222238
c.ResponseError(err.Error())
223239
return
@@ -229,24 +245,28 @@ func (c *ApiController) DeployApplication() {
229245
// UndeployApplication
230246
// @Title UndeployApplication
231247
// @Tag Application API
232-
// @Description delete application deployment
248+
// @Description undeploy application synchronously
233249
// @Param body body ApplicationDeploymentRequest true "The deployment request details"
234250
// @Success 200 {object} controllers.Response The Response object
235251
// @router /undeploy-application [post]
236252
func (c *ApiController) UndeployApplication() {
237-
var req ApplicationDeploymentRequest
238-
err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
253+
id := c.Input().Get("id")
254+
255+
application, err := object.GetApplication(id)
239256
if err != nil {
240257
c.ResponseError(err.Error())
241258
return
242259
}
243260

244-
if req.Owner == "" || req.Name == "" {
245-
c.ResponseError("Missing required parameters")
261+
if application == nil {
262+
c.ResponseError(fmt.Sprintf("The application: %s is not found", id))
246263
return
247264
}
248265

249-
success, err := object.UndeployApplication(req.Owner, req.Name)
266+
owner, name := util.GetOwnerAndNameFromId(id)
267+
268+
// Undeploy the application synchronously and wait for completion
269+
success, err := object.UndeployApplicationSync(owner, name)
250270
if err != nil {
251271
c.ResponseError(err.Error())
252272
return

controllers/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (c *ApiController) GetProviders() {
6565
providers = object.GetMaskedProviders(providers, true, user)
6666
c.ResponseOk(providers)
6767
} else {
68-
if !c.IsPreviewMode() && !c.RequireAdmin() {
68+
if !c.RequireAdmin() {
6969
return
7070
}
7171
limit := util.ParseInt(limit)

controllers/store.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ func (c *ApiController) GetGlobalStores() {
4646

4747
c.ResponseOk(stores)
4848
} else {
49-
if !c.IsPreviewMode() && !c.RequireAdmin() {
49+
if !c.RequireAdmin() {
5050
return
5151
}
52+
5253
limit := util.ParseInt(limit)
5354
count, err := object.GetStoreCount(field, value)
5455
if err != nil {

controllers/template.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package controllers
1717
import (
1818
"encoding/json"
1919

20+
"github.com/beego/beego/utils/pagination"
2021
"github.com/casibase/casibase/object"
2122
"github.com/casibase/casibase/util"
2223
)
@@ -30,14 +31,37 @@ import (
3031
// @router /get-templates [get]
3132
func (c *ApiController) GetTemplates() {
3233
owner := c.Input().Get("owner")
33-
34-
res, err := object.GetTemplates(owner)
35-
if err != nil {
36-
c.ResponseError(err.Error())
37-
return
34+
limit := c.Input().Get("pageSize")
35+
page := c.Input().Get("p")
36+
field := c.Input().Get("field")
37+
value := c.Input().Get("value")
38+
sortField := c.Input().Get("sortField")
39+
sortOrder := c.Input().Get("sortOrder")
40+
41+
if limit == "" || page == "" {
42+
templates, err := object.GetTemplates(owner)
43+
if err != nil {
44+
c.ResponseError(err.Error())
45+
return
46+
}
47+
c.ResponseOk(templates)
48+
} else {
49+
limit := util.ParseInt(limit)
50+
count, err := object.GetTemplateCount(owner, field, value)
51+
if err != nil {
52+
c.ResponseError(err.Error())
53+
return
54+
}
55+
56+
paginator := pagination.SetPaginator(c.Ctx, limit, count)
57+
templates, err := object.GetPaginationTemplates(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
58+
if err != nil {
59+
c.ResponseError(err.Error())
60+
return
61+
}
62+
63+
c.ResponseOk(templates, paginator.Nums())
3864
}
39-
40-
c.ResponseOk(res)
4165
}
4266

4367
// GetTemplate

controllers/util.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ func (c *ApiController) CheckSignedIn() (string, bool) {
113113
}
114114

115115
func (c *ApiController) RequireAdmin() bool {
116+
disablePreviewMode, _ := beego.AppConfig.Bool("disablePreviewMode")
117+
if !disablePreviewMode {
118+
return true
119+
}
120+
116121
if !c.IsAdmin() {
117122
c.ResponseError("this operation requires admin privilege")
118123
return false
@@ -131,15 +136,6 @@ func (c *ApiController) IsAdmin() bool {
131136
return res
132137
}
133138

134-
func (c *ApiController) IsPreviewMode() bool {
135-
disablePreviewMode, _ := beego.AppConfig.Bool("disablePreviewMode")
136-
if disablePreviewMode {
137-
c.ResponseError("this operation requires admin privilege")
138-
return false
139-
}
140-
return true
141-
}
142-
143139
func DenyRequest(ctx *context.Context) {
144140
responseError(ctx, "Unauthorized operation")
145141
}

object/activity.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func GetActivities(days int, user string, fieldName string) ([]*Activity, error)
9898
}
9999
value, err := getTargetfieldValue(record, fieldName)
100100
if err != nil {
101-
return nil, err
101+
return nil, fmt.Errorf("failed to parse record: name %s, field %s, error: %v", record.Name, fieldName, err)
102102
}
103103
activities[dayIndex].FieldCount[value] += 1
104104
}

0 commit comments

Comments
 (0)