Skip to content

Commit

Permalink
addMyList
Browse files Browse the repository at this point in the history
  • Loading branch information
Johyunik committed Nov 29, 2023
1 parent 1486a5c commit 3c80fe9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/weatherfit/board/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.weatherfit.board;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getMessage());
}
}

14 changes: 12 additions & 2 deletions src/main/java/com/weatherfit/board/controller/BoardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public List<BoardListResponseDTO> listBoards(@RequestParam(required = false) Str
return list;
}

// 내가 쓴 게시글 조회
@GetMapping("/myList")
public List<BoardListResponseDTO> myListBoards(@RequestHeader("decodedToken") String nickName) throws UnsupportedEncodingException {
String decodedNickname = new String(Base64.getDecoder().decode(nickName), "UTF-8");
return boardService.findNickname(decodedNickname);
}

// 게시글 상세 조회
@GetMapping("/detail/{boardId}")
public BoardDetailResponseDTO detailBoard(@PathVariable int boardId) {
Expand Down Expand Up @@ -129,13 +136,15 @@ public String insertBoard(@RequestHeader("decodedToken") String nickName, @Reque
@PatchMapping(value = "/edit/{boardId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public boolean patchBoard(
@RequestHeader("decodedToken") String nickName,
@PathVariable int boardId,
@RequestPart("board") String boardJson,
@RequestPart(value = "images", required = false) MultipartFile[] images) {
ObjectMapper objectMapper = new ObjectMapper();

try {
BoardUpdateDTO boardUpdateDTO = objectMapper.readValue(boardJson, BoardUpdateDTO.class);
boardService.patchBoard(boardId, boardUpdateDTO, images);
boardService.patchBoard(boardId, boardUpdateDTO, images, nickName);
return true;
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
Expand All @@ -146,8 +155,9 @@ public boolean patchBoard(
@DeleteMapping("/delete/{boardId}")
@ResponseBody
public void deleteBoard(
@RequestHeader("decodedToken") String nickName,
@PathVariable int boardId) {
boardService.deleteBoard(boardId);
boardService.deleteBoard(boardId, nickName);
}

// 게시글 검색
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface BoardRepository extends JpaRepository<BoardEntity, Integer>, Bo

BoardEntity findById(int id);

List<BoardEntity> findByNickName(String nickName);

List<BoardEntity> findByCategoryInAndHashTagIn(List<String> categories, List<String> hashTags);
List<BoardEntity> findByHashTagIn(List<String> hashTags);
List<BoardEntity> findByCategoryIn(List<String> categories);
Expand Down
40 changes: 35 additions & 5 deletions src/main/java/com/weatherfit/board/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public List<BoardListResponseDTO> findAll() {
.category(board.getCategory())
.temperature(board.getTemperature());

if(!board.getImages().isEmpty()) {
if (!board.getImages().isEmpty()) {
builder.images(board.entityToDTO(board.getImages().get(0)));
}

Expand All @@ -70,7 +70,7 @@ public List<BoardListResponseDTO> findDate() {
.category(board.getCategory())
.temperature(board.getTemperature());

if(!board.getImages().isEmpty()) {
if (!board.getImages().isEmpty()) {
builder.images(board.entityToDTO(board.getImages().get(0)));
}

Expand All @@ -93,7 +93,7 @@ public List<BoardListResponseDTO> findLike() {
.category(board.getCategory())
.temperature(board.getTemperature());

if(!board.getImages().isEmpty()) {
if (!board.getImages().isEmpty()) {
builder.images(board.entityToDTO(board.getImages().get(0)));
}

Expand All @@ -103,6 +103,29 @@ public List<BoardListResponseDTO> findLike() {
return dtoList;
}

// 내가 쓴 게시글 조회
public List<BoardListResponseDTO> findNickname(String nickName) {
List<BoardEntity> entities = boardRepository.findByNickName(nickName);
List<BoardListResponseDTO> dtoList = new ArrayList<>();

for (BoardEntity board : entities) {
BoardListResponseDTO.BoardListResponseDTOBuilder builder = BoardListResponseDTO.builder()
.boardId(board.getBoardId())
.nickName(board.getNickName())
.likeCount(likeService.countLikes(board.getBoardId()))
.hashTag(board.getHashTag())
.category(board.getCategory())
.temperature(board.getTemperature());

if (!board.getImages().isEmpty()) {
builder.images(board.entityToDTO(board.getImages().get(0)));
}
dtoList.add(builder.build());
}
return dtoList;
}


// 게시글 상세 조회
public BoardEntity getBoardById(int boardId) {
return boardRepository.findById(boardId);
Expand All @@ -125,11 +148,14 @@ public BoardEntity insertBoard(BoardEntity board) {
}

// 게시글 수정
public void patchBoard(int boardId, BoardUpdateDTO boardUpdateDTO, MultipartFile[] images) {
public void patchBoard(int boardId, BoardUpdateDTO boardUpdateDTO, MultipartFile[] images, String nickName) {
Optional<BoardEntity> optionalBoard = Optional.ofNullable(boardRepository.findById(boardId));

BoardEntity originalBoard = optionalBoard.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id=" + boardId));

if (!originalBoard.getNickName().equals(nickName)) {
throw new IllegalArgumentException("게시글 수정 권한이 없습니다.");
}

List<Integer> imageIdsToDelete = boardUpdateDTO.getImageIdsToDelete();
if (imageIdsToDelete != null && !imageIdsToDelete.isEmpty()) {
Expand Down Expand Up @@ -175,11 +201,15 @@ public void patchBoard(int boardId, BoardUpdateDTO boardUpdateDTO, MultipartFile
}

// 게시글 삭제
public void deleteBoard(int boardId) {
public void deleteBoard(int boardId, String nickName) {
Optional<BoardEntity> optionalBoard = Optional.ofNullable(boardRepository.findById(boardId));

BoardEntity originalBoard = optionalBoard.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id=" + boardId));

if (!originalBoard.getNickName().equals(nickName)) {
throw new IllegalArgumentException("게시글 삭제 권한이 없습니다.");
}

String afterJoiendString = originalBoard.getTemperature() + "/" + String.join("/", originalBoard.getCategory());
String afterJoiendString2 = String.join("/", originalBoard.getHashTag());

Expand Down

0 comments on commit 3c80fe9

Please sign in to comment.