Skip to content

Commit

Permalink
✨ feat(UrlData / Main): Adicionado funcionalidade para exportar para …
Browse files Browse the repository at this point in the history
…JSON
  • Loading branch information
marcos-rts committed Nov 26, 2024
1 parent 3f274f6 commit 3617138
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;
// Importa o ObjectMapper, usado para converter objetos Java em JSON e vice-versa.

import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectAclRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;

public class Main implements RequestHandler<Map<String, Object>, Map<String, String>> {
// Define a classe principal que implementa a interface RequestHandler.
// O tipo genérico `Map<String, Object>` representa o tipo da entrada da Lambda.
Expand All @@ -24,6 +29,8 @@ public class Main implements RequestHandler<Map<String, Object>, Map<String, Str
private final ObjectMapper objectMapper = new ObjectMapper();
// Cria uma instância do ObjectMapper para lidar com a conversão entre JSON e objetos Java.

private final S3Client s3Client = S3Client.builder().build();

@Override
public Map<String, String> handleRequest(Map<String, Object> input, Context context) {
// Sobrescreve o método `handleRequest`, onde a lógica da função Lambda é implementada.
Expand All @@ -46,13 +53,31 @@ public Map<String, String> handleRequest(Map<String, Object> input, Context cont
// Extrai valores específicos do mapa gerado a partir do JSON.
String originalUrl = bodyMap.get("origunalUrl");
// Obtém o valor associado à chave "origunalUrl" (nota: há um erro de digitação aqui).
String expirationTime = bodyMap.get("expirationMap");
// Obtém o valor associado à chave "expirationMap".
String expirationTime = bodyMap.get("expirationTime");
// Obtém o valor associado à chave "expirationTime".

long expirationTimeInSecond = Long.parseLong(expirationTime) * 3600;

// Gera um código curto aleatório usando UUID (Universal Unique Identifier).
// O código é composto pelos 8 primeiros caracteres do UUID gerado.
String shortUrlCode = UUID.randomUUID().toString().substring(0, 8);

UrlData urlData = new UrlData(originalUrl, expirationTimeInSecond);

try {
String urlDataJson = objectMapper.writeValueAsString(urlData);

PutObjectRequest request = PutObjectRequest.builder()
.bucket("mxt-url-shotener-storage")
.key(shortUrlCode + ".json")
.build();

s3Client.putObject(request, RequestBody.fromString(urlDataJson));

} catch (Exception exception) {
throw new RuntimeException("Erro saving URL data to S3" + exception.getMessage(), exception);
}

// Cria um mapa para armazenar a resposta que será retornada.
Map<String, String> response = new HashMap<>();
response.put("code", shortUrlCode);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.rocketseat.createurlshortner;

import lombok.AllArgsConstructor;
import lombok.Getter;
// import lombok.NoArgsConstructor;
import lombok.Setter;

// @NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class UrlData {
private String originalUrl;
private long expirationTime;


}

0 comments on commit 3617138

Please sign in to comment.