-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFeedbackHttpFunction.java
More file actions
74 lines (60 loc) · 2.83 KB
/
FeedbackHttpFunction.java
File metadata and controls
74 lines (60 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.feedback.functions;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.feedback.model.Feedback;
import com.feedback.service.FeedbackService;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import jakarta.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Optional;
public class FeedbackHttpFunction {
private static final Logger log = LoggerFactory.getLogger(NotificationHttpFunction.class);
@Inject
private FeedbackService feedbackService;
@Inject
private ObjectMapper mapper;
@FunctionName("FeedbackHttpFunction")
public HttpResponseMessage run(
@HttpTrigger(name = "req",
methods = {HttpMethod.POST},
route = "feedbacks",
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Processando novo feedback.");
log.info("Recebendo requisição HTTP para criar feedback.");
try {
String body = request.getBody().get();
if (body.isBlank()){
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body("Dados inválidos")
.build();
}
Feedback input = mapper.readValue(body, Feedback.class);
Feedback result = feedbackService.processar(input);
String jsonResponse = mapper.writeValueAsString(result);
log.info("Feedback processado com sucesso. Descrição: " + result.descricao);
return request.createResponseBuilder(HttpStatus.CREATED)
.header("Content-Type", "application/json")
.body(jsonResponse)
.build();
} catch (JsonMappingException | JsonParseException e) {
context.getLogger().severe("Erro de JSON: " + e.getMessage());
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body("Erro no formato dos dados")
.build();
} catch (Exception e) {
// MUITO IMPORTANTE: Logar o stacktrace real para diagnóstico
context.getLogger().severe("Erro inesperado: " + e.getClass().getName() + " - " + e.getMessage());
e.printStackTrace();
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Erro interno: " + e.getMessage())
.build();
}
}
}