Skip to content

Commit

Permalink
add API endpoint to remove a app's image
Browse files Browse the repository at this point in the history
  • Loading branch information
tessus committed May 24, 2023
1 parent a37afce commit 5b6f7b0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
24 changes: 24 additions & 0 deletions api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,30 @@ func (a *ApplicationAPI) UploadApplicationImage(ctx *gin.Context) {
})
}

func (a *ApplicationAPI) RemoveApplicationImage(ctx *gin.Context) {
withID(ctx, "id", func(id uint) {
app, err := a.DB.GetApplicationByID(id)
if success := successOrAbort(ctx, 500, err); !success {
return

Check warning on line 365 in api/application.go

View check run for this annotation

Codecov / codecov/patch

api/application.go#L362-L365

Added lines #L362 - L365 were not covered by tests
}
if app != nil && app.UserID == auth.GetUserID(ctx) {
if app.Image == "" {
ctx.AbortWithError(400, fmt.Errorf("app with id %d does not have a customized image", id))
return

Check warning on line 370 in api/application.go

View check run for this annotation

Codecov / codecov/patch

api/application.go#L367-L370

Added lines #L367 - L370 were not covered by tests
}

app.Image = ""
if success := successOrAbort(ctx, 500, a.DB.UpdateApplication(app)); !success {
return

Check warning on line 375 in api/application.go

View check run for this annotation

Codecov / codecov/patch

api/application.go#L373-L375

Added lines #L373 - L375 were not covered by tests
}
os.Remove(a.ImageDir + app.Image)
ctx.JSON(200, withResolvedImage(app))
} else {
ctx.AbortWithError(404, fmt.Errorf("app with id %d doesn't exists", id))

Check warning on line 380 in api/application.go

View check run for this annotation

Codecov / codecov/patch

api/application.go#L377-L380

Added lines #L377 - L380 were not covered by tests
}
})
}

func withResolvedImage(app *model.Application) *model.Application {
if app.Image == "" {
app.Image = "static/defaultapp.png"
Expand Down
2 changes: 2 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Co

app.POST("/:id/image", applicationHandler.UploadApplicationImage)

app.DELETE("/:id/image", applicationHandler.RemoveApplicationImage)

app.PUT("/:id", applicationHandler.UpdateApplication)

app.DELETE("/:id", applicationHandler.DeleteApplication)
Expand Down

0 comments on commit 5b6f7b0

Please sign in to comment.