Skip to content

Commit 5cd2d54

Browse files
authored
Add delete application image endpoint
1 parent a37afce commit 5cd2d54

4 files changed

Lines changed: 179 additions & 0 deletions

File tree

api/application.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,70 @@ func (a *ApplicationAPI) UploadApplicationImage(ctx *gin.Context) {
358358
})
359359
}
360360

361+
// RemoveApplicationImage deletes an image of an application.
362+
// swagger:operation DELETE /application/{id}/image application removeAppImage
363+
//
364+
// Deletes an image of an application.
365+
//
366+
// ---
367+
// consumes: [application/json]
368+
// produces: [application/json]
369+
// parameters:
370+
// - name: id
371+
// in: path
372+
// description: the application id
373+
// required: true
374+
// type: integer
375+
// format: int64
376+
// security: [clientTokenAuthorizationHeader: [], clientTokenHeader: [], clientTokenQuery: [], basicAuth: []]
377+
// responses:
378+
// 200:
379+
// description: Ok
380+
// 400:
381+
// description: Bad Request
382+
// schema:
383+
// $ref: "#/definitions/Error"
384+
// 401:
385+
// description: Unauthorized
386+
// schema:
387+
// $ref: "#/definitions/Error"
388+
// 403:
389+
// description: Forbidden
390+
// schema:
391+
// $ref: "#/definitions/Error"
392+
// 404:
393+
// description: Not Found
394+
// schema:
395+
// $ref: "#/definitions/Error"
396+
// 500:
397+
// description: Server Error
398+
// schema:
399+
// $ref: "#/definitions/Error"
400+
func (a *ApplicationAPI) RemoveApplicationImage(ctx *gin.Context) {
401+
withID(ctx, "id", func(id uint) {
402+
app, err := a.DB.GetApplicationByID(id)
403+
if success := successOrAbort(ctx, 500, err); !success {
404+
return
405+
}
406+
if app != nil && app.UserID == auth.GetUserID(ctx) {
407+
if app.Image == "" {
408+
ctx.AbortWithError(400, fmt.Errorf("app with id %d does not have a customized image", id))
409+
return
410+
}
411+
412+
image := app.Image
413+
app.Image = ""
414+
if success := successOrAbort(ctx, 500, a.DB.UpdateApplication(app)); !success {
415+
return
416+
}
417+
os.Remove(a.ImageDir + image)
418+
ctx.JSON(200, withResolvedImage(app))
419+
} else {
420+
ctx.AbortWithError(404, fmt.Errorf("app with id %d doesn't exists", id))
421+
}
422+
})
423+
}
424+
361425
func withResolvedImage(app *model.Application) *model.Application {
362426
if app.Image == "" {
363427
app.Image = "static/defaultapp.png"

api/application_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,47 @@ func (s *ApplicationSuite) Test_UploadAppImage_expectNotFound() {
426426
assert.Equal(s.T(), 404, s.recorder.Code)
427427
}
428428

429+
func (s *ApplicationSuite) Test_RemoveAppImage_expectNotFound() {
430+
s.db.User(5)
431+
432+
test.WithUser(s.ctx, 5)
433+
s.ctx.Request = httptest.NewRequest("DELETE", "/irrelevant", nil)
434+
s.ctx.Params = gin.Params{{Key: "id", Value: "4"}}
435+
436+
s.a.RemoveApplicationImage(s.ctx)
437+
438+
assert.Equal(s.T(), 404, s.recorder.Code)
439+
}
440+
441+
func (s *ApplicationSuite) Test_RemoveAppImage_noCustomizedImage() {
442+
s.db.User(5).App(1)
443+
444+
test.WithUser(s.ctx, 5)
445+
s.ctx.Request = httptest.NewRequest("DELETE", "/irrelevant", nil)
446+
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
447+
s.a.RemoveApplicationImage(s.ctx)
448+
449+
assert.Equal(s.T(), 400, s.recorder.Code)
450+
}
451+
452+
func (s *ApplicationSuite) Test_RemoveAppImage_expectSuccess() {
453+
s.db.User(5)
454+
455+
imageFile := "existing.png"
456+
s.db.CreateApplication(&model.Application{UserID: 5, ID: 1, Image: imageFile})
457+
fakeImage(s.T(), imageFile)
458+
459+
test.WithUser(s.ctx, 5)
460+
s.ctx.Request = httptest.NewRequest("DELETE", "/irrelevant", nil)
461+
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
462+
s.a.RemoveApplicationImage(s.ctx)
463+
464+
_, err := os.Stat(imageFile)
465+
assert.True(s.T(), os.IsNotExist(err))
466+
467+
assert.Equal(s.T(), 200, s.recorder.Code)
468+
}
469+
429470
func (s *ApplicationSuite) Test_UploadAppImage_WithSaveError_expectServerError() {
430471
s.db.User(5).App(1)
431472

docs/spec.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,78 @@
365365
}
366366
}
367367
}
368+
},
369+
"delete": {
370+
"security": [
371+
{
372+
"clientTokenAuthorizationHeader": []
373+
},
374+
{
375+
"clientTokenHeader": []
376+
},
377+
{
378+
"clientTokenQuery": []
379+
},
380+
{
381+
"basicAuth": []
382+
}
383+
],
384+
"consumes": [
385+
"application/json"
386+
],
387+
"produces": [
388+
"application/json"
389+
],
390+
"tags": [
391+
"application"
392+
],
393+
"summary": "Deletes an image of an application.",
394+
"operationId": "removeAppImage",
395+
"parameters": [
396+
{
397+
"type": "integer",
398+
"format": "int64",
399+
"description": "the application id",
400+
"name": "id",
401+
"in": "path",
402+
"required": true
403+
}
404+
],
405+
"responses": {
406+
"200": {
407+
"description": "Ok"
408+
},
409+
"400": {
410+
"description": "Bad Request",
411+
"schema": {
412+
"$ref": "#/definitions/Error"
413+
}
414+
},
415+
"401": {
416+
"description": "Unauthorized",
417+
"schema": {
418+
"$ref": "#/definitions/Error"
419+
}
420+
},
421+
"403": {
422+
"description": "Forbidden",
423+
"schema": {
424+
"$ref": "#/definitions/Error"
425+
}
426+
},
427+
"404": {
428+
"description": "Not Found",
429+
"schema": {
430+
"$ref": "#/definitions/Error"
431+
}
432+
},
433+
"500": {
434+
"description": "Server Error",
435+
"schema": {
436+
"$ref": "#/definitions/Error"
437+
}
438+
}
439+
}
368440
}
369441
},
370442
"/application/{id}/message": {

router/router.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Co
119119

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

122+
app.DELETE("/:id/image", applicationHandler.RemoveApplicationImage)
123+
122124
app.PUT("/:id", applicationHandler.UpdateApplication)
123125

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

0 commit comments

Comments
 (0)