@@ -16,7 +16,9 @@ package controllers
1616
1717import (
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]
3941func (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() {
5883func (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]
179204func (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]
236252func (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
0 commit comments