diff --git a/build.gradle b/build.gradle index 0cd61ecdd06f9699088ba14da303bc73793e3fed..17ec5eba20dbe6e646aefa2a485ab7ba0ae3ff14 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' - + implementation 'com.google.code.gson:gson:2.8.9' // swagger implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0' implementation 'org.hibernate.validator:hibernate-validator:7.0.1.Final' diff --git a/src/main/java/umc/spring/post/config/security/SecurityUtil.java b/src/main/java/umc/spring/post/config/security/SecurityUtil.java index fe26efe609113056cf5764e9fce6c271b691302f..030ac5ff3585ea81106b0cab6178cf6037afd910 100644 --- a/src/main/java/umc/spring/post/config/security/SecurityUtil.java +++ b/src/main/java/umc/spring/post/config/security/SecurityUtil.java @@ -17,7 +17,6 @@ public class SecurityUtil { final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null || authentication.getName() == null) { - System.out.println("2번"); throw new RuntimeException("No authentication information."); } MyUser myUser = (MyUser) authentication.getPrincipal(); diff --git a/src/main/java/umc/spring/post/controller/PostController.java b/src/main/java/umc/spring/post/controller/PostController.java index 00357386f21587771a8252beb9e32a433b08eb29..c2b532ca330a941db183da1d34c781a619242d45 100644 --- a/src/main/java/umc/spring/post/controller/PostController.java +++ b/src/main/java/umc/spring/post/controller/PostController.java @@ -27,9 +27,9 @@ public class PostController { @ResponseStatus(HttpStatus.OK) @PostMapping("/post/upload") - public void upload(@RequestBody PostDto postDto){ + public PostResDto upload(@RequestBody PostDto postDto){ try{ - postService.upload(postDto); + return postService.upload(postDto); } catch(Exception e){ throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token not Found"); @@ -63,6 +63,7 @@ public class PostController { postService.likeCrew(id); } catch (Exception e){ + System.out.println("여기"); throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token not found"); } } diff --git a/src/main/java/umc/spring/post/data/dto/PostDto.java b/src/main/java/umc/spring/post/data/dto/PostDto.java index e6830cf366fd807ee20270249623e875f6754ef4..78c39ea7b3db7fb158438370777c2aeb81cbe0b3 100644 --- a/src/main/java/umc/spring/post/data/dto/PostDto.java +++ b/src/main/java/umc/spring/post/data/dto/PostDto.java @@ -9,6 +9,5 @@ import umc.spring.file.domain.S3File; public class PostDto { String title; String body; - int likeCount; S3File s3File; } diff --git a/src/main/java/umc/spring/post/data/dto/PostResDto.java b/src/main/java/umc/spring/post/data/dto/PostResDto.java index ccd903e4b58dd96970a858e91181d06fba703bf2..34bc73f08738958b99f4d90b462899c731cdbe7d 100644 --- a/src/main/java/umc/spring/post/data/dto/PostResDto.java +++ b/src/main/java/umc/spring/post/data/dto/PostResDto.java @@ -4,12 +4,15 @@ import jakarta.persistence.*; import lombok.*; import umc.spring.file.domain.S3File; import umc.spring.post.data.entity.Comment; +import umc.spring.post.data.entity.LikeData; import umc.spring.post.data.entity.Post; import java.util.ArrayList; import java.util.Date; import java.util.List; +import static umc.spring.post.config.security.SecurityUtil.getCurrentMemberId; + @Data @Builder public class PostResDto { @@ -24,6 +27,8 @@ public class PostResDto { Date createdTime; Date modifiedTime; List<CommentResDto> comments; + boolean isHeart; + public static PostResDto toDTO(Post post){ List<CommentResDto> resDtos = new ArrayList<>(); @@ -31,6 +36,36 @@ public class PostResDto { CommentResDto dto = CommentResDto.toDTO(comment); resDtos.add(dto); }); + UserInfoDto userInfoDto; + boolean flag=false; + + try { + // 유저 모드 + userInfoDto = getCurrentMemberId(); + Long userId = userInfoDto.getUserId(); + List<LikeData> likes = post.getLikes(); + for (LikeData likeData : likes) { + if (likeData.getUser().getId().equals(userId)) { + flag = true; + break; + } + } + } catch (Exception e) { + // 게스트 모드 + return PostResDto.builder() + .id(post.getId()) + .userId(post.getUserId()) + .s3File(post.getS3File()) + .title(post.getTitle()) + .author(post.getAuthor()) + .body(post.getBody()) + .likeCount(post.getLikes().size()) + .createdTime(post.getCreatedTime()) + .modifiedTime(post.getModifiedTime()) + .comments(resDtos) + .isHeart(flag).build(); + } + return PostResDto.builder() .id(post.getId()) .userId(post.getUserId()) @@ -41,6 +76,7 @@ public class PostResDto { .likeCount(post.getLikes().size()) .createdTime(post.getCreatedTime()) .modifiedTime(post.getModifiedTime()) - .comments(resDtos).build(); + .comments(resDtos) + .isHeart(flag).build(); } } diff --git a/src/main/java/umc/spring/post/service/PostService.java b/src/main/java/umc/spring/post/service/PostService.java index b3382a7aa1cda94e07ef117a6ed803a7ab1b46c1..299f50b05e2933deee0f57ddbd00ad7ed0bc4355 100644 --- a/src/main/java/umc/spring/post/service/PostService.java +++ b/src/main/java/umc/spring/post/service/PostService.java @@ -11,7 +11,7 @@ import java.io.IOException; import java.util.List; public interface PostService { - void upload(PostDto postDto); + PostResDto upload(PostDto postDto); List<PostResDto> getAllPost(); diff --git a/src/main/java/umc/spring/post/service/PostServiceImpl.java b/src/main/java/umc/spring/post/service/PostServiceImpl.java index e5a286fd732a3aa41cab6830821a182458fce887..403d3b430ffecd2961cce3ea105ced6e42f633fe 100644 --- a/src/main/java/umc/spring/post/service/PostServiceImpl.java +++ b/src/main/java/umc/spring/post/service/PostServiceImpl.java @@ -1,6 +1,7 @@ package umc.spring.post.service; +import com.google.gson.Gson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; @@ -44,7 +45,7 @@ public class PostServiceImpl implements PostService{ } @Override - public void upload(PostDto postDto){ + public PostResDto upload(PostDto postDto){ UserInfoDto userInfoDto = getCurrentMemberId(); Post post = new Post(); setPost(postDto, post); @@ -54,6 +55,7 @@ public class PostServiceImpl implements PostService{ post.setModifiedTime(post.getCreatedTime()); postRepository.save(post); + return PostResDto.toDTO(post); } @@ -125,7 +127,6 @@ public class PostServiceImpl implements PostService{ @Override public void likeCrew(Long id) { UserInfoDto userInfoDto = getCurrentMemberId(); - // 내가 좋아요를 누른적이 있는지? 있으면 반영안됨 Post post = postRepository.findById(id).orElseThrow(() -> new RuntimeException("id가 존재하지 않습니다.")); User user = userRepository.findByLoginId(userInfoDto.getLoginId()).orElseThrow(); Optional<LikeData> byPostAndUser = likeRepository.findByPostAndUser(post, user); @@ -162,7 +163,6 @@ public class PostServiceImpl implements PostService{ else{ throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "좋아요를 누른 적이 없습니다"); } - } @Override