Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. @Slf4j
  2. @RestController
  3. @RequiredArgsConstructor
  4. @RequestMapping(path = "api/tasks")
  5. final class TasksController {
  6.  
  7.     private final StorageService storageService;
  8.     private final TasksService tasksService;
  9.     private final TasksConfig config;
  10.  
  11.     @PostConstruct
  12.     private void init() {
  13.         tasksService.addTask("Jarek", "DO APP", "Prepare application");
  14.         tasksService.addTask("Jarek", "LEARN", "Refresh multithreading knowledge. Learn about monads");
  15.         tasksService.addTask("Guest", "Test", "Test guest task");
  16.     }
  17.  
  18.     @GetMapping
  19.     public ResponseEntity getTasks(@RequestParam Optional<String> query) {
  20.         log.info("Fetching all tasks with filter: {}", query);
  21.         List<TaskResponse> tasks = query.map(tasksService::filterAllByQuery)
  22.                 .orElseGet(tasksService::fetchAll)
  23.                 .stream()
  24.                 .map(this::toTaskResponse)
  25.                 .collect(toList());
  26.         return ResponseEntity.ok(tasks);
  27.     }
  28.  
  29.     @GetMapping(path = "/{id}")
  30.     public ResponseEntity getTaskById(@PathVariable Long id) {
  31.         log.info("Fetching task with id: {}", id);
  32.         return ResponseEntity.ok(tasksService.fetchById(id));
  33.     }
  34.  
  35.     @PostMapping
  36.     public ResponseEntity addTask(@RequestBody CreateTaskRequest task) {
  37.         log.info("Storing new task {}", task);
  38.         tasksService.addTask(task.getAuthor(), task.getTitle(), task.getDescription());
  39.         return ResponseEntity.status(HttpStatus.CREATED).build();
  40.     }
  41.  
  42.     @DeleteMapping(path = "/{id}")
  43.     public ResponseEntity deleteTask(@PathVariable Long id) {
  44.         log.info("Deleting a task with id: {}", id);
  45.         tasksService.deleteTask(id);
  46.         return ResponseEntity.ok().build();
  47.     }
  48.  
  49.     @PutMapping(path = "/{id}")
  50.     public ResponseEntity<String> updateTask(@PathVariable Long id, @RequestBody UpdateTaskRequest request) {
  51.         log.info("Updating a task with id: {}", id);
  52.         tasksService.updateTask(id, request.getTitle(), request.getDescription());
  53.         return ResponseEntity.ok().build();
  54.     }
  55.  
  56.     @GetMapping(path = "/{id}/attachments/{filename}")
  57.     public ResponseEntity getAttachment(
  58.             @PathVariable Long id,
  59.             @PathVariable String filename,
  60.             HttpServletRequest request) throws IOException {
  61.         log.info("Fetching attachment: {}, from task with id: {}", filename, id);
  62.         Resource resource = storageService.loadFile(id, filename);
  63.         String mimeType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
  64.         if (mimeType == null) {
  65.             mimeType = "application/octet-stream";
  66.         }
  67.         return ResponseEntity.ok()
  68.                 .contentType(MediaType.parseMediaType(mimeType))
  69.                 .body(resource);
  70.     }
  71.  
  72.     @PostMapping(path = "/{id}/attachments")
  73.     public ResponseEntity addAttachment(@PathVariable Long id, @RequestParam("file") MultipartFile file) throws IOException {
  74.         log.info("Saving attachment: {}, to task with id: {}", file.getName(), id);
  75.         Task task = tasksService.fetchById(id);
  76.         task.getAttachments().add(file.getOriginalFilename());
  77.         storageService.saveFile(id, file);
  78.         return ResponseEntity.status(HttpStatus.CREATED).build();
  79.     }
  80.  
  81.     @GetMapping(path = "/hello")
  82.     public String hello() {
  83.         return config.getEndpointMessage();
  84.     }
  85.  
  86.     private TaskResponse toTaskResponse(Task task) {
  87.         return new TaskResponse(
  88.                 task.getId(),
  89.                 task.getTitle(),
  90.                 task.getAuthor(),
  91.                 task.getDescription(),
  92.                 task.getCreatedAt(),
  93.                 task.getAttachments());
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement