Skip to content

Commit 5fc6623

Browse files
author
rn-h
committed
Add: delete app,deployment
1 parent ef5e209 commit 5fc6623

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
config/app.dev.json
2+
bundels
3+
__debug_bin*

main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
)
1414

1515
func main() {
16-
fmt.Println("code-push-server-go V1.0.0")
17-
16+
fmt.Println("code-push-server-go V1.0.1")
17+
// gin.SetMode(gin.ReleaseMode)
1818
g := gin.Default()
1919
g.Use(gzip.Gzip(gzip.DefaultCompression))
2020
g.Use(middleware.Recover)
@@ -34,8 +34,8 @@ func main() {
3434
authApi.POST("/createDeployment", request.App{}.CreateDeployment)
3535
authApi.POST("/createBundle", request.App{}.CreateBundle)
3636
authApi.POST("/checkBundle", request.App{}.CheckBundle)
37-
authApi.POST("/delApp")
38-
authApi.POST("/delDeployment")
37+
authApi.POST("/delApp", request.App{}.DelApp)
38+
authApi.POST("/delDeployment", request.App{}.DelDeployment)
3939
authApi.POST("/lsDeployment", request.App{}.LsDeployment)
4040
authApi.GET("/lsApp", request.App{}.LsApp)
4141
authApi.POST("/uploadBundle", request.App{}.UploadBundle)

model/base.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func GetList[T any](sql string, arg any) *[]T {
4949
return t
5050
}
5151

52-
func Delete[T any](deleteData *T) {
53-
userDb.Delete(&deleteData)
52+
func Delete[T any](deleteData T) error {
53+
return userDb.Delete(deleteData).Error
54+
}
55+
56+
func DeleteWhere(query string, args string, deleteData any) error {
57+
return userDb.Where(query, args).Delete(deleteData).Error
5458
}

request/app.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88

99
"com.lc.go.codepush/server/config"
10+
"com.lc.go.codepush/server/db"
1011
"com.lc.go.codepush/server/db/redis"
1112
"com.lc.go.codepush/server/model"
1213
"com.lc.go.codepush/server/model/constants"
@@ -19,6 +20,7 @@ import (
1920
"github.com/gin-gonic/gin/binding"
2021
"github.com/google/uuid"
2122
"github.com/jlaffaye/ftp"
23+
"gorm.io/gorm"
2224
)
2325

2426
type App struct{}
@@ -340,3 +342,72 @@ func (App) CheckBundle(ctx *gin.Context) {
340342
log.Panic(err.Error())
341343
}
342344
}
345+
346+
type delAppInfo struct {
347+
AppName *string `json:"appName" binding:"required"`
348+
}
349+
350+
func (App) DelApp(ctx *gin.Context) {
351+
delAppInfo := delAppInfo{}
352+
if err := ctx.ShouldBindBodyWith(&delAppInfo, binding.JSON); err == nil {
353+
uid := ctx.MustGet(constants.GIN_USER_ID).(int)
354+
355+
app := model.App{}.GetAppByUidAndAppName(uid, *delAppInfo.AppName)
356+
if app == nil {
357+
log.Panic("App not found")
358+
}
359+
deployment := model.Deployment{}.GetByAppids(*app.Id)
360+
if deployment != nil && len(*deployment) > 0 {
361+
log.Panic("App exist deployment,Delete the deployment first and then delete the app ")
362+
}
363+
model.Delete[model.App](model.App{Id: app.Id})
364+
ctx.JSON(http.StatusOK, gin.H{
365+
"success": true,
366+
})
367+
} else {
368+
log.Panic(err.Error())
369+
}
370+
}
371+
372+
type delDeploymentInfo struct {
373+
AppName *string `json:"appName" binding:"required"`
374+
Deployment *string `json:"deployment" binding:"required"`
375+
}
376+
377+
func (App) DelDeployment(ctx *gin.Context) {
378+
delDeploymentInfo := delDeploymentInfo{}
379+
if err := ctx.ShouldBindBodyWith(&delDeploymentInfo, binding.JSON); err == nil {
380+
uid := ctx.MustGet(constants.GIN_USER_ID).(int)
381+
382+
app := model.App{}.GetAppByUidAndAppName(uid, *delDeploymentInfo.AppName)
383+
if app == nil {
384+
log.Panic("App not found")
385+
}
386+
deployment := model.Deployment{}.GetByAppidAndName(*app.Id, *delDeploymentInfo.Deployment)
387+
if deployment == nil {
388+
log.Panic("Deployment " + *delDeploymentInfo.Deployment + " not found")
389+
}
390+
userDb, _ := db.GetUserDB()
391+
err := userDb.Transaction(func(tx *gorm.DB) error {
392+
if err := tx.Delete(model.Deployment{Id: deployment.Id}).Error; err != nil {
393+
panic("DeleteError:" + err.Error())
394+
}
395+
if err := tx.Where("deployment_id", *deployment.Id).Delete(model.DeploymentVersion{}).Error; err != nil {
396+
panic("DeleteError:" + err.Error())
397+
}
398+
if err := tx.Where("deployment_id", *deployment.Id).Delete(model.Package{}).Error; err != nil {
399+
panic("DeleteError:" + err.Error())
400+
}
401+
return nil
402+
})
403+
if err != nil {
404+
panic("DeleteError:" + err.Error())
405+
}
406+
407+
ctx.JSON(http.StatusOK, gin.H{
408+
"success": true,
409+
})
410+
} else {
411+
log.Panic(err.Error())
412+
}
413+
}

0 commit comments

Comments
 (0)