Skip to content

Commit

Permalink
Add delete application image endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tessus committed May 28, 2023
1 parent a37afce commit 5cd2d54
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
64 changes: 64 additions & 0 deletions api/application.go
Expand Up @@ -358,6 +358,70 @@ func (a *ApplicationAPI) UploadApplicationImage(ctx *gin.Context) {
})
}

// RemoveApplicationImage deletes an image of an application.
// swagger:operation DELETE /application/{id}/image application removeAppImage
//
// Deletes an image of an application.
//
// ---
// consumes: [application/json]
// produces: [application/json]
// parameters:
// - name: id
// in: path
// description: the application id
// required: true
// type: integer
// format: int64
// security: [clientTokenAuthorizationHeader: [], clientTokenHeader: [], clientTokenQuery: [], basicAuth: []]
// responses:
// 200:
// description: Ok
// 400:
// description: Bad Request
// schema:
// $ref: "#/definitions/Error"
// 401:
// description: Unauthorized
// schema:
// $ref: "#/definitions/Error"
// 403:
// description: Forbidden
// schema:
// $ref: "#/definitions/Error"
// 404:
// description: Not Found
// schema:
// $ref: "#/definitions/Error"
// 500:
// description: Server Error
// schema:
// $ref: "#/definitions/Error"
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
}
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
}

image := app.Image
app.Image = ""
if success := successOrAbort(ctx, 500, a.DB.UpdateApplication(app)); !success {
return
}
os.Remove(a.ImageDir + image)
ctx.JSON(200, withResolvedImage(app))
} else {
ctx.AbortWithError(404, fmt.Errorf("app with id %d doesn't exists", id))
}
})
}

func withResolvedImage(app *model.Application) *model.Application {
if app.Image == "" {
app.Image = "static/defaultapp.png"
Expand Down
41 changes: 41 additions & 0 deletions api/application_test.go
Expand Up @@ -426,6 +426,47 @@ func (s *ApplicationSuite) Test_UploadAppImage_expectNotFound() {
assert.Equal(s.T(), 404, s.recorder.Code)
}

func (s *ApplicationSuite) Test_RemoveAppImage_expectNotFound() {
s.db.User(5)

test.WithUser(s.ctx, 5)
s.ctx.Request = httptest.NewRequest("DELETE", "/irrelevant", nil)
s.ctx.Params = gin.Params{{Key: "id", Value: "4"}}

s.a.RemoveApplicationImage(s.ctx)

assert.Equal(s.T(), 404, s.recorder.Code)
}

func (s *ApplicationSuite) Test_RemoveAppImage_noCustomizedImage() {
s.db.User(5).App(1)

test.WithUser(s.ctx, 5)
s.ctx.Request = httptest.NewRequest("DELETE", "/irrelevant", nil)
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
s.a.RemoveApplicationImage(s.ctx)

assert.Equal(s.T(), 400, s.recorder.Code)
}

func (s *ApplicationSuite) Test_RemoveAppImage_expectSuccess() {
s.db.User(5)

imageFile := "existing.png"
s.db.CreateApplication(&model.Application{UserID: 5, ID: 1, Image: imageFile})
fakeImage(s.T(), imageFile)

test.WithUser(s.ctx, 5)
s.ctx.Request = httptest.NewRequest("DELETE", "/irrelevant", nil)
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
s.a.RemoveApplicationImage(s.ctx)

_, err := os.Stat(imageFile)
assert.True(s.T(), os.IsNotExist(err))

assert.Equal(s.T(), 200, s.recorder.Code)
}

func (s *ApplicationSuite) Test_UploadAppImage_WithSaveError_expectServerError() {
s.db.User(5).App(1)

Expand Down
72 changes: 72 additions & 0 deletions docs/spec.json
Expand Up @@ -365,6 +365,78 @@
}
}
}
},
"delete": {
"security": [
{
"clientTokenAuthorizationHeader": []
},
{
"clientTokenHeader": []
},
{
"clientTokenQuery": []
},
{
"basicAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"application"
],
"summary": "Deletes an image of an application.",
"operationId": "removeAppImage",
"parameters": [
{
"type": "integer",
"format": "int64",
"description": "the application id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "Ok"
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/Error"
}
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/Error"
}
},
"403": {
"description": "Forbidden",
"schema": {
"$ref": "#/definitions/Error"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/Error"
}
},
"500": {
"description": "Server Error",
"schema": {
"$ref": "#/definitions/Error"
}
}
}
}
},
"/application/{id}/message": {
Expand Down
2 changes: 2 additions & 0 deletions router/router.go
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 5cd2d54

Please sign in to comment.