From d9575bc9f86b18345aa421aa626415d58836e9e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=A1=EC=9E=AC=EC=9B=90?= Date: Tue, 14 May 2024 16:33:54 +0900 Subject: [PATCH] =?UTF-8?q?feat(#2)=20:=20=EA=B2=8C=EC=8B=9C=ED=8C=90=20?= =?UTF-8?q?=EA=B8=B0=EB=B3=B8=20CRUD=20=EA=B8=B0=EB=8A=A5=20=EA=B0=9C?= =?UTF-8?q?=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BoardController.java | 53 +++++++++++++++++++ .../EnjoyTripBackend/dto/Board/BoardDto.java | 21 ++++++++ .../repository/BoardRepository.java | 18 +++++++ .../service/BoardService.java | 48 +++++++++++++++++ .../main/resources/config/mybatis-config.xml | 1 + 5 files changed, 141 insertions(+) create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/controller/BoardController.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/Board/BoardDto.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/repository/BoardRepository.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/BoardService.java diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/controller/BoardController.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/controller/BoardController.java new file mode 100644 index 0000000..33ce544 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/controller/BoardController.java @@ -0,0 +1,53 @@ +package com.example.EnjoyTripBackend.controller; + +import com.example.EnjoyTripBackend.dto.Board.BoardDto; +import com.example.EnjoyTripBackend.service.BoardService; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +import static org.springframework.http.HttpStatus.OK; +import static org.springframework.http.HttpStatus.CREATED; +import static org.springframework.http.HttpStatus.NO_CONTENT; + +@RestController +@RequestMapping("/api/boards") +@RequiredArgsConstructor +public class BoardController { + + private final BoardService boardService; + + @PostMapping("/") + public ResponseEntity save(@Valid @RequestBody BoardDto boardDto){ + Long id = boardService.save(boardDto); + return ResponseEntity.status(CREATED).body("게시글 등록 완료"); + } + + @GetMapping("/{id}") + public ResponseEntity findById(@PathVariable("id") Long id) { + BoardDto boardDto = boardService.findById(id); + return ResponseEntity.status(OK).body(boardDto); + } + + @GetMapping("/") + public ResponseEntity> findAll() { + List boardList = boardService.findAll(); + return ResponseEntity.status(OK).body(boardList); + } + + @PutMapping("/{id}") + public ResponseEntity update(@PathVariable("id") Long id, @Valid @RequestBody BoardDto boardDto) { + + boardService.update(id, boardDto); + return ResponseEntity.status(OK).body("게시글 수정 완료"); + } + + @DeleteMapping("/{id}") + public ResponseEntity delete(@PathVariable("id") Long id) { + boardService.delete(id); + return ResponseEntity.status(NO_CONTENT).build(); + } +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/Board/BoardDto.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/Board/BoardDto.java new file mode 100644 index 0000000..c7f1806 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/Board/BoardDto.java @@ -0,0 +1,21 @@ +package com.example.EnjoyTripBackend.dto.Board; + +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; +import lombok.NoArgsConstructor; +import lombok.Getter; + +@Getter +@NoArgsConstructor +public class BoardDto { + + @NotNull + private Long memberId; + + @NotEmpty + private String title; + + @NotEmpty + private String content; + +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/repository/BoardRepository.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/repository/BoardRepository.java new file mode 100644 index 0000000..f0ace8d --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/repository/BoardRepository.java @@ -0,0 +1,18 @@ +package com.example.EnjoyTripBackend.repository; + +import com.example.EnjoyTripBackend.domain.Board; +import com.example.EnjoyTripBackend.dto.Board.BoardDto; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; +import java.util.Optional; + +@Mapper +public interface BoardRepository { + Long save(Board board); + Optional findById(Long id); + List findAll(); + void update(@Param("id") Long id, @Param("boardDto") BoardDto boardDto); + void delete(Long id); +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/BoardService.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/BoardService.java new file mode 100644 index 0000000..e4d2c92 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/BoardService.java @@ -0,0 +1,48 @@ +package com.example.EnjoyTripBackend.service; + +import com.example.EnjoyTripBackend.domain.Board; +import com.example.EnjoyTripBackend.dto.Board.BoardDto; +import com.example.EnjoyTripBackend.exception.EnjoyTripException; +import com.example.EnjoyTripBackend.exception.ErrorCode; +import com.example.EnjoyTripBackend.repository.BoardRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class BoardService { + + private final BoardRepository boardRepository; + + @Transactional + public Long save(BoardDto boardDto) { + Board board = Board.builder() + .memberId(boardDto.getMemberId()) + .title(boardDto.getTitle()) + .content(boardDto.getContent()) + .build(); + return boardRepository.save(board); + } + + public BoardDto findById(Long id) { + return boardRepository.findById(id).orElseThrow(()-> new EnjoyTripException(ErrorCode.BOARD_NOT_FOUND)); + } + + public List findAll(){ + return boardRepository.findAll(); + } + + @Transactional + public void update(Long id, BoardDto boardDto) { + boardRepository.update(id, boardDto); + } + + @Transactional + public void delete(Long id){ + boardRepository.delete(id); + } +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/resources/config/mybatis-config.xml b/EnjoyTripBackend/src/main/resources/config/mybatis-config.xml index f2ff12c..8aeffbf 100644 --- a/EnjoyTripBackend/src/main/resources/config/mybatis-config.xml +++ b/EnjoyTripBackend/src/main/resources/config/mybatis-config.xml @@ -9,5 +9,6 @@ + \ No newline at end of file