-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskController.java
More file actions
76 lines (70 loc) · 2.79 KB
/
TaskController.java
File metadata and controls
76 lines (70 loc) · 2.79 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
75
76
package com.study.springjrproj.controller;
import com.study.springjrproj.dto.CreateTaskDto;
import com.study.springjrproj.dto.TaskDto;
import com.study.springjrproj.exception.TaskNotFoundException;
import com.study.springjrproj.service.TaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
@Controller
@RequiredArgsConstructor
@RequestMapping("/tasks")
@Slf4j
public class TaskController {
private final TaskService taskService;
@RequestMapping(method = RequestMethod.GET)
public String getAllTasks(Model model,
@RequestParam(defaultValue = "0", required = false) int page,
@RequestParam(defaultValue = "5", required = false) int size) {
Pageable pageable = PageRequest.of(page, size);
var tasksPerPage = taskService.findAllBy(pageable);
model.addAttribute("tasksPerPage", tasksPerPage);
log.trace("Added attribute - tasksPerPage");
return "tasks";
}
@RequestMapping(method = RequestMethod.POST, value = "/edit/{id}")
public String edit(@PathVariable Integer id, @ModelAttribute TaskDto taskToUpdate) {
taskService.update(taskToUpdate);
log.info("Updated task");
return "redirect:/tasks";
}
@RequestMapping(method = RequestMethod.GET, value = "/edit/{id}")
public String editForm(@PathVariable Integer id, Model model) {
try {
var taskDto = taskService.findById(id);
model.addAttribute("task", taskDto.get());
log.trace("Added attribute - task");
return "edit";
} catch (TaskNotFoundException e) {
log.warn("Task not found");
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
}
@PostMapping("/delete/{id}")
public String delete(@PathVariable Integer id) {
try {
taskService.deleteById(id);
log.info("Task was deleted");
return "redirect:/tasks";
} catch (TaskNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
}
@GetMapping("/create")
public String createForm(Model model) {
model.addAttribute("task", new TaskDto());
log.trace("Added attribute - task");
return "create";
}
@PostMapping("/create")
public String createForm(@ModelAttribute("task") CreateTaskDto taskDto) {
taskService.save(taskDto);
return "redirect:/tasks";
}
}