Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Slf4j
- @RestController
- @RequiredArgsConstructor
- @RequestMapping(path = "api/tasks")
- final class TasksController {
- private final StorageService storageService;
- private final TasksService tasksService;
- private final TasksConfig config;
- @PostConstruct
- private void init() {
- tasksService.addTask("Jarek", "DO APP", "Prepare application");
- tasksService.addTask("Jarek", "LEARN", "Refresh multithreading knowledge. Learn about monads");
- tasksService.addTask("Guest", "Test", "Test guest task");
- }
- @GetMapping
- public ResponseEntity getTasks(@RequestParam Optional<String> query) {
- log.info("Fetching all tasks with filter: {}", query);
- List<TaskResponse> tasks = query.map(tasksService::filterAllByQuery)
- .orElseGet(tasksService::fetchAll)
- .stream()
- .map(this::toTaskResponse)
- .collect(toList());
- return ResponseEntity.ok(tasks);
- }
- @GetMapping(path = "/{id}")
- public ResponseEntity getTaskById(@PathVariable Long id) {
- log.info("Fetching task with id: {}", id);
- return ResponseEntity.ok(tasksService.fetchById(id));
- }
- @PostMapping
- public ResponseEntity addTask(@RequestBody CreateTaskRequest task) {
- log.info("Storing new task {}", task);
- tasksService.addTask(task.getAuthor(), task.getTitle(), task.getDescription());
- return ResponseEntity.status(HttpStatus.CREATED).build();
- }
- @DeleteMapping(path = "/{id}")
- public ResponseEntity deleteTask(@PathVariable Long id) {
- log.info("Deleting a task with id: {}", id);
- tasksService.deleteTask(id);
- return ResponseEntity.ok().build();
- }
- @PutMapping(path = "/{id}")
- public ResponseEntity<String> updateTask(@PathVariable Long id, @RequestBody UpdateTaskRequest request) {
- log.info("Updating a task with id: {}", id);
- tasksService.updateTask(id, request.getTitle(), request.getDescription());
- return ResponseEntity.ok().build();
- }
- @GetMapping(path = "/{id}/attachments/{filename}")
- public ResponseEntity getAttachment(
- @PathVariable Long id,
- @PathVariable String filename,
- HttpServletRequest request) throws IOException {
- log.info("Fetching attachment: {}, from task with id: {}", filename, id);
- Resource resource = storageService.loadFile(id, filename);
- String mimeType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
- if (mimeType == null) {
- mimeType = "application/octet-stream";
- }
- return ResponseEntity.ok()
- .contentType(MediaType.parseMediaType(mimeType))
- .body(resource);
- }
- @PostMapping(path = "/{id}/attachments")
- public ResponseEntity addAttachment(@PathVariable Long id, @RequestParam("file") MultipartFile file) throws IOException {
- log.info("Saving attachment: {}, to task with id: {}", file.getName(), id);
- Task task = tasksService.fetchById(id);
- task.getAttachments().add(file.getOriginalFilename());
- storageService.saveFile(id, file);
- return ResponseEntity.status(HttpStatus.CREATED).build();
- }
- @GetMapping(path = "/hello")
- public String hello() {
- return config.getEndpointMessage();
- }
- private TaskResponse toTaskResponse(Task task) {
- return new TaskResponse(
- task.getId(),
- task.getTitle(),
- task.getAuthor(),
- task.getDescription(),
- task.getCreatedAt(),
- task.getAttachments());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement