-
Notifications
You must be signed in to change notification settings - Fork 1
Kanban thymeleaf #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
29cd05f
3d526ec
7dfd010
ca73c01
2879868
d8a12e5
05d025d
46c19ed
52d5d10
9bbe94d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,41 @@ | ||
| buildscript { | ||
| ext { | ||
| springBootVersion = '2.0.4.RELEASE' | ||
| } | ||
| repositories { | ||
| mavenCentral() | ||
| } | ||
| dependencies { | ||
| classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") | ||
| } | ||
| ext { | ||
| springBootVersion = '2.0.3.RELEASE' | ||
| } | ||
| repositories { | ||
| mavenCentral() | ||
| } | ||
| dependencies { | ||
| classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") | ||
| } | ||
| } | ||
|
|
||
| apply plugin: 'java' | ||
| apply plugin: 'eclipse' | ||
| apply plugin: 'idea' | ||
| apply plugin: 'org.springframework.boot' | ||
| apply plugin: 'io.spring.dependency-management' | ||
|
|
||
| group = 'ru.otus.spring.hw' | ||
| version = '0.0.1-SNAPSHOT' | ||
| sourceCompatibility = 1.8 | ||
| targetCompatibility = 1.8 | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| mavenCentral() | ||
| } | ||
|
|
||
| bootJar { | ||
| baseName = 'ru.otus.spring.hw' | ||
| version = '0.1.0' | ||
| } | ||
|
|
||
| dependencies { | ||
| compile('org.springframework.boot:spring-boot-starter-thymeleaf') | ||
| compile('org.springframework.boot:spring-boot-starter-web') | ||
| compile('org.springframework.boot:spring-boot-starter-data') | ||
| compile("org.springframework.boot:spring-boot-starter-data-jpa") | ||
| runtime('com.h2database:h2') | ||
| testCompile('org.springframework.boot:spring-boot-starter-test') | ||
| compile('org.springframework.boot:spring-boot-parent:2.0.1.RELEASE') | ||
| compile('org.springframework.boot:spring-boot-starter-thymeleaf') | ||
| compile('org.springframework.boot:spring-boot-starter-web') | ||
| compile('org.springframework.boot:spring-boot-starter-thymeleaf') | ||
| // compile('org.springframework.boot:spring-boot-starter-data:') | ||
| compile('org.springframework.boot:spring-boot-starter-data-jpa:1.5.8.RELEASE') | ||
| runtime('com.h2database:h2') | ||
| testCompile('org.springframework.boot:spring-boot-starter-test') | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,15 @@ | ||
| package ru.otus.spring.hw.kanban; | ||
|
|
||
| //import org.h2.tools.Console; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| import org.springframework.context.ConfigurableApplicationContext; | ||
| import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
| import ru.otus.spring.hw.kanban.domain.Board; | ||
| import ru.otus.spring.hw.kanban.repository.BoardRepository; | ||
|
|
||
| import java.awt.print.Book; | ||
|
|
||
|
|
||
|
|
||
| @SpringBootApplication | ||
| @EnableJpaRepositories(basePackages = {"ru.otus.spring.hw.kanban.repository"}) | ||
| public class KanbanApplication { | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| //SpringApplication.run(KanbanApplication.class, args); | ||
|
|
||
| // Console.main(args); | ||
| ConfigurableApplicationContext ctx = SpringApplication.run(KanbanApplication.class, args); | ||
| BoardRepository boardRepository = ctx.getBean(BoardRepository.class); | ||
|
|
||
| Board board = new Board("new board"); | ||
|
|
||
| boardRepository.save(board); | ||
|
|
||
|
|
||
| System.out.println(board.getId()); | ||
| } | ||
| public static void main(String[] args) { | ||
| SpringApplication.run(KanbanApplication.class, args); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,98 @@ | ||
| package ru.otus.spring.hw.kanban.controllers; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.ui.Model; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import ru.otus.spring.hw.kanban.dto.BoardDTO; | ||
| import ru.otus.spring.hw.kanban.dto.StageDTO; | ||
| import ru.otus.spring.hw.kanban.service.BoardService; | ||
| import ru.otus.spring.hw.kanban.service.StageService; | ||
| import ru.otus.spring.hw.kanban.service.TaskService; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Controller | ||
| public class BoardController { | ||
|
|
||
|
|
||
| BoardService boardService; | ||
| StageService stageService; | ||
| TaskService taskService; | ||
|
|
||
| @Autowired | ||
| public BoardController(BoardService boardService, StageService stageService, TaskService taskService) { | ||
| this.boardService = boardService; | ||
| this.stageService = stageService; | ||
| this.taskService = taskService; | ||
| } | ||
|
|
||
|
|
||
| @GetMapping({"/boards", "/"}) | ||
| public String listPage(Model model) { | ||
| List<BoardDTO> boards = boardService.findAll(); | ||
| model.addAttribute("boards", boards); | ||
| return "boards-list"; | ||
| } | ||
|
|
||
| @PostMapping("/boards/") | ||
| public String createBoard(Model model, BoardDTO boardDTO) { | ||
| boardService.create(boardDTO); | ||
| List<BoardDTO> boards = boardService.findAll(); | ||
| model.addAttribute("boards", boards); | ||
| return "boards-list"; | ||
| } | ||
|
|
||
|
|
||
| @GetMapping("/boards/edit") | ||
| public String getBoard(@RequestParam("id") int id, Model model) { | ||
| BoardDTO board = boardService.find(id); | ||
|
|
||
| board.setStages(stageService.findByBoard(id)); | ||
| model.addAttribute("board", board); | ||
| return "board-edit"; | ||
| } | ||
|
|
||
|
|
||
| @PutMapping("/boards/{id}") | ||
| BoardDTO updateBoard(@RequestBody BoardDTO boardDTO, @PathVariable Long id) { | ||
| return boardService.update(boardDTO); | ||
| } | ||
|
|
||
| @RequestMapping(value = {"/boards/edit"}, method = RequestMethod.POST) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. МОжно просто |
||
| public String saveBoard(@RequestParam("id") int id, Model model, // | ||
| @ModelAttribute BoardDTO boardToUpdate) { | ||
| boardService.update(boardToUpdate); | ||
| model.addAttribute("boards", boardService.findAll()); | ||
| return "redirect:/boards"; | ||
| } | ||
|
|
||
| @RequestMapping(value = {"/boards/delete/"}, method = RequestMethod.POST) | ||
| public String deleteBoard(@RequestParam("id") int id, Model model) { | ||
| try { | ||
| boardService.deleteById(id); | ||
| } catch (Exception ex) { | ||
| model.addAttribute("error", ex.toString()); | ||
| return "redirect:/error"; | ||
| } | ||
| model.addAttribute("boards", boardService.findAll()); | ||
| return "redirect:/boards"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Клёво! Но я бы последни две строчки прямо в try бы и оставил. |
||
| } | ||
|
|
||
| @GetMapping("/boards/") | ||
| public String showBoard(@RequestParam("id") int id, Model model) { | ||
| BoardDTO board = boardService.find(id); | ||
| List<StageDTO> stages = stageService.findByBoard(id); | ||
| stages.forEach(stageDTO -> stageDTO.setTasks(taskService.findAllByStage(stageDTO.id))); | ||
| board.setStages(stages); | ||
| model.addAttribute("board", board); | ||
| return "board-show"; | ||
| } | ||
|
|
||
|
|
||
| @DeleteMapping("/boards/{id}") | ||
| void deleteBoard(@PathVariable int id) { | ||
| boardService.deleteById(id); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package ru.otus.spring.hw.kanban.controllers; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.ResponseBody; | ||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||
| import ru.otus.spring.hw.kanban.exceptions.BoardNotFoundException; | ||
| import ru.otus.spring.hw.kanban.exceptions.StageNotFoundException; | ||
| import ru.otus.spring.hw.kanban.exceptions.TaskNotFoundException; | ||
|
|
||
| @ControllerAdvice | ||
| public class NotFoundAdvice { | ||
|
|
||
| @ResponseBody | ||
| @ExceptionHandler(BoardNotFoundException.class) | ||
| @ResponseStatus(HttpStatus.NOT_FOUND) | ||
| String boardNotFoundHandler(BoardNotFoundException ex) { | ||
| return ex.getMessage(); | ||
| } | ||
|
|
||
| @ResponseBody | ||
| @ExceptionHandler(StageNotFoundException.class) | ||
| @ResponseStatus(HttpStatus.NOT_FOUND) | ||
| String stageNotFoundHandler(StageNotFoundException ex) { | ||
| return ex.getMessage(); | ||
| } @ResponseBody | ||
|
|
||
| @ExceptionHandler(TaskNotFoundException.class) | ||
| @ResponseStatus(HttpStatus.NOT_FOUND) | ||
| String taskNotFoundHandler(TaskNotFoundException ex) { | ||
| return ex.getMessage(); | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. КРуто! А для REST контроллеров, это кстати можно сделать с помощью аннотации |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package ru.otus.spring.hw.kanban.controllers; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.ui.Model; | ||
| import org.springframework.web.bind.annotation.ModelAttribute; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestMethod; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import ru.otus.spring.hw.kanban.dto.BoardDTO; | ||
| import ru.otus.spring.hw.kanban.dto.StageDTO; | ||
| import ru.otus.spring.hw.kanban.service.BoardService; | ||
| import ru.otus.spring.hw.kanban.service.StageService; | ||
|
|
||
| @Controller | ||
| public class StageController { | ||
| StageService stageService; | ||
| BoardService boardService; | ||
|
|
||
| @Autowired | ||
| public StageController(StageService stageService, BoardService boardService) { | ||
| this.stageService = stageService; | ||
| this.boardService = boardService; | ||
| } | ||
|
|
||
|
|
||
| @RequestMapping(value = {"/boards/stages/update"}, method = RequestMethod.POST) | ||
| public String saveStage(@RequestParam("boardId") int boardId, @RequestParam("stageId") int stageId, Model model, // | ||
| @ModelAttribute StageDTO stage) { | ||
|
|
||
| StageDTO stageDTO = stageService.find(stageId); | ||
| stageDTO.setName(stage.getName()); | ||
| stageService.update(stageDTO); | ||
|
|
||
| BoardDTO board = boardService.find(boardId); | ||
| board.setStages(stageService.findByBoard(boardId)); | ||
| model.addAttribute("board", board); | ||
| return "board-edit"; | ||
| } | ||
|
|
||
|
|
||
| @RequestMapping(value = {"/boards/stages"}, method = RequestMethod.GET) | ||
| public String editStage2(@RequestParam("boardId") int boardId, @RequestParam("stageId") int stageId, | ||
| Model model) { | ||
|
|
||
| StageDTO stageDTO = stageService.find(stageId); | ||
| BoardDTO boardDTO = boardService.find(boardId); | ||
|
|
||
| model.addAttribute("stage", stageDTO); | ||
| model.addAttribute("board", boardDTO); | ||
| return "stage-edit"; | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| @RequestMapping(value = {"/boards/stages/"}, method = RequestMethod.POST) | ||
| public String createStage(@RequestParam("boardId") int boardId, Model model, StageDTO stageDTO) { | ||
|
|
||
| stageService.create(stageDTO); | ||
| BoardDTO board = boardService.find(boardId); | ||
| board.setStages(stageService.findByBoard(boardId)); | ||
| model.addAttribute("board", board); | ||
| return "board-edit"; | ||
| } | ||
|
|
||
| @RequestMapping(value = {"/boards/stages/delete"}, method = RequestMethod.POST) | ||
| public String deleteStage(@RequestParam("boardId") int boardId, @RequestParam("stageId") int stageId, Model model, // | ||
| @ModelAttribute StageDTO stageDTO) { | ||
|
|
||
| stageService.deleteById(stageId); | ||
| BoardDTO board = boardService.find(boardId); | ||
| board.setStages(stageService.findByBoard(boardId)); | ||
|
|
||
| model.addAttribute("board", board); | ||
| return "board-edit"; | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Добавьте уже *.iml в .gitignore и удалите из репозитория)