Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature/27 post update] 게시글 수정 #24

Merged
merged 5 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/main/java/com/tomato/market/controller/BoardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -240,4 +242,39 @@ public ResponseDto<PostListResponseDto> getFavoriteList(String userId) {
.data(responseDto) // 어떤 형식으로 넘기지?
.build();
}


// 게시글 내용 변경
@PutMapping("/board/post") // null 값 그대로 전달
public ResponseDto<PostDto> updatePost(@RequestBody PostDto postDto) {
logger.info("BoardController.updatePost() is called");

// 게시글 수정
PostDto result = boardService.updatePost(postDto);

// 이미지 수정은 어떻게?

logger.info("BoardController.updatePost() : 게시글 수정 성공");
return ResponseDto.<PostDto>builder()
.status(HttpStatus.OK)
.message("게시글 수정 성공")
.data(postDto)
.build();
}

// 판매 상태 변경
// Patch?
@PatchMapping("board/post")
public ResponseDto<PostDto> updateStatus(@RequestBody PostDto postDto) {
logger.info("BoardController.updateStatus() is called");

PostDto result = boardService.updateStatus(postDto);

logger.info("BoardController.updateStatus() : 게시글 상태 수정 성공");
return ResponseDto.<PostDto>builder()
.status(HttpStatus.OK)
.message("게시글 상태 수정 성공")
.data(result)
.build();
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/tomato/market/data/dto/PostDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class PostDto {
public static PostEntity toPostEntity(PostDto postDto) {

return PostEntity.builder()
.postNum(postDto.getPostNum())
.userId(postDto.getUserId())
.location(postDto.getLocation())
.title(postDto.getTitle())
Expand All @@ -52,6 +53,7 @@ public static PostEntity toPostEntity(PostDto postDto) {
.price(postDto.getPrice())
.detailLocation(postDto.getDetailLocation())
.status(postDto.getStatus())
.createdAt(postDto.getCreateAt())
.boughtUserId(postDto.getBoughtUserId())
.build();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/tomato/market/data/entity/PostEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Entity
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/tomato/market/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public interface BoardService {
FavoriteDto getFavorite(String userId, Integer postNum);

List<FavoriteDto> getFavoriteList(String userId);

PostDto updatePost(PostDto postDto);

PostDto updateStatus(PostDto postDto);
}
36 changes: 36 additions & 0 deletions src/main/java/com/tomato/market/service/impl/BoardServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,40 @@ public List<FavoriteDto> getFavoriteList(String userId) {
}
return favoriteDtoList;
}

@Override
public PostDto updatePost(PostDto postDto) {
logger.info("BoardServiceImpl.updatePost() is called");

PostEntity postEntity = boardDao.save(PostDto.toPostEntity(postDto));
if (postEntity == null) {
logger.warn("BoardServiceImpl.updatePost() : 게시글 수정 실패");
throw new BoardException("게시글 수정에 실패했습니다.");
}

logger.info("BoardServiceImpl.updatePost() : 게시글 수정 성공");
return PostDto.toPostDto(postEntity);
}

@Override
public PostDto updateStatus(PostDto postDto) {
logger.info("BoardServiceImpl.updateStatus() is called");

PostEntity postEntity = boardDao.findPostByPostNum(postDto.getPostNum());
if (postEntity == null) {
logger.info("BoardServiceImpl.updateStatus() : 게시글 조회 실패");
throw new BoardException("게시글 조회에 실패했습니다.");
}

logger.info("BoardServiceImpl.updateStatus() : 게시글 조회 성공");
postEntity.setStatus(postDto.getStatus());
PostEntity result = boardDao.save(postEntity);
if (result == null) {
logger.warn("BoardServiceImpl.updateStatus() : 게시글 수정 실패");
throw new BoardException("게시글 상태 수정에 실패했습니다.");
}

logger.info("BoardServiceImpl.updateStatus() : 게시글 수정 성공");
return PostDto.toPostDto(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -515,4 +517,74 @@ void getFavoriteListFailure() throws Exception {

verify(boardService).getFavoriteList(userId);
}

@Test
@DisplayName("게시글_수정_성공")
void updatePostSuccess() throws Exception {
postDto.setContent("수정된 내용");
given(boardService.updatePost(any(PostDto.class))).willReturn(postDto);

String content = new ObjectMapper().writeValueAsString(postDto);
mockMvc.perform(put("/api/board/post")
.contentType(MediaType.APPLICATION_JSON)
.content(content)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.message", is("게시글 수정 성공")))
.andExpect(jsonPath("$.data.content", is("수정된 내용")))
.andDo(print());

verify(boardService).updatePost(any(PostDto.class));
}

@Test
@DisplayName("게시글_수정_실패")
void updatePostFailure() throws Exception {
given(boardService.updatePost(any(PostDto.class))).willThrow(new BoardException("게시글 수정에 실패했습니다."));

String content = new ObjectMapper().writeValueAsString(postDto);
mockMvc.perform(put("/api/board/post")
.contentType(MediaType.APPLICATION_JSON)
.content(content)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.message", is("게시글 수정에 실패했습니다.")))
.andDo(print());

verify(boardService).updatePost(any(PostDto.class));
}

@Test
@DisplayName("게시글_상태_수정_성공")
void updateStatusSuccess() throws Exception {
given(boardService.updateStatus(any(PostDto.class))).willReturn(postDto);

String content = new ObjectMapper().writeValueAsString(postDto);
mockMvc.perform(patch("/api/board/post")
.contentType(MediaType.APPLICATION_JSON)
.content(content)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.message", is("게시글 상태 수정 성공")))
.andDo(print());

verify(boardService).updateStatus(any(PostDto.class));
}

@Test
@DisplayName("게시글_상태_수정_실패")
void updateStatusFailure() throws Exception {
given(boardService.updateStatus(any(PostDto.class))).willThrow(new BoardException("게시글 상태 수정에 실패했습니다."));

String content = new ObjectMapper().writeValueAsString(postDto);
mockMvc.perform(patch("/api/board/post")
.contentType(MediaType.APPLICATION_JSON)
.content(content)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.message", is("게시글 상태 수정에 실패했습니다.")))
.andDo(print());

verify(boardService).updateStatus(any(PostDto.class));
}
}
59 changes: 58 additions & 1 deletion src/test/java/com/tomato/market/service/BoardServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public class BoardServiceTest {

@BeforeEach
void setUp() {
postDto = PostDto.builder().userId(userId).location(location).title(title).category(category).content(content)
postDto = PostDto.builder().postNum(postNum).userId(userId).location(location).title(title).category(category)
.content(content)
.price(price).detailLocation(detailLocation).status(status).boughtUserId(boughtUserId).build();

postEntity = PostDto.toPostEntity(postDto);
Expand Down Expand Up @@ -420,4 +421,60 @@ void getFavoriteListFailure() {

verify(boardDao).findByUserId(userId);
}

@Test
@DisplayName("게시글_수정_성공")
void updatePostSuccess() {
postDto.setContent("수정된 내용");
postEntity.setContent("수정된 내용");
given(boardDao.save(any(PostEntity.class))).willReturn(postEntity);

BoardServiceImpl boardService = new BoardServiceImpl(boardDao);
Assertions.assertEquals(boardService.updatePost(postDto).toString(), postDto.toString());

verify(boardDao).save(any(PostEntity.class));
}

@Test
@DisplayName("게시글_수정_실패")
void updatePostFailure() {
given(boardDao.save(any(PostEntity.class))).willThrow(new BoardException("게시글 수정에 실패했습니다."));

BoardServiceImpl boardService = new BoardServiceImpl(boardDao);
BoardException exception = Assertions.assertThrows(BoardException.class, () -> {
boardService.updatePost(postDto);
});
Assertions.assertEquals(exception.getMessage(), "게시글 수정에 실패했습니다.");

verify(boardDao).save(any(PostEntity.class));
}

@Test
@DisplayName("게시글_상태_수정_성공")
void updateStatusSuccess() {
given(boardDao.findPostByPostNum(any(Integer.class))).willReturn(postEntity);
given(boardDao.save(any(PostEntity.class))).willReturn(postEntity);

BoardServiceImpl boardService = new BoardServiceImpl(boardDao);
Assertions.assertEquals(boardService.updateStatus(postDto).toString(), postDto.toString());

verify(boardDao).findPostByPostNum(any(Integer.class));
verify(boardDao).save(any(PostEntity.class));
}

@Test
@DisplayName("게시글_상태_수정_실패")
void updateStatusFailure() {
given(boardDao.findPostByPostNum(any(Integer.class))).willReturn(postEntity);
given(boardDao.save(any(PostEntity.class))).willThrow(new BoardException("게시글 상태 수정에 실패했습니다."));

BoardServiceImpl boardService = new BoardServiceImpl(boardDao);
BoardException exception = Assertions.assertThrows(BoardException.class, () -> {
boardService.updateStatus(postDto);
});
Assertions.assertEquals(exception.getMessage(), "게시글 상태 수정에 실패했습니다.");

verify(boardDao).findPostByPostNum(any(Integer.class));
verify(boardDao).save(any(PostEntity.class));
}
}
Loading