Skip to content

Commit

Permalink
fix: post관련 오류 수정
Browse files Browse the repository at this point in the history
post에서 swagger에서 테스트 할 수있게 config 설정 및 postDto에 있던 BoardType을 없앰. controller에서 따로 받게함.
  • Loading branch information
s13121312 committed Aug 27, 2024
1 parent 1f0aeff commit 9e8e404
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
17 changes: 17 additions & 0 deletions src/main/java/certis/CertisHomepage/config/SwaggerBeanConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package certis.CertisHomepage.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.util.ArrayList;

@Configuration
public class SwaggerBeanConfig {

public SwaggerBeanConfig(MappingJackson2HttpMessageConverter converter) {
var supportedMediaTypes = new ArrayList<>(converter.getSupportedMediaTypes());
supportedMediaTypes.add(new MediaType("application", "octet-stream"));
converter.setSupportedMediaTypes(supportedMediaTypes);
}
}
12 changes: 7 additions & 5 deletions src/main/java/certis/CertisHomepage/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -92,21 +93,22 @@ public PageApi<List<PostDto>> searchPosts(Pageable pageable, BoardType boardType

//게시물 작성
@Transactional
public PostDto write(PostDto postDto, UserEntity userEntity, List<MultipartFile> files) throws IOException {
public PostDto write(BoardType type, PostDto postDto, UserEntity userEntity, List<MultipartFile> files) throws IOException {
log.info("userEntity is: {}", userEntity);
if(files == null){
files = new ArrayList<>();
}
List<ImageEntity> imageList = fileHandler.parseFileInfo(files);

PostEntity post = PostEntity.builder()
.title(postDto.getTitle())
.content(postDto.getContent())
.view(0L)
.boardType(postDto.getBoardType())
.boardType(type)
.registeredAt(LocalDateTime.now())
.user(userEntity)
.build();

//userEntity.addPost(post); // Bidirectional relationship 설정

if(!imageList.isEmpty()){
for (ImageEntity image : imageList){
//원래 이미지이름이 빈것이아니라면 저장하기.
Expand All @@ -125,7 +127,7 @@ public PostDto write(PostDto postDto, UserEntity userEntity, List<MultipartFile>

//게시물 수정
@Transactional
public PostDto update(Long id, PostDto postDto, List<MultipartFile> files) throws IOException {
public PostDto update(Long id, BoardType type, PostDto postDto, List<MultipartFile> files) throws IOException {
PostEntity post = postRepository.findById(id).orElseThrow(() -> {
return new IllegalArgumentException("해당 게시물 id 가 없습니다");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

Expand Down Expand Up @@ -81,12 +82,12 @@ public PageApi<List<PostDto>> searchPosts(

//게시글 작성
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/{boardType}/write")
@PostMapping(value = "/{boardType}/write", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE})
public Response write(
@Valid
@PathVariable("boardType") String boardType,
@RequestPart("postDto") PostDto postDto, //@ResponseBody, @RequestBody의 차이
@RequestPart("files") List<MultipartFile> files, //// @RequestPart: MultipartFile을 받음
@RequestPart(value = "files", required = false) List<MultipartFile> files, //// @RequestPart: MultipartFile을 받음
@RequestHeader("authorization-token") String accesstoken//@RequestHeader를 통해 헤더에 있는 토큰 값을 받아옴
) throws Exception {
// 원래 로그인을 하면, User 정보는 세션을 통해서 구하고 주면 되지만,
Expand All @@ -112,15 +113,14 @@ public Response write(
}

String bt = boardType.toUpperCase();
BoardType type;
//NOTI도 아니고 PROJECT도 아니면 오류 발생
try {
BoardType type = BoardType.valueOf(bt);
postDto.setBoardType(type);
type = BoardType.valueOf(bt);
}catch (IllegalArgumentException e){
throw new ApiException(PostErrorCode.POST_NOT_EXIST);
}
return new Response("성공", " 게시물 작성",postService.write(postDto, user, files));

return new Response("성공", " 게시물 작성",postService.write(type, postDto, user, files));


}
Expand All @@ -132,21 +132,22 @@ public Response write(
public Response edit(
@PathVariable("boardType") String boardType,
@PathVariable("id") Long id,
@RequestPart("files") List<MultipartFile> files,
@RequestPart(value = "files", required = false) List<MultipartFile> files,
@RequestPart("postDto") PostDto postDto,
@RequestHeader("authorization-token") String accesstoken
) throws IOException {


Optional<PostEntity> post = postRepository.findById(id);

if (post.isEmpty() || post.get().getBoardType() != BoardType.valueOf(boardType.toUpperCase())) {
throw new ApiException(PostErrorCode.POST_NOT_EXIST);
}


//쓴사람이 아니라면 수정도 불가
if(Objects.equals(post.get().getUser().getId(), tokenBusiness.validationAccessToken(accesstoken))) {
return new Response("성공", "글 수정 성공", postService.update(id, postDto, files));
return new Response("성공", "글 수정 성공", postService.update(id, post.get().getBoardType(), postDto, files));
}else{
return new Response("실패", "글 수정 실패", new ApiException(UserErrorCode.USER_NOT_CORRET));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class PostDto {

private LocalDateTime registeredAt;

private BoardType boardType;

private Long userId;

Expand All @@ -44,6 +43,6 @@ public class PostDto {


public static PostDto toDto(PostEntity post){
return new PostDto(post.getId(), post.getTitle(), post.getContent(), post.getRegisteredAt(), post.getBoardType(),post.getUser().getId(),post.getView(),post.getModifiedAt());
return new PostDto(post.getId(), post.getTitle(), post.getContent(), post.getRegisteredAt(), post.getUser().getId(),post.getView(),post.getModifiedAt());
}
}

0 comments on commit 9e8e404

Please sign in to comment.