Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
Expand Down Expand Up @@ -54,7 +56,7 @@ public ApiResponse<AgoraResponseDto> getPostById(@PathVariable Long postId,
}

@PostMapping("/{postId}/like")
public ApiResponse<Boolean> incrementLikeCount(@PathVariable Long postId,
public ApiResponse<Map<String, Object>> incrementLikeCount(@PathVariable Long postId,
@CookieValue(value = "accessToken", required = false) String token) {
if (token == null) {
return new ApiResponse<>(0, "로그인이 필요합니다.", null);
Expand All @@ -65,9 +67,16 @@ public ApiResponse<Boolean> incrementLikeCount(@PathVariable Long postId,
return new ApiResponse<>(0, "유효하지 않은 JWT 토큰입니다.", null);
}

boolean isLike = agoraService.toggleAgoraLike(postId, token);
String message = isLike ? "번 게시글 좋아요 성공" : "번 게시글 좋아요 취소 성공";
Map<String, Object> result = agoraService.toggleAgoraLike(postId, token);
boolean isLiked = (boolean) result.get("isLiked");
long likeCount = (long) result.get("likeCount");

return new ApiResponse<>(1, postId + message, isLike);
String message = isLiked ? "번 게시글 좋아요 성공" : "번 게시글 좋아요 취소 성공";

Map<String, Object> responseData = new HashMap<>();
responseData.put("isLiked", isLiked);
responseData.put("likeCount", likeCount);

return new ApiResponse<>(1, postId + message, responseData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
Expand Down Expand Up @@ -56,8 +58,9 @@ public ApiResponse<QnaPostResponseDto> getPostById(@PathVariable Long postId,
}

@PostMapping("/{postId}/like")
public ApiResponse<Boolean> incrementLikeCount(@PathVariable Long postId,
@CookieValue(value = "accessToken", required = false) String token) {
public ApiResponse<Map<String, Object>> incrementLikeCount(
@PathVariable Long postId,
@CookieValue(value = "accessToken", required = false) String token) {

if (token == null) {
return new ApiResponse<>(0, "로그인이 필요합니다.", null);
Expand All @@ -68,10 +71,17 @@ public ApiResponse<Boolean> incrementLikeCount(@PathVariable Long postId,
return new ApiResponse<>(0, "유효하지 않은 JWT 토큰입니다.", null);
}

boolean isLike = qnaPostService.toggleLike(postId, token);
String message = isLike ? "번 게시글 좋아요 성공" : "번 게시글 좋아요 취소 성공";
Map<String, Object> result = qnaPostService.toggleLike(postId, token);
boolean isLiked = (boolean) result.get("isLiked");
long likeCount = (long) result.get("likeCount");

return new ApiResponse<>(1, postId + message, isLike);
String message = isLiked ? "번 게시글 좋아요 성공" : "번 게시글 좋아요 취소 성공";

Map<String, Object> responseData = new HashMap<>();
responseData.put("isLiked", isLiked);
responseData.put("likeCount", likeCount);

return new ApiResponse<>(1, postId + message, responseData);
}

private String extractTokenFromHeader(HttpServletRequest request) {
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/ajouchong/service/AgoraService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -49,7 +51,7 @@ public AgoraResponseDto createAgoraPost(AgoraRequestDto requestDto, String autho
return convertToAgoraResponseDto(savedPost);
}

@Transactional(readOnly = true)
@Transactional
public AgoraResponseDto getAgoraById(Long postId, String token) {
Agora agora = agoraRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException(postId + "번 게시글을 찾을 수 없습니다."));
Expand Down Expand Up @@ -120,22 +122,23 @@ public void approvePost(Long postId, String token) {


@Transactional
public boolean toggleAgoraLike(Long postId, String token) {
public Map<String, Object> toggleAgoraLike(Long postId, String token) {
String email = jwtTokenProvider.getEmailFromToken(token);
Member member = memberRepository.findByEmail(email)
.orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다."));

Optional<AgoraLike> exLike = agoraLikeRepository.findByMemberAndAgoraId(member, postId);
boolean isLike = false;
boolean isLiked;

if (exLike.isPresent()) {
agoraLikeRepository.delete(exLike.get());
isLiked = false;
} else {
AgoraLike agoraLike = new AgoraLike();
agoraLike.setMember(member);
agoraLike.setAgoraId(postId);
agoraLikeRepository.save(agoraLike);
isLike = true;
isLiked = true;
}

// 좋아요 개수 업데이트
Expand All @@ -145,7 +148,10 @@ public boolean toggleAgoraLike(Long postId, String token) {
agora.setApUserLikeCount((int) likeCount);
agoraRepository.save(agora);

return isLike;
Map<String, Object> result = new HashMap<>();
result.put("isLiked", isLiked);
result.put("likeCount", likeCount);
return result;
}

public AgoraResponseDto convertToAgoraResponseDto(Agora agora) {
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/ajouchong/service/QnaPostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -80,22 +82,23 @@ public List<QnaPostResponseDto> getAllPosts() {
}

@Transactional
public boolean toggleLike(Long postId, String token) {
public Map<String, Object> toggleLike(Long postId, String token) {
String email = jwtTokenProvider.getEmailFromToken(token);
Member member = memberRepository.findByEmail(email)
.orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다."));

Optional<QnaLike> existingLike = qnaLikeRepository.findByMemberAndQnaPostId(member, postId);
boolean isLike = false;
boolean isLiked;

if (existingLike.isPresent()) {
qnaLikeRepository.delete(existingLike.get());
isLiked = false; // 좋아요 취소됨
} else {
QnaLike qnaLike = new QnaLike();
qnaLike.setMember(member);
qnaLike.setQnaPostId(postId);
qnaLikeRepository.save(qnaLike);
isLike = true;
isLiked = true; // 좋아요 추가됨
}

// 좋아요 개수 업데이트
Expand All @@ -105,9 +108,14 @@ public boolean toggleLike(Long postId, String token) {
qnaPost.setQpUserLikeCnt((int) likeCount);
qnaPostRepository.save(qnaPost);

return isLike;
// 결과 반환
Map<String, Object> result = new HashMap<>();
result.put("isLiked", isLiked);
result.put("likeCount", likeCount);
return result;
}


@Transactional
public boolean isUserLikedPost(Long postId, String userEmail) {
Member member = memberRepository.findByEmail(userEmail)
Expand Down
Loading