-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathBoardController.java
More file actions
73 lines (63 loc) · 2.28 KB
/
BoardController.java
File metadata and controls
73 lines (63 loc) · 2.28 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
package com.example.demo.controller;
import com.example.demo.controller.dto.request.BoardCreateRequest;
import com.example.demo.controller.dto.request.BoardUpdateRequest;
import com.example.demo.controller.dto.response.BoardResponse;
import com.example.demo.exception.RestApiException;
import com.example.demo.exception.error.BoardErrorCode;
import com.example.demo.exception.error.CommonErrorCode;
import com.example.demo.service.ArticleService;
import com.example.demo.service.BoardService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/boards")
public class BoardController {
private final BoardService boardService;
private final ArticleService articleService;
public BoardController(BoardService boardService, ArticleService articleService) {
this.boardService = boardService;
this.articleService = articleService;
}
@GetMapping()
public List<BoardResponse> getBoards() {
return boardService.getBoards();
}
@GetMapping("/{id}")
public BoardResponse getBoard(
@PathVariable Long id
) {
if (boardService.getBoards().stream().noneMatch(res -> res.id().equals(id))) {
throw new RestApiException(CommonErrorCode.RESOURCE_NOT_FOUND);
}
return boardService.getBoardById(id);
}
@PostMapping()
public BoardResponse createBoard(
@RequestBody BoardCreateRequest request
) {
if (request.name() == null) {
throw new RestApiException(CommonErrorCode.NULL_PARAMETER);
}
return boardService.createBoard(request);
}
@PutMapping("/{id}")
public BoardResponse updateBoard(
@PathVariable Long id,
@RequestBody BoardUpdateRequest updateRequest
) {
return boardService.update(id, updateRequest);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteBoard(
@PathVariable Long id
) {
if (articleService.getArticles()
.stream()
.anyMatch(res -> res.boardId().equals(id))) {
throw new RestApiException(BoardErrorCode.ARTICLE_EXISTENCE);
}
boardService.deleteBoard(id);
return ResponseEntity.noContent().build();
}
}