forked from EnjoyTripKorea/EnjoyTripBackend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d703ee8
commit c1c141a
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/controller/BoardController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import static org.springframework.http.HttpStatus.CREATED; | ||
|
||
@RestController | ||
@RequestMapping("/api/boards") | ||
@RequiredArgsConstructor | ||
public class BoardController { | ||
|
||
private final BoardService boardService; | ||
|
||
@PostMapping("/") | ||
public ResponseEntity<String> save(@Valid @RequestBody BoardDto boardDto){ | ||
Long id = boardService.save(boardDto); | ||
return ResponseEntity.status(CREATED).body("게시글 등록 완료"); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<board> findById(@PathVariable("id") Long id) { | ||
Board board = boardService.findById(id); | ||
return ResponseEntity.status(OK).body(board); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<List<board>> findAll() { | ||
List<board> boardList = boardService.findAll(); | ||
return ResponseEntity.status(OK).body(boardList); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ResponseEntity<Long> update(@Valid @RequestBody BoardDto boardDto) { | ||
Long boardId = boardService.update(boardDto); | ||
return ResponseEntity.status(OK).body(boardId); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> delete(@PathVariable("id") Long id) { | ||
boardService.delete(id); | ||
return ResponseEntity.status(NO_CONTENT).build(); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/board/BoardDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/repository/BoardRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.EnjoyTripBackend.repository; | ||
|
||
|
||
import com.example.EnjoyTripBackend.domain.Member; | ||
import org.apache.ibatis.annotations.Mapper; | ||
|
||
import java.util.Optional; | ||
|
||
@Mapper | ||
public interface BoardRepository { | ||
Long save(Board board); | ||
Board findById(Long id) | ||
List<Board> findAll(); | ||
Long update(BoardDto boardDto); | ||
void delete(Long id); | ||
} | ||
|
56 changes: 56 additions & 0 deletions
56
EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/BoardService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.example.EnjoyTripBackend.service; | ||
|
||
import com.example.EnjoyTripBackend.domain.Board; | ||
import com.example.EnjoyTripBackend.dto.Board.BoardDto; | ||
import com.example.EnjoyTripBackend.repository.BoardRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
|
||
@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 Board findById(Long id) { | ||
return boardRepository.findById(id) | ||
.orElseThrow(() -> new EnjoyTripException(ErrorCode.BOARD_NOT_FOUND)); | ||
} | ||
|
||
public List<Board> findAll(){ | ||
return boardRepository.findAll(); | ||
// return boardRepository.findAll().stream().map(this::Board).collect(Collector.toList()); | ||
} | ||
|
||
@Transactional | ||
public Long update(BoardDto boardDto){ | ||
Optional<Board> optionalBoard = boardRepository.findById(boardDto.getId()); | ||
if (!optionalBoard.isPresent()) { | ||
throw new EnjoyTripException( | ||
"Board is not present in the database"); | ||
} | ||
return boardRepository.update(boardDto); | ||
} | ||
|
||
public void delete(Long id){ | ||
Optional<Board> optionalBoard = boardRepository.findById(id); | ||
if (!optionalBoard.isPresent()) { | ||
throw new EnjoyTripException( | ||
"Board is not present in the database"); | ||
} | ||
boardRepository.delete(id); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
EnjoyTripBackend/src/main/resources/mapper/board-query.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
|
||
<mapper namespace="com.example.EnjoyTripBackend.repository.BoardRepository"> | ||
<insert id="save" parameterType="com.example.EnjoyTripBackend.domain.Board"> | ||
insert | ||
into board | ||
values (#{id}, #{memberId}, #{title}, #{content}, now(), now()) | ||
</insert> | ||
|
||
<select id="findById" parameterType="long" resultType="com.example.EnjoyTripBackend.domain.Board"> | ||
select * | ||
from board | ||
where board_id = #{id} | ||
</select> | ||
|
||
<select id="findAll" resultType="com.example.EnjoyTripBackend.domain.Board"> | ||
select * | ||
from board | ||
</select> | ||
|
||
<update id="update" parameterType="com.example.EnjoyTripBackend.dto.BoardDto" resultType="long"> | ||
update board | ||
set member_id = #{memberId}, | ||
title = #{title}, | ||
content = #{content}, | ||
modifiedDate = #{modifiedDate} | ||
where id = #{id} | ||
</update> | ||
|
||
<delete id="delete"> | ||
delete from board where id = #{id} | ||
</delete> | ||
</mapper> |