From a798f04b518657fd569beda52d163195c1970cb8 Mon Sep 17 00:00:00 2001 From: Vadim Vlasenko <33458204+vadimvlasenko@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:13:03 +0300 Subject: [PATCH 1/3] Add DeleteSponsorHandler.java --- .../test/handlers/DeleteSponsorHandler.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/java/com/test/handlers/DeleteSponsorHandler.java diff --git a/src/main/java/com/test/handlers/DeleteSponsorHandler.java b/src/main/java/com/test/handlers/DeleteSponsorHandler.java new file mode 100644 index 0000000..3520f20 --- /dev/null +++ b/src/main/java/com/test/handlers/DeleteSponsorHandler.java @@ -0,0 +1,42 @@ +package com.test.handlers; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; +import com.test.config.CoreComponent; +import com.test.config.DaggerCoreComponent; +import com.test.service.SponsorService; +import com.test.utils.HandlerUtils; +import lombok.extern.slf4j.Slf4j; + +import javax.inject.Inject; + +@Slf4j +public class DeleteSponsorHandler extends ApiHandler { + @Inject + SponsorService sponsorService; + protected final CoreComponent coreComponent; + + public DeleteSponsorHandler() { + coreComponent = DaggerCoreComponent.builder().build(); + coreComponent.inject(this); + } + + @Override + public APIGatewayProxyResponseEvent handle(APIGatewayProxyRequestEvent input, Context context) { + try { + String sponsorId = input.getPathParameters().get("id"); + if (sponsorId == null || sponsorId.trim().isEmpty()) { + return HandlerUtils.buildBadRequestError("Sponsor ID is required"); + } + + sponsorService.deleteSponsorById(sponsorId); + + return new APIGatewayProxyResponseEvent() + .withStatusCode(204); + } catch (Exception e) { + log.error("Error deleting sponsor: {}", e.getMessage(), e); + return HandlerUtils.buildServerError("Error deleting sponsor"); + } + } +} \ No newline at end of file From e44cf1a788a714759d2a337a2ae568dc80a48891 Mon Sep 17 00:00:00 2001 From: Vadim Vlasenko <33458204+vadimvlasenko@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:13:12 +0300 Subject: [PATCH 2/3] Update SponsorService to include deleteSponsorById method --- src/main/java/com/test/service/SponsorService.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/test/service/SponsorService.java b/src/main/java/com/test/service/SponsorService.java index bbef27a..3be5b3d 100644 --- a/src/main/java/com/test/service/SponsorService.java +++ b/src/main/java/com/test/service/SponsorService.java @@ -29,4 +29,9 @@ public SponsorModel getSponsorById(String id) { return new SponsorModel("1", "test"); } } -} + + public void deleteSponsorById(String id) { + // Your logic to delete the sponsor from the database or storage + // For example, if using DynamoDB: dynamoDBMapper.delete(...) + } +} \ No newline at end of file From 5071b9ad525d14582fbc4107269136f7b6b90548 Mon Sep 17 00:00:00 2001 From: Vadim Vlasenko <33458204+vadimvlasenko@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:14:26 +0300 Subject: [PATCH 3/3] Add DeleteSponsorFunction to template.yaml --- template.yaml | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/template.yaml b/template.yaml index 83fe9f7..7f825dc 100644 --- a/template.yaml +++ b/template.yaml @@ -22,7 +22,6 @@ Resources: Name: !Sub 'test-sponsors-api-${Stage}' StageName: Prod EndpointConfiguration: REGIONAL - # This turns on X-Ray tracing. For more details, see https://meetup.atlassian.net/wiki/spaces/CP/pages/854098408/DIY+Edge+API+Operations#DIYEdgeAPIOperations-Instrumentation TracingEnabled: true MinimumCompressionSize: 0 MethodSettings: @@ -49,6 +48,12 @@ Resources: passthroughBehavior: "when_no_match" httpMethod: "POST" type: "aws_proxy" + delete: + x-amazon-apigateway-integration: + uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${DeleteSponsorFunction.Arn}/invocations" + passthroughBehavior: "when_no_match" + httpMethod: "POST" + type: "aws_proxy" x-amazon-apigateway-request-validators: ValidateBody: validateRequestParameters: false @@ -98,7 +103,7 @@ Resources: Resource: "*" GetSponsorsFunction: - Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Type: AWS::Serverless::Function Properties: FunctionName: !Sub 'test-java-lambda-get-sponsors-${Stage}' CodeUri: build/libs/SponsorService-all.jar @@ -106,12 +111,12 @@ Resources: Runtime: java11 Role: !GetAtt LambdaFunctionRole.Arn MemorySize: 512 - Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object + Environment: Variables: PARAM1: VALUE Events: MyApi: - Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Type: Api Properties: RestApiId: !Ref MyApi Path: /sponsors @@ -125,7 +130,7 @@ Resources: RetentionInDays: 60 FindSponsorFunction: - Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Type: AWS::Serverless::Function Properties: FunctionName: !Sub 'test-java-lambda-find-sponsor-${Stage}' CodeUri: build/libs/SponsorService-all.jar @@ -133,12 +138,12 @@ Resources: Runtime: java11 MemorySize: 512 Role: !GetAtt LambdaFunctionRole.Arn - Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object + Environment: Variables: PARAM1: VALUE Events: MyApi: - Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Type: Api Properties: RestApiId: !Ref MyApi Path: /sponsors/{id} @@ -151,6 +156,22 @@ Resources: LogGroupName: !Sub /aws/lambda/${FindSponsorFunction} RetentionInDays: 60 + DeleteSponsorFunction: + Type: AWS::Serverless::Function + Properties: + FunctionName: !Sub 'test-java-lambda-delete-sponsor-${Stage}' + CodeUri: build/libs/SponsorService-all.jar + Handler: com.test.handlers.DeleteSponsorHandler::handleRequest + Runtime: java11 + Role: !GetAtt LambdaFunctionRole.Arn + Events: + DeleteApi: + Type: Api + Properties: + RestApiId: !Ref MyApi + Path: /sponsors/{id} + Method: delete + Outputs: ApiId: Description: the unique ID associated with the API Gateway @@ -163,3 +184,6 @@ Outputs: FindSponsorFunction: Description: "Find sponsor Lambda Function ARN" Value: !GetAtt FindSponsorFunction.Arn + DeleteSponsorFunction: + Description: "Delete sponsor Lambda Function ARN" + Value: !GetAtt DeleteSponsorFunction.Arn \ No newline at end of file