Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Kanban.iml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<excludeFolder url="file://$MODULE_DIR$/dist" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавьте уже *.iml в .gitignore и удалите из репозитория)

41 changes: 24 additions & 17 deletions kanban-server/build.gradle
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

МОжно просто @PostMapping

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";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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();
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

КРуто! А для REST контроллеров, это кстати можно сделать с помощью аннотации @ResponseStatus

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";
}

}
Loading