From 015cf44452a571bbcf940750a74503aa9eba0f52 Mon Sep 17 00:00:00 2001 From: 3un0ia Date: Tue, 21 May 2024 19:54:32 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=EB=B8=94=EB=A1=9C=EA=B7=B8=20?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=20dto=20=EB=B3=84=EB=8F=84=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EB=B0=8F=20=EA=B4=80=EB=A0=A8=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tech/blog/controller/PostController.java | 3 ++- .../tgwing/tech/blog/dto/PostCreationDto.java | 24 +++++++++++++++++++ .../tgwing/tech/blog/entity/PostEntity.java | 4 ++++ .../tgwing/tech/blog/service/PostService.java | 3 ++- .../tech/blog/service/PostServiceImpl.java | 13 +++++----- 5 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 src/main/java/kr/tgwing/tech/blog/dto/PostCreationDto.java diff --git a/src/main/java/kr/tgwing/tech/blog/controller/PostController.java b/src/main/java/kr/tgwing/tech/blog/controller/PostController.java index 5fa9b58..0fb4fc9 100644 --- a/src/main/java/kr/tgwing/tech/blog/controller/PostController.java +++ b/src/main/java/kr/tgwing/tech/blog/controller/PostController.java @@ -1,5 +1,6 @@ package kr.tgwing.tech.blog.controller; +import kr.tgwing.tech.blog.dto.PostCreationDto; import kr.tgwing.tech.blog.dto.PostDto; import kr.tgwing.tech.blog.exception.PathHasNoPostIdException; import kr.tgwing.tech.blog.service.PostServiceImpl; @@ -61,7 +62,7 @@ public ResponseEntity getPost(@PathVariable(required = false, name = "p } @PostMapping("/blog") // 블로그 작성 - POST, /tgwing.kr/blog/post - public ResponseEntity post(@RequestBody PostDto requestDto, + public ResponseEntity post(@RequestBody PostCreationDto requestDto, Principal principal) { System.out.println("-- Post new post --"); diff --git a/src/main/java/kr/tgwing/tech/blog/dto/PostCreationDto.java b/src/main/java/kr/tgwing/tech/blog/dto/PostCreationDto.java new file mode 100644 index 0000000..beb8efc --- /dev/null +++ b/src/main/java/kr/tgwing/tech/blog/dto/PostCreationDto.java @@ -0,0 +1,24 @@ +package kr.tgwing.tech.blog.dto; + +import kr.tgwing.tech.blog.entity.PostEntity; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +@AllArgsConstructor +public class PostCreationDto { + private String title; + private String content; + private String thumbnail; + + public static PostEntity toEntity(PostCreationDto dto) { + return PostEntity.builder() + .title(dto.title) + .content(dto.content) + .thumbnail(dto.thumbnail) + .build(); + } + +} diff --git a/src/main/java/kr/tgwing/tech/blog/entity/PostEntity.java b/src/main/java/kr/tgwing/tech/blog/entity/PostEntity.java index cee5180..0cd28f8 100644 --- a/src/main/java/kr/tgwing/tech/blog/entity/PostEntity.java +++ b/src/main/java/kr/tgwing/tech/blog/entity/PostEntity.java @@ -53,4 +53,8 @@ public void updateContent(PostDto postDto) { this.content = postDto.getContent(); this.thumbnail = postDto.getThumbnail(); } + + public void setWriter(Long writer) { + this.writer = writer; + } } diff --git a/src/main/java/kr/tgwing/tech/blog/service/PostService.java b/src/main/java/kr/tgwing/tech/blog/service/PostService.java index 8d29500..2b92095 100644 --- a/src/main/java/kr/tgwing/tech/blog/service/PostService.java +++ b/src/main/java/kr/tgwing/tech/blog/service/PostService.java @@ -1,5 +1,6 @@ package kr.tgwing.tech.blog.service; +import kr.tgwing.tech.blog.dto.PostCreationDto; import kr.tgwing.tech.blog.dto.PostDto; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -9,7 +10,7 @@ public interface PostService { public PostDto getPost(Long postId); - public PostDto createPost(PostDto requestDto, String token); + public PostDto createPost(PostCreationDto requestDto, String token); public PostDto updatePost(PostDto postDto, Long postId, String utilStudentId); public void deletePost(Long postId, String utilStudentId); public Page getPostsInPage(String text, Pageable pageable); diff --git a/src/main/java/kr/tgwing/tech/blog/service/PostServiceImpl.java b/src/main/java/kr/tgwing/tech/blog/service/PostServiceImpl.java index d923c22..fe7c1b5 100644 --- a/src/main/java/kr/tgwing/tech/blog/service/PostServiceImpl.java +++ b/src/main/java/kr/tgwing/tech/blog/service/PostServiceImpl.java @@ -1,6 +1,7 @@ package kr.tgwing.tech.blog.service; +import kr.tgwing.tech.blog.dto.PostCreationDto; import kr.tgwing.tech.blog.dto.PostDto; import kr.tgwing.tech.blog.entity.PostEntity; import kr.tgwing.tech.blog.exception.PostNotFoundException; @@ -55,20 +56,20 @@ private Page getAllPosts(PageRequest pageRequest) { // 모든 공지 } @Override - public PostDto createPost(PostDto requestDto, String utilStudentId) // 공지 생성하기 + public PostDto createPost(PostCreationDto requestDto, String utilStudentId) // 공지 생성하기 { // 글을 작성할 user 조회 - Optional userById = userRepository.findById(requestDto.getWriter()); -// // userEntity를 받아오지 못한 경우 - 회원을 찾을 수 없음 (Exception) - UserEntity userEntity = userById.orElseThrow(UserNotFoundException::new); + Optional byStudentId = userRepository.findByStudentId(utilStudentId); + UserEntity userEntity = byStudentId.orElseThrow(UserNotFoundException::new); // 현재 사용자와 요청 시 들어온 writer가 같은지 확인 if (!Objects.equals(userEntity.getStudentId(), utilStudentId)) { throw new WrongPostRequestException(); } - // entity에는 저장된 썸네일의 uri만 추가로 넣어서 저장 - PostEntity postEntity = toEntity(requestDto); + // entity에 작성자 Id 설정해서 저장하기 + PostEntity postEntity = PostCreationDto.toEntity(requestDto); + postEntity.setWriter(userEntity.getId()); PostEntity savedEntity = postRepository.save(postEntity); return toDto(savedEntity);