Skip to content

Commit d47aead

Browse files
author
Ricardo Campos
committed
feat: basic template and structure for tasks handler
1 parent 362f2c3 commit d47aead

File tree

7 files changed

+137
-8
lines changed

7 files changed

+137
-8
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
Java REST API to play with AWS technologies
33

44
Tasks:
5-
- receive a task through HTTP Request
6-
- Get task description template from AWS S3
7-
- Replace and fill task template with values
8-
- Send to SQS to be sent through email as finished
5+
- List all tasks available through GET http request: http://localhost:8080/tasks/get-list
6+
- Receive a task name through POST HTTP request to be processed: http://localhost:8080/tasks/handle-task/CODE_REVIEW
7+
- Get task description from AWS S3 given the task name
8+
- Send to SQS to be sent through email as finished
99

1010
### Packaging
1111

src/main/java/br/com/campos/ricardo/awsjavarestapi/AwsJavaRestApiApplication.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
@SpringBootApplication
77
public class AwsJavaRestApiApplication {
88

9-
public static void main(String[] args) {
10-
SpringApplication.run(AwsJavaRestApiApplication.class, args);
11-
}
12-
9+
public static void main(String[] args) {
10+
SpringApplication.run(AwsJavaRestApiApplication.class, args);
11+
}
1312
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package br.com.campos.ricardo.awsjavarestapi;
2+
3+
public record Task(String name, String description) {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package br.com.campos.ricardo.awsjavarestapi;
2+
3+
import java.util.List;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.http.MediaType;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
@RequestMapping("/tasks")
14+
public class TaskController {
15+
16+
@Autowired private TaskService taskService;
17+
18+
@GetMapping(path = "/get-list", produces = MediaType.APPLICATION_JSON_VALUE)
19+
public List<String> getTasksList() {
20+
return taskService.getTaskList();
21+
}
22+
23+
@PostMapping(path = "/handle-task/{taskName}", produces = MediaType.APPLICATION_JSON_VALUE)
24+
public TaskResponse doTask(@PathVariable String taskName) {
25+
return taskService.handleTask(taskName);
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package br.com.campos.ricardo.awsjavarestapi;
2+
3+
public enum TaskEnum {
4+
CODE_REVIEW("CODE_REVIEW"),
5+
HELP_COLLEAGUES("HELP_COLLEAGUES");
6+
7+
private String code;
8+
9+
TaskEnum(String code) {
10+
this.code = code;
11+
}
12+
13+
public static TaskEnum getByCode(String code) {
14+
for (TaskEnum taskEnum : values()) {
15+
if (taskEnum.code.equals(code)) {
16+
return taskEnum;
17+
}
18+
}
19+
return null;
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package br.com.campos.ricardo.awsjavarestapi;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public class TaskResponse {
9+
10+
private String message;
11+
private Task task;
12+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package br.com.campos.ricardo.awsjavarestapi;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.web.server.ResponseStatusException;
10+
11+
@Slf4j
12+
@Service
13+
public class TaskService {
14+
15+
public List<String> getTaskList() {
16+
List<String> tasks = new ArrayList<>();
17+
Arrays.asList(TaskEnum.values())
18+
.forEach(
19+
task -> {
20+
tasks.add(task.toString());
21+
});
22+
return tasks;
23+
}
24+
25+
public TaskResponse handleTask(String taskName) {
26+
TaskEnum taskEnum = TaskEnum.getByCode(taskName);
27+
if (taskEnum == null) {
28+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid Task!");
29+
}
30+
31+
log.info("Handling task: {}", taskName);
32+
33+
// Get from AWS S3
34+
String template = getTemplate(taskEnum);
35+
if (template.isBlank()) {
36+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "No template for this Task!");
37+
}
38+
39+
// Send to SQS to be sent through email
40+
Task task = new Task(taskEnum.name(), template);
41+
sendToSqs(task);
42+
43+
TaskResponse response = new TaskResponse();
44+
response.setTask(task);
45+
response.setMessage("Task processed and sent to the Queue!");
46+
47+
return response;
48+
}
49+
50+
private String getTemplate(TaskEnum task) {
51+
log.info("Getting task template from AWS S3");
52+
53+
if (task.equals(TaskEnum.CODE_REVIEW)) {
54+
log.info("Template found for task CODE_REVIEW");
55+
return "Here 1";
56+
} else if (task.equals(TaskEnum.HELP_COLLEAGUES)) {
57+
log.info("Template found for task HELP_COLLEAGUES");
58+
return "Here 2";
59+
}
60+
log.warn("No template found for task :(");
61+
return "";
62+
}
63+
64+
private void sendToSqs(Task task) {
65+
log.info("Sending task to SQS {}", task.name());
66+
}
67+
}

0 commit comments

Comments
 (0)