Skip to content
Snippets Groups Projects
Commit cf73aa05 authored by Gwan Ju's avatar Gwan Ju
Browse files

feat: Add 'isHeart' field to PostResDto, update related functionality

parent 87db3eaf
No related branches found
No related tags found
No related merge requests found
......@@ -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'
......
......@@ -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();
......
......@@ -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");
}
}
......
......@@ -9,6 +9,5 @@ import umc.spring.file.domain.S3File;
public class PostDto {
String title;
String body;
int likeCount;
S3File s3File;
}
......@@ -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();
}
}
......@@ -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();
......
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment