Skip to content

Commit

Permalink
Merge pull request #418 from seokho-son/master
Browse files Browse the repository at this point in the history
Add tumblebug obj delete feature and update obj api
  • Loading branch information
seokho-son authored Mar 18, 2021
2 parents 0a0d0ac + ffc6b7c commit 21a4126
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 34 deletions.
69 changes: 61 additions & 8 deletions src/api/rest/server/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ type ObjectList struct {
Object []string `json:"object"`
}

// func RestGetObjectList is a rest api wrapper for GetObjectList.
// RestGetObjectList godoc
// func RestGetObjects is a rest api wrapper for GetObjectList.
// RestGetObjects godoc
// @Summary List all objects for a given key
// @Description List all objects for a given key
// @Tags Admin
Expand All @@ -122,8 +122,8 @@ type ObjectList struct {
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /objectList [get]
func RestGetObjectList(c echo.Context) error {
// @Router /objects [get]
func RestGetObjects(c echo.Context) error {
parentKey := c.QueryParam("key")
fmt.Printf("[Get Tumblebug Object List] with Key: %s \n", parentKey)

Expand All @@ -137,8 +137,8 @@ func RestGetObjectList(c echo.Context) error {
return c.JSON(http.StatusOK, &objectList)
}

// func RestGetObjectValue is a rest api wrapper for GetObjectValue.
// RestGetObjectValue godoc
// func RestGetObject is a rest api wrapper for GetObject.
// RestGetObject godoc
// @Summary Get value of an object
// @Description Get value of an object
// @Tags Admin
Expand All @@ -148,8 +148,8 @@ func RestGetObjectList(c echo.Context) error {
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /objectValue [get]
func RestGetObjectValue(c echo.Context) error {
// @Router /object [get]
func RestGetObject(c echo.Context) error {
parentKey := c.QueryParam("key")
fmt.Printf("[Get Tumblebug Object Value] with Key: %s \n", parentKey)

Expand All @@ -162,4 +162,57 @@ func RestGetObjectValue(c echo.Context) error {
json.Unmarshal([]byte(content), &contentJSON)

return c.JSON(http.StatusOK, &contentJSON)
}

// func RestDeleteObject is a rest api wrapper for DeleteObject.
// RestDeleteObject godoc
// @Summary Delete an object
// @Description Delete an object
// @Tags Admin
// @Accept json
// @Produce json
// @Param key query string true "delete object value by key"
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /object [delete]
func RestDeleteObject(c echo.Context) error {
parentKey := c.QueryParam("key")
fmt.Printf("[Delete Tumblebug Object] with Key: %s \n", parentKey)

content, err := common.GetObjectValue(parentKey)
if err != nil || content == "" {
return SendMessage(c, http.StatusOK, "Cannot find [" + parentKey+ "] object")
}

err = common.DeleteObject(parentKey)
if err != nil {
return SendMessage(c, http.StatusOK, "Cannot delete [" + parentKey+ "] object")
}

return SendMessage(c, http.StatusOK, "The object has been deleted")
}

// func RestDeleteObjects is a rest api wrapper for DeleteObjects.
// RestDeleteObjects godoc
// @Summary Delete child objects along with the given object
// @Description Delete child objects along with the given object
// @Tags Admin
// @Accept json
// @Produce json
// @Param key query string true "Delete child objects based on the given key string"
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /objects [delete]
func RestDeleteObjects(c echo.Context) error {
parentKey := c.QueryParam("key")
fmt.Printf("[Delete Tumblebug child Objects] with Key: %s \n", parentKey)

err := common.DeleteObjects(parentKey)
if err != nil {
return SendMessage(c, http.StatusOK, "Cannot delete objects")
}

return SendMessage(c, http.StatusOK, "Objects have been deleted")
}
6 changes: 4 additions & 2 deletions src/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ func ApiServer() {
e.GET("/tumblebug/config", rest_common.RestGetAllConfig)
e.DELETE("/tumblebug/config", rest_common.RestDelAllConfig)

e.GET("/tumblebug/objectList", rest_common.RestGetObjectList)
e.GET("/tumblebug/objectValue", rest_common.RestGetObjectValue)
e.GET("/tumblebug/object", rest_common.RestGetObject)
e.GET("/tumblebug/objects", rest_common.RestGetObjects)
e.DELETE("/tumblebug/object", rest_common.RestDeleteObject)
e.DELETE("/tumblebug/objects", rest_common.RestDeleteObjects)

g := e.Group("/tumblebug/ns", common.NsValidation())

Expand Down
24 changes: 24 additions & 0 deletions src/core/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,3 +725,27 @@ func GetObjectValue(key string) (string, error) {
}
return keyValue.Value, nil
}

// func DeleteObject delete the object.
func DeleteObject(key string) error {

err := CBStore.Delete(key)
if err != nil {
CBLog.Error(err)
return err
}
return nil
}

// func DeleteObjects delete objects.
func DeleteObjects(key string) error {
keyValue, _ := CBStore.GetList(key, true)
for _, v := range keyValue {
err := CBStore.Delete(v.Key)
if err != nil {
CBLog.Error(err)
return err
}
}
return nil
}
100 changes: 92 additions & 8 deletions src/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3098,9 +3098,9 @@ var doc = `{
}
}
},
"/objectList": {
"/object": {
"get": {
"description": "List all objects for a given key",
"description": "Get value of an object",
"consumes": [
"application/json"
],
Expand All @@ -3110,11 +3110,53 @@ var doc = `{
"tags": [
"Admin"
],
"summary": "List all objects for a given key",
"summary": "Get value of an object",
"parameters": [
{
"type": "string",
"description": "retrieve objects by key",
"description": "get object value by key",
"name": "key",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
}
}
},
"delete": {
"description": "Delete an object",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Admin"
],
"summary": "Delete an object",
"parameters": [
{
"type": "string",
"description": "delete object value by key",
"name": "key",
"in": "query",
"required": true
Expand Down Expand Up @@ -3142,9 +3184,9 @@ var doc = `{
}
}
},
"/objectValue": {
"/objects": {
"get": {
"description": "Get value of an object",
"description": "List all objects for a given key",
"consumes": [
"application/json"
],
Expand All @@ -3154,11 +3196,53 @@ var doc = `{
"tags": [
"Admin"
],
"summary": "Get value of an object",
"summary": "List all objects for a given key",
"parameters": [
{
"type": "string",
"description": "get object value by key",
"description": "retrieve objects by key",
"name": "key",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/common.SimpleMsg"
}
}
}
},
"delete": {
"description": "Delete child objects along with the given object",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Admin"
],
"summary": "Delete child objects along with the given object",
"parameters": [
{
"type": "string",
"description": "Delete child objects based on the given key string",
"name": "key",
"in": "query",
"required": true
Expand Down
Loading

0 comments on commit 21a4126

Please sign in to comment.