1) controller 패키지
2) BoardController(클래스)
3) model 패키지
4) BoardDto (클래스)
5) Repository
6) BoardRepository (인터페이스)
model.BoardDto
public class BoardDto {
private Long id;
private String name;
BoardDto
public interface BoardRepository {
BoardDto create(BoardDto dto);
BoardDto read(Long id);
Collection<BoardDto> readAll();
boolean update(Long id, BoardDto dto);
boolean delete(Long id);
}
BoardController
@RestController
@RequestMapping("board")
public class BoardController {
private static final Logger logger = LoggerFactory.getLogger(BoardController.class);
private final BoardRepository boardRepository;
public BoardController(
BoardRepository boardRepository
){
this.boardRepository=boardRepository;
}
@PostMapping//Entity는 더 다양한 http 응답을 돌려주기 가능
public ResponseEntity<BoardDto> createBoard(@RequestBody BoardDto dto){
return ResponseEntity.ok(boardRepository.create(dto));
}
@GetMapping("{id}")
public ResponseEntity<BoardDto> readBoard(
@PathVariable("id") Long id
){
BoardDto dto = boardRepository.read(id);
return ResponseEntity.ok(dto);
}
@GetMapping
public ResponseEntity<Collection<BoardDto>> readBoardAll(){
return ResponseEntity.ok(this.boardRepository.readAll());
}
@PutMapping("{id}")
public ResponseEntity<?> updateBoard(
@PathVariable("id") Long id, @RequestBody BoardDto dto
){
if(!boardRepository.update(id, dto)) return ResponseEntity.notFound().build();
return ResponseEntity.noContent().build();
}
@DeleteMapping("{id}")
public ResponseEntity<?> deleteBoard(
@PathVariable("id") Long id
){
if(!boardRepository.delete(id)) return ResponseEntity.notFound().build();
return ResponseEntity.noContent().build();
}
}
BoardRepository의 구현체 InmemoryBoardRepository 만들기
@Repository
public class InMemoryBoardRepository implements BoardRepository{
private Long lastIndex=0L;
//데이터들은 모두 (키, 값)의 1:1 구조로 되어있는 Entry
private final Map<Long, BoardDto> memory= new HashMap<>();
@Override
public BoardDto create(BoardDto dto) {
lastIndex++;
dto.setId(lastIndex);
memory.put(lastIndex, dto);
return null;
}
@Override
public BoardDto read(Long id) {
return memory.getOrDefault(id,null);
}
@Override
public Collection<BoardDto> readAll() {
return memory.values();
}
@Override
public boolean update(Long id, BoardDto dto) {
if(memory.containsKey(id)){
dto.setId(id);
memory.put(id,dto);
return true;
}
return false;
}
@Override
public boolean delete(Long id) {
if(memory.containsKey(id)){
memory.remove(id);
return true;
}
return false;
}
}
PostDto
public class PostDto {
private Long id;
private String title;
private String content;
private String writer;
private String password;
private Long boardId;
PostController
@RestController
@RequestMapping("board/{boardId}/post")
public class PostController {
private static Logger logger = LoggerFactory.getLogger(PostController.class);
public PostController(){
}
@PostMapping
public ResponseEntity<PostDto> createPost(
@PathVariable("boardId") Long boardId
){
return null;
}
@GetMapping("{postId}")
public ResponseEntity<PostDto> readPost(
@PathVariable("postId") Long postId
){
return null;
}
@GetMapping
public ResponseEntity<Collection<PostDto>> readPostAll(){
return null;
}
@PutMapping("{postId}")
public ResponseEntity<?> updatePost(
@PathVariable("boardId") Long boardId,
@PathVariable("postId") Long postId,
@RequestBody PostDto dto
){
return null;
}
@DeleteMapping("{postId}")
public ResponseEntity<?> deletePost(
@PathVariable("boardId") Long boardId,
@PathVariable("postId") Long postId,
@RequestParam("password") String password
//
){
return null;
}
PostRepository
public interface PostRepository {
PostDto create(Long boardId, PostDto dto);
PostDto read(Long boardId, PostDto dto);
Collection<PostDto> readAll (Long boardId);
boolean update(Long boardId, Long postId, PostDto dto);
boolean delete(Long boardId, Long postId, String password);
}
InMemoryPostRepository
@Repository
public class InMemoryPostRepository implements PostRepository {
private final BoardRepository boardRepository;
private Long lastIndex = 0L;
//데이터들은 모두 (키, 값)의 1:1 구조로 되어있는 Entry
private final Map<Long, PostDto> memory = new HashMap<>();
public InMemoryPostRepository(
//여기 오토와이어드 안 붙여도 되긴 함
@Autowired BoardRepository boardRepository){
this.boardRepository=boardRepository;
}
@Repository
public class InMemoryPostRepository implements PostRepository {
private final BoardRepository boardRepository;
private Long lastIndex = 0L;
//데이터들은 모두 (키, 값)의 1:1 구조로 되어있는 Entry
private final Map<Long, PostDto> memory = new HashMap<>();
public InMemoryPostRepository(
//여기 오토와이어드 안 붙여도 되긴 함
@Autowired BoardRepository boardRepository){
this.boardRepository=boardRepository;
}
@Override
public PostDto create(Long boardId, PostDto dto) {
BoardDto boardDto = this.boardRepository.read(boardId);
if (boardDto==null){
return null;
}
dto.setBoardId(boardId);//post는 board가 있는지 여부도 점검하는 부분이 추가적으로 필요!
lastIndex++;
dto.setId(lastIndex);
memory.put(lastIndex, dto);
return dto;
}
@Override
public PostDto read(Long boardId, Long postId) {
PostDto postDto=memory.getOrDefault(postId, null);
if(postDto == null){
return null;
}
else if(!Objects.equals(postDto.getBoardId(),boardId)){
return null;
}
return postDto;
}
@Override
public Collection<PostDto> readAll(Long boardId) {
if (boardRepository.read(boardId)==null) return null;
//얘는 아무것도 없으면 null
//memory : Map<Long, PostDto>
Collection<PostDto> postList=new ArrayList<>();
memory.forEach((postId, postDto)-> {
if (Objects.equals(postDto.getBoardId(), boardId))
postList.add(postDto);
}
);
return postList;//얘는 아무것도 없어도 empty
}
@Override
public boolean update(Long boardId, Long postId, PostDto dto) {
PostDto targetPost = memory.getOrDefault(postId, null);
if (targetPost == null) {
return false;
} else if (!Objects.equals(targetPost.getBoardId(), boardId)) {
return false;
} else if (!Objects.equals(targetPost.getPassword(), dto.getPassword())) {
return false;
}
targetPost.setTitle(
dto.getTitle() == null ? targetPost.getTitle() : dto.getTitle()
);
targetPost.setContent(
dto.getContent() == null ? targetPost.getContent() : dto.getContent()
);
return true;
}
@Override
public boolean delete(Long boardId, Long postId, String password) {
PostDto targetPost = memory.getOrDefault(postId, null);
if (targetPost == null) {
return false;
} else if (!Objects.equals(targetPost.getBoardId(), boardId)) {
return false;
} else if (!Objects.equals(targetPost.getPassword(), password)) {
return false;
}
memory.remove(postId);
return true;
}
}
PostDto
public PostDto passwordMasked(){
return new PostDto(
this.id,
this.title,
this.content,
this.writer,
"*****",
this.boardId
);
}
PostController
@RestController
@RequestMapping("board/{boardId}/post")
public class PostController {
private static Logger logger = LoggerFactory.getLogger(PostController.class);
private final PostRepository postRepository;
public PostController(PostRepository postRepository){
this.postRepository = postRepository; //레포지토리 구현 후 추가
}
@PostMapping
public ResponseEntity<PostDto> createPost(
@PathVariable("boardId") Long boardId,
@RequestBody PostDto dto
){
PostDto result = this.postRepository.create(boardId,dto);
return ResponseEntity.ok(result.passwordMasked());
//비밀번호 안보임 처리후 객체 돌려주는 메소드
}
@GetMapping("{postId}")
public ResponseEntity<PostDto> readPost(
@PathVariable("boardId") Long boardId,
@PathVariable("postId") Long postId
){
PostDto postDto = this.postRepository.read(boardId, postId);
if(postDto==null) return ResponseEntity.notFound().build();
else return ResponseEntity.ok(postDto.passwordMasked());
}
@GetMapping
public ResponseEntity<Collection<PostDto>> readPostAll(
@PathVariable("boardId") Long boardId
){
Collection<PostDto> postList = this.postRepository.readAll(boardId);
if (postList==null) return ResponseEntity.notFound().build();
else return ResponseEntity.ok(postList);
}
@PutMapping("{postId}")
public ResponseEntity<?> updatePost(
@PathVariable("boardId") Long boardId,
@PathVariable("postId") Long postId,
@RequestBody PostDto dto
){
if(!postRepository.update(boardId, postId, dto))
return ResponseEntity.notFound().build();
return ResponseEntity.noContent().build();
}
@DeleteMapping("{postId}")
public ResponseEntity<?> deletePost(
@PathVariable("boardId") Long boardId,
@PathVariable("postId") Long postId,
@RequestParam("password") String password
//
){
if(!postRepository.delete(boardId, postId, password))
return ResponseEntity.notFound().build();
return ResponseEntity.noContent().build();
}