Skip to content
Merged
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
45 changes: 23 additions & 22 deletions app/src/main/java/tech/buildrun/lambda/HandlerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

import com.fasterxml.jackson.databind.ObjectMapper;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.*;

import java.util.*;

public class HandlerClient implements
RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
RequestHandler<APIGatewayV2HTTPEvent, APIGatewayProxyResponseEvent> {

private static final String TABLE_NAME = "tc-identification-table";

Expand All @@ -20,38 +21,40 @@ public class HandlerClient implements

@Override
public APIGatewayProxyResponseEvent handleRequest(
APIGatewayProxyRequestEvent request,
APIGatewayV2HTTPEvent request,
Context context) {

try {
String method = request.getHttpMethod();
String path = request.getPath();
String path = request.getRawPath();
String method = request.getRequestContext()
.getHttp()
.getMethod();

context.getLogger().log("METHOD=" + method);
context.getLogger().log("PATH=" + path);

// POST /clientes
if ("POST".equals(method) && "/clientes".equals(path)) {
if ("POST".equalsIgnoreCase(method) && "/clientes".equals(path)) {
return criarCliente(request);
}

// GET /clientes/{document}
if ("GET".equals(method) && path.startsWith("/clientes/")) {
if ("GET".equalsIgnoreCase(method) && path.startsWith("/clientes/")) {
return consultarCliente(path);
}

return response(404, Map.of("message", "Endpoint não encontrado"));

} catch (Exception e) {
e.printStackTrace();
context.getLogger().log("ERRO: " + e.getMessage());
return response(500, Map.of("message", "Erro interno"));
}
}

// ===================== CRIAR CLIENTE =====================
/* ===================== CRIAR CLIENTE ===================== */

private APIGatewayProxyResponseEvent criarCliente(
APIGatewayProxyRequestEvent request) throws Exception {
APIGatewayV2HTTPEvent request) throws Exception {

if (request.getBody() == null || request.getBody().isBlank()) {
return response(400, Map.of("message", "Body obrigatório"));
Expand All @@ -61,16 +64,15 @@ private APIGatewayProxyResponseEvent criarCliente(
mapper.readValue(request.getBody(), Map.class);

String document = body.get("document");
String name = body.get("name");
String email = body.get("email");
String name = body.get("name");
String email = body.get("email");

if (document == null || name == null || email == null) {
return response(400, Map.of(
"message", "document, name e email são obrigatórios"
));
}

// 🔍 Verifica se já existe (regra do SQL 23505)
if (clienteExistePorDocumento(document)) {
return response(409, Map.of("message", "Cliente já existe"));
}
Expand All @@ -93,11 +95,12 @@ private APIGatewayProxyResponseEvent criarCliente(
));
}

// ===================== CONSULTAR CLIENTE =====================
/* ===================== CONSULTAR CLIENTE ===================== */

private APIGatewayProxyResponseEvent consultarCliente(String path)
throws Exception {

// /clientes/123456
String document = path.substring("/clientes/".length());

QueryRequest query = QueryRequest.builder()
Expand All @@ -110,13 +113,13 @@ private APIGatewayProxyResponseEvent consultarCliente(String path)
.limit(1)
.build();

QueryResponse response = dynamo.query(query);
QueryResponse result = dynamo.query(query);

if (response.count() == 0) {
if (result.count() == 0) {
return response(404, Map.of("message", "Cliente não encontrado"));
}

Map<String, AttributeValue> item = response.items().get(0);
Map<String, AttributeValue> item = result.items().get(0);

return response(200, Map.of(
"id", item.get("id").s(),
Expand All @@ -126,7 +129,7 @@ private APIGatewayProxyResponseEvent consultarCliente(String path)
));
}

// ===================== UTIL =====================
/* ===================== UTIL ===================== */

private boolean clienteExistePorDocumento(String document) {

Expand All @@ -150,10 +153,8 @@ private APIGatewayProxyResponseEvent response(int status, Object body) {
.withHeaders(Map.of(
"Content-Type", "application/json",
"Access-Control-Allow-Origin", "*",
"Access-Control-Allow-Headers",
"Content-Type,Authorization",
"Access-Control-Allow-Methods",
"GET,POST,OPTIONS"
"Access-Control-Allow-Headers", "Content-Type,Authorization",
"Access-Control-Allow-Methods", "GET,POST,OPTIONS"
))
.withBody(mapper.writeValueAsString(body));
} catch (Exception e) {
Expand Down