Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/com/test/handlers/DeleteSponsorHandler.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
7 changes: 6 additions & 1 deletion src/main/java/com/test/service/SponsorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(...)
}
}
38 changes: 31 additions & 7 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -98,20 +103,20 @@ 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
Handler: com.test.handlers.GetSponsorsHandler::handleRequest
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
Expand All @@ -125,20 +130,20 @@ 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
Handler: com.test.handlers.FindSponsorHandler::handleRequest
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}
Expand All @@ -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
Expand All @@ -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