Skip to content

Commit 1beb94c

Browse files
dark-Qyhsluoyz
authored andcommitted
feat: refactor template and application CURD logic (casibase#1459)
1 parent 5727ee5 commit 1beb94c

File tree

5 files changed

+33
-46
lines changed

5 files changed

+33
-46
lines changed

controllers/application.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ import (
2323
"github.com/casibase/casibase/util"
2424
)
2525

26-
type ApplicationDeploymentRequest struct {
27-
Owner string `json:"owner"`
28-
Name string `json:"name"`
29-
DisplayName string `json:"displayName"`
30-
Template string `json:"template"`
31-
Parameters string `json:"parameters"`
32-
}
33-
3426
// GetApplications
3527
// @Title GetApplications
3628
// @Tag Application API
@@ -127,20 +119,20 @@ func (c *ApiController) UpdateApplication() {
127119
// @Success 200 {object} controllers.Response The Response object
128120
// @router /add-application [post]
129121
func (c *ApiController) AddApplication() {
130-
var req ApplicationDeploymentRequest
131-
err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
122+
var application object.Application
123+
err := json.Unmarshal(c.Ctx.Input.RequestBody, &application)
132124
if err != nil {
133125
c.ResponseError(err.Error())
134126
return
135127
}
136128

137-
if req.Owner == "" || req.Name == "" || req.Template == "" {
129+
if application.Template == "" {
138130
c.ResponseError("Missing required parameters")
139131
return
140132
}
141133

142134
// Verify template exists
143-
template, err := object.GetTemplate(req.Owner, req.Template)
135+
template, err := object.GetTemplate(util.GetIdFromOwnerAndName(application.Owner, application.Template))
144136
if err != nil {
145137
c.ResponseError(err.Error())
146138
return
@@ -151,17 +143,7 @@ func (c *ApiController) AddApplication() {
151143
return
152144
}
153145

154-
// Create new application
155-
application := &object.Application{
156-
Owner: req.Owner,
157-
Name: req.Name,
158-
DisplayName: req.DisplayName,
159-
Template: req.Template,
160-
Parameters: req.Parameters,
161-
Status: "Not Deployed",
162-
}
163-
164-
success, err := object.AddApplication(application)
146+
success, err := object.AddApplication(&application)
165147
if err != nil {
166148
c.ResponseError(err.Error())
167149
return
@@ -198,7 +180,7 @@ func (c *ApiController) DeleteApplication() {
198180
// @Title DeployApplication
199181
// @Tag Application API
200182
// @Description deploy application synchronously
201-
// @Param body body ApplicationDeploymentRequest true "The deployment request details"
183+
// @Param body body object.Application true "The details of the application"
202184
// @Success 200 {object} controllers.Response The Response object
203185
// @router /deploy-application [post]
204186
func (c *ApiController) DeployApplication() {
@@ -246,7 +228,7 @@ func (c *ApiController) DeployApplication() {
246228
// @Title UndeployApplication
247229
// @Tag Application API
248230
// @Description undeploy application synchronously
249-
// @Param body body ApplicationDeploymentRequest true "The deployment request details"
231+
// @Param body body object.Application true "The details of the application"
250232
// @Success 200 {object} controllers.Response The Response object
251233
// @router /undeploy-application [post]
252234
func (c *ApiController) UndeployApplication() {

controllers/template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (c *ApiController) GetTemplates() {
7474
func (c *ApiController) GetTemplate() {
7575
id := c.Input().Get("id")
7676

77-
res, err := object.GetTemplate(util.GetOwnerAndNameFromId(id))
77+
res, err := object.GetTemplate(id)
7878
if err != nil {
7979
c.ResponseError(err.Error())
8080
return
@@ -149,7 +149,7 @@ func (c *ApiController) DeleteTemplate() {
149149
return
150150
}
151151

152-
success, err := object.DeleteTemplate(template.Owner, template.Name)
152+
success, err := object.DeleteTemplate(&template)
153153
if err != nil {
154154
c.ResponseError(err.Error())
155155
return

object/application.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ package object
1717
import (
1818
"fmt"
1919
"strings"
20-
"xorm.io/core"
2120

2221
"github.com/casibase/casibase/util"
22+
"xorm.io/core"
2323
)
2424

2525
type Application struct {
@@ -82,16 +82,16 @@ func GetApplication(id string) (*Application, error) {
8282

8383
func UpdateApplication(id string, application *Application) (bool, error) {
8484
owner, name := util.GetOwnerAndNameFromId(id)
85-
86-
if application.Owner == "" {
87-
application.Owner = owner
85+
application.UpdatedTime = util.GetCurrentTime()
86+
_, err := getApplication(owner, name)
87+
if err != nil {
88+
return false, err
8889
}
89-
if application.Name == "" {
90-
application.Name = name
90+
if application == nil {
91+
return false, nil
9192
}
92-
application.UpdatedTime = util.GetCurrentTime()
9393

94-
affected, err := adapter.engine.ID(core.PK{owner, name}).Update(application)
94+
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(application)
9595
if err != nil {
9696
return false, err
9797
}

object/application_k8s.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func DeployApplication(application *Application) (bool, error) {
145145
}
146146

147147
// Get the template
148-
template, err := GetTemplate(application.Owner, application.Template)
148+
template, err := getTemplate(application.Owner, application.Template)
149149
if err != nil {
150150
return false, fmt.Errorf("failed to get template: %v", err)
151151
}

object/template.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ func GetPaginationTemplates(owner string, offset, limit int, field, value, sortF
5757
return templates, nil
5858
}
5959

60-
func GetTemplate(owner, name string) (*Template, error) {
60+
func GetTemplate(id string) (*Template, error) {
61+
owner, name := util.GetOwnerAndNameFromId(id)
62+
return getTemplate(owner, name)
63+
}
64+
65+
func getTemplate(owner, name string) (*Template, error) {
6166
template := Template{Owner: owner, Name: name}
6267
existed, err := adapter.engine.Get(&template)
6368
if err != nil {
@@ -73,16 +78,16 @@ func GetTemplate(owner, name string) (*Template, error) {
7378

7479
func UpdateTemplate(id string, template *Template) (bool, error) {
7580
owner, name := util.GetOwnerAndNameFromId(id)
76-
77-
if template.Owner == "" {
78-
template.Owner = owner
81+
template.UpdatedTime = util.GetCurrentTime()
82+
_, err := getTemplate(owner, name)
83+
if err != nil {
84+
return false, err
7985
}
80-
if template.Name == "" {
81-
template.Name = name
86+
if template == nil {
87+
return false, nil
8288
}
83-
template.UpdatedTime = util.GetCurrentTime()
8489

85-
affected, err := adapter.engine.ID(core.PK{owner, name}).Update(template)
90+
affected, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(template)
8691
if err != nil {
8792
return false, err
8893
}
@@ -106,8 +111,8 @@ func AddTemplate(template *Template) (bool, error) {
106111
return affected != 0, nil
107112
}
108113

109-
func DeleteTemplate(owner, name string) (bool, error) {
110-
affected, err := adapter.engine.Delete(&Template{Owner: owner, Name: name})
114+
func DeleteTemplate(template *Template) (bool, error) {
115+
affected, err := adapter.engine.ID(core.PK{template.Owner, template.Name}).Delete(&Template{})
111116
if err != nil {
112117
return false, err
113118
}

0 commit comments

Comments
 (0)