Skip to content

Commit

Permalink
#189 - Add support for "soft" delete
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Nov 28, 2023
1 parent 7bf10ba commit d3fb038
Show file tree
Hide file tree
Showing 34 changed files with 1,173 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void testDeleteDocument() {
service.saveActions(siteId, documentId, Arrays.asList(action0));

// when
documentService.deleteDocument(siteId, documentId);
documentService.deleteDocument(siteId, documentId, false);

// then
List<Action> actions = service.getActions(siteId, documentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public interface DynamoDbService {
* Delete Items.
*
* @param attrs {@link Collection} {@link Map} {@link AttributeValue}
* @return boolena
* @return boolean
*/
boolean deleteItems(Collection<Map<String, AttributeValue>> attrs);

Expand Down Expand Up @@ -92,6 +92,15 @@ public interface DynamoDbService {
List<Map<String, AttributeValue>> getBatch(BatchGetConfig config,
List<Map<String, AttributeValue>> keys);

/**
* Move Records.
*
* @param attrs {@link Collection} {@link Map}
* @param func {@link MoveAttributeFunction}
* @return boolean
*/
boolean moveItems(Collection<Map<String, AttributeValue>> attrs, MoveAttributeFunction func);

/**
* Put DynamoDb Record.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static com.formkiq.aws.dynamodb.DbKeys.PK;
import static com.formkiq.aws.dynamodb.DbKeys.SK;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -42,6 +43,7 @@
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
import software.amazon.awssdk.services.dynamodb.model.PutRequest;
import software.amazon.awssdk.services.dynamodb.model.QueryRequest;
import software.amazon.awssdk.services.dynamodb.model.QueryResponse;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;
Expand Down Expand Up @@ -170,6 +172,30 @@ private String getKey(final Map<String, AttributeValue> attr) {
return attr.get(PK).s() + "#" + attr.get(SK).s();
}

@Override
public boolean moveItems(final Collection<Map<String, AttributeValue>> attrs,
final MoveAttributeFunction func) {

List<WriteRequest> writes = new ArrayList<>();

for (Map<String, AttributeValue> attr : attrs) {

Map<String, AttributeValue> newAttr = func.transform(attr);

Map<String, AttributeValue> key = Map.of(PK, attr.get(PK), SK, attr.get(SK));
WriteRequest del =
WriteRequest.builder().deleteRequest(DeleteRequest.builder().key(key).build()).build();
writes.add(del);

WriteRequest add =
WriteRequest.builder().putRequest(PutRequest.builder().item(newAttr).build()).build();
writes.add(add);
}

WriteRequestBuilder builder = new WriteRequestBuilder().append(this.tableName, writes);
return builder.batchWriteItem(this.dbClient);
}

@Override
public void putItem(final Map<String, AttributeValue> attributes) {
putItem(this.tableName, attributes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* MIT License
*
* Copyright (c) 2018 - 2020 FormKiQ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.aws.dynamodb;

import java.util.Map;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

/**
* {@link DynamoDbService} Move Attributes Function.
*/
public interface MoveAttributeFunction {

/**
* Transform {@link Map} {@link AttributeValue}.
*
* @param attr {@link Map} {@link AttributeValue}
* @return {@link Map} {@link AttributeValue}
*/
Map<String, AttributeValue> transform(Map<String, AttributeValue> attr);

}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public boolean batchWriteItem(final DynamoDbClient dbClient) {
BatchWriteItemRequest batch =
BatchWriteItemRequest.builder().requestItems(Map.of(e.getKey(), writelist)).build();
dbClient.batchWriteItem(batch);

write = true;
}
}
Expand Down
46 changes: 46 additions & 0 deletions docs/openapi/openapi-iam.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
- PENDING
- RUNNING
- SKIPPED
- $ref: '#/components/parameters/documentSearchDeleted'
- name: date
in: query
description: Fetch documents inserted on a certain date (yyyy-MM-dd)
Expand Down Expand Up @@ -562,6 +563,11 @@
parameters:
- $ref: '#/components/parameters/siteIdParam'
- $ref: '#/components/parameters/documentIdParam'
- name: softDelete
in: query
description: Whether to soft delete document
schema:
type: boolean
responses:
"200":
$ref: '#/components/responses/200Cors'
Expand Down Expand Up @@ -1033,6 +1039,34 @@
- ApiAuthorization: []
x-amazon-apigateway-integration:
$ref: '#/components/x-amazon-apigateway-integrations/lambdaApi200'
/documents/{documentId}/restore:
put:
operationId: SetDocumentRestore
summary: Restore soft deleted document
parameters:
- $ref: '#/components/parameters/siteIdParam'
- $ref: '#/components/parameters/documentIdParam'
description: Restores a soft deleted document
tags:
- Documents
responses:
"200":
description: 200 OK
headers:
Access-Control-Allow-Origin:
$ref: '#/components/headers/AccessControlAllowOrigin'
Access-Control-Allow-Methods:
$ref: '#/components/headers/AccessControlAllowMethods'
Access-Control-Allow-Headers:
$ref: '#/components/headers/AccessControlAllowHeaders'
content:
application/json:
schema:
$ref: '#/components/schemas/SetDocumentRestoreResponse'
security:
- ApiAuthorization: []
x-amazon-apigateway-integration:
$ref: '#/components/x-amazon-apigateway-integrations/lambdaApi200'
/documents/compress:
post:
operationId: CompressDocuments
Expand Down Expand Up @@ -3353,6 +3387,12 @@
required: false
schema:
type: string
documentSearchDeleted:
name: deleted
in: query
description: Fetch soft deleted documents
schema:
type: boolean
wsParam:
name: ws
in: query
Expand Down Expand Up @@ -4194,6 +4234,12 @@
- READ
- WRITE
- DELETE
SetDocumentRestoreResponse:
type: object
properties:
message:
type: string
description: Document Document restore message
AddFolderShareResponse:
type: object
properties:
Expand Down
46 changes: 46 additions & 0 deletions docs/openapi/openapi-jwt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
- PENDING
- RUNNING
- SKIPPED
- $ref: '#/components/parameters/documentSearchDeleted'
- name: date
in: query
description: Fetch documents inserted on a certain date (yyyy-MM-dd)
Expand Down Expand Up @@ -562,6 +563,11 @@
parameters:
- $ref: '#/components/parameters/siteIdParam'
- $ref: '#/components/parameters/documentIdParam'
- name: softDelete
in: query
description: Whether to soft delete document
schema:
type: boolean
responses:
"200":
$ref: '#/components/responses/200Cors'
Expand Down Expand Up @@ -1033,6 +1039,34 @@
- ApiAuthorization: []
x-amazon-apigateway-integration:
$ref: '#/components/x-amazon-apigateway-integrations/lambdaApi200'
/documents/{documentId}/restore:
put:
operationId: SetDocumentRestore
summary: Restore soft deleted document
parameters:
- $ref: '#/components/parameters/siteIdParam'
- $ref: '#/components/parameters/documentIdParam'
description: Restores a soft deleted document
tags:
- Documents
responses:
"200":
description: 200 OK
headers:
Access-Control-Allow-Origin:
$ref: '#/components/headers/AccessControlAllowOrigin'
Access-Control-Allow-Methods:
$ref: '#/components/headers/AccessControlAllowMethods'
Access-Control-Allow-Headers:
$ref: '#/components/headers/AccessControlAllowHeaders'
content:
application/json:
schema:
$ref: '#/components/schemas/SetDocumentRestoreResponse'
security:
- ApiAuthorization: []
x-amazon-apigateway-integration:
$ref: '#/components/x-amazon-apigateway-integrations/lambdaApi200'
/documents/compress:
post:
operationId: CompressDocuments
Expand Down Expand Up @@ -3353,6 +3387,12 @@
required: false
schema:
type: string
documentSearchDeleted:
name: deleted
in: query
description: Fetch soft deleted documents
schema:
type: boolean
wsParam:
name: ws
in: query
Expand Down Expand Up @@ -4194,6 +4234,12 @@
- READ
- WRITE
- DELETE
SetDocumentRestoreResponse:
type: object
properties:
message:
type: string
description: Document Document restore message
AddFolderShareResponse:
type: object
properties:
Expand Down
46 changes: 46 additions & 0 deletions docs/openapi/openapi-key.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
- PENDING
- RUNNING
- SKIPPED
- $ref: '#/components/parameters/documentSearchDeleted'
- name: date
in: query
description: Fetch documents inserted on a certain date (yyyy-MM-dd)
Expand Down Expand Up @@ -562,6 +563,11 @@
parameters:
- $ref: '#/components/parameters/siteIdParam'
- $ref: '#/components/parameters/documentIdParam'
- name: softDelete
in: query
description: Whether to soft delete document
schema:
type: boolean
responses:
"200":
$ref: '#/components/responses/200Cors'
Expand Down Expand Up @@ -1033,6 +1039,34 @@
- ApiAuthorization: []
x-amazon-apigateway-integration:
$ref: '#/components/x-amazon-apigateway-integrations/lambdaApi200'
/documents/{documentId}/restore:
put:
operationId: SetDocumentRestore
summary: Restore soft deleted document
parameters:
- $ref: '#/components/parameters/siteIdParam'
- $ref: '#/components/parameters/documentIdParam'
description: Restores a soft deleted document
tags:
- Documents
responses:
"200":
description: 200 OK
headers:
Access-Control-Allow-Origin:
$ref: '#/components/headers/AccessControlAllowOrigin'
Access-Control-Allow-Methods:
$ref: '#/components/headers/AccessControlAllowMethods'
Access-Control-Allow-Headers:
$ref: '#/components/headers/AccessControlAllowHeaders'
content:
application/json:
schema:
$ref: '#/components/schemas/SetDocumentRestoreResponse'
security:
- ApiAuthorization: []
x-amazon-apigateway-integration:
$ref: '#/components/x-amazon-apigateway-integrations/lambdaApi200'
/documents/compress:
post:
operationId: CompressDocuments
Expand Down Expand Up @@ -3353,6 +3387,12 @@
required: false
schema:
type: string
documentSearchDeleted:
name: deleted
in: query
description: Fetch soft deleted documents
schema:
type: boolean
wsParam:
name: ws
in: query
Expand Down Expand Up @@ -4194,6 +4234,12 @@
- READ
- WRITE
- DELETE
SetDocumentRestoreResponse:
type: object
properties:
message:
type: string
description: Document Document restore message
AddFolderShareResponse:
type: object
properties:
Expand Down
Binary file modified dynamodb-documents/documentsTableSchema.numbers
Binary file not shown.
Loading

0 comments on commit d3fb038

Please sign in to comment.