Skip to content

Commit

Permalink
Merge pull request #38 from Leets-Official/fix/#35/게시글-좋아요-누락-수정
Browse files Browse the repository at this point in the history
[fix] 게시글 좋아요 누락 수정 Bug
  • Loading branch information
yechan-kim authored Nov 13, 2024
2 parents 4244f59 + 38ce769 commit c751d01
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand Down Expand Up @@ -51,7 +52,7 @@ public ResponseEntity<GlobalResponseDto<List<ArticlesResponseDto>>> getMediaArti
}

//게시글 작성
@PostMapping
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "게시글 작성", description = "새 게시글을 작성합니다.")
public ResponseEntity<GlobalResponseDto<ArticleCreateResponseDto>> createArticle(
HttpServletRequest request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public class ArticleResponseDto {
private long likeCnt;
private long commentCnt;
private boolean owner;
private boolean isLiked;
private LocalDateTime createdAt;

@Builder
private ArticleResponseDto(UUID articleId, String userName, String customId, String content,
DeletedStatus deletedStatus,
List<String> hashtags, UUID rePostId, List<String> mediaUrls, List<CommentResponseDto> comments,
int viewCnt, long rePostCnt, long likeCnt, long commentCnt, boolean owner, LocalDateTime createdAt) {
int viewCnt, long rePostCnt, long likeCnt, long commentCnt, boolean owner, boolean isLiked,
LocalDateTime createdAt) {
this.articleId = articleId;
this.userName = userName;
this.customId = customId;
Expand All @@ -55,10 +57,11 @@ private ArticleResponseDto(UUID articleId, String userName, String customId, Str
this.commentCnt = commentCnt;
this.owner = owner;
this.comments = comments;
this.isLiked = isLiked;
this.createdAt = createdAt;
}

public static ArticleResponseDto from(Article article, boolean isOwner, ArticleStatsDto stats,
public static ArticleResponseDto from(Article article, boolean isOwner, boolean isLiked, ArticleStatsDto stats,
Map<UUID, CommentStatsDto> replyStatsMap) {
return ArticleResponseDto.builder()
.articleId(article.getArticleId())
Expand Down Expand Up @@ -91,11 +94,13 @@ public static ArticleResponseDto from(Article article, boolean isOwner, ArticleS
.likeCnt(stats.getLikeCnt())
.commentCnt(stats.getCommentCnt())
.owner(isOwner)
.isLiked(isLiked)
.createdAt(article.getCreatedAt())
.build();
}

public static ArticleResponseDto fromWithoutComments(Article article, boolean isOwner, ArticleStatsDto stats) {
public static ArticleResponseDto fromWithoutComments(Article article, boolean isOwner, boolean isLiked,
ArticleStatsDto stats) {
return ArticleResponseDto.builder()
.articleId(article.getArticleId())
.content(article.getContent())
Expand All @@ -117,6 +122,7 @@ public static ArticleResponseDto fromWithoutComments(Article article, boolean is
.likeCnt(stats.getLikeCnt())
.commentCnt(stats.getCommentCnt())
.owner(isOwner)
.isLiked(isLiked)
.createdAt(article.getCreatedAt())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.leets.xcellentbe.domain.comment.domain.Comment;
import com.leets.xcellentbe.domain.comment.dto.CommentStatsDto;
import com.leets.xcellentbe.domain.commentLike.domain.repository.CommentLikeRepository;
import com.leets.xcellentbe.domain.shared.DeletedStatus;
import com.leets.xcellentbe.global.error.exception.custom.DeleteForbiddenException;
import com.leets.xcellentbe.domain.articleMedia.domain.ArticleMedia;
import com.leets.xcellentbe.domain.articleMedia.domain.repository.ArticleMediaRepository;
Expand Down Expand Up @@ -160,6 +161,8 @@ public ArticleResponseDto getArticle(HttpServletRequest request, UUID articleId)
ArticleStatsDto stats = findArticleStats(targetArticle);
targetArticle.updateViewCount();
boolean isOwner = targetArticle.getWriter().getUserId().equals(user.getUserId());
boolean isLiked = articleLikeRepository.existsByArticle_ArticleIdAndUser_UserIdAndDeletedStatus(
targetArticle.getArticleId(), user.getUserId(), DeletedStatus.NOT_DELETED);

List<Comment> comments = commentRepository.findAllByArticleAndNotDeleted(targetArticle);
Map<UUID, CommentStatsDto> replyStatsMap = comments.stream()
Expand All @@ -171,7 +174,7 @@ public ArticleResponseDto getArticle(HttpServletRequest request, UUID articleId)
return CommentStatsDto.from(likeCount, replyCount);
}
));
return ArticleResponseDto.from(targetArticle, isOwner, stats, replyStatsMap);
return ArticleResponseDto.from(targetArticle, isOwner, isLiked, stats, replyStatsMap);
}

//게시글 전체 조회
Expand All @@ -189,8 +192,10 @@ public List<ArticleResponseDto> getArticles(HttpServletRequest request, LocalDat
.stream()
.map(article -> {
boolean isOwner = article.getWriter().getUserId().equals(user.getUserId());
boolean isLiked = articleLikeRepository.existsByArticle_ArticleIdAndUser_UserIdAndDeletedStatus(
article.getArticleId(), user.getUserId(), DeletedStatus.NOT_DELETED);
ArticleStatsDto stats = findArticleStats(article);
return ArticleResponseDto.fromWithoutComments(article, isOwner, stats);
return ArticleResponseDto.fromWithoutComments(article, isOwner, isLiked, stats);
})
.collect(Collectors.toList());
}
Expand All @@ -215,7 +220,7 @@ public void deleteRepost(HttpServletRequest request, UUID articleId) {
Article targetArticle = articleRepository.findById(articleId)
.orElseThrow(ArticleNotFoundException::new);
// 게시글 작성자와 현재 사용자 일치 여부 확인, 리포스트 ID가 있는 경우에만 삭제 가능
if ((!targetArticle.getWriter().getUserId().equals(user.getUserId()))||(targetArticle.getRePost() == null)) {
if ((!targetArticle.getWriter().getUserId().equals(user.getUserId())) || (targetArticle.getRePost() == null)) {
throw new DeleteForbiddenException();
}
// 리포스트 삭제 처리
Expand All @@ -232,12 +237,11 @@ public ArticleStatsDto findArticleStats(Article article) {

//JWT 토큰 기반 사용자 정보 반환 메소드
private User getUser(HttpServletRequest request) {
User user = jwtService.extractAccessToken(request)

return jwtService.extractAccessToken(request)
.filter(jwtService::isTokenValid)
.flatMap(jwtService::extractEmail)
.flatMap(userRepository::findByEmail)
.orElseThrow(UserNotFoundException::new);

return user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface ArticleLikeRepository extends JpaRepository<ArticleLike, UUID>
long countLikesByArticleId(@Param("articleId") UUID articleId);

Optional<ArticleLike> findByArticle_ArticleIdAndUser_UserIdAndDeletedStatus(UUID articleId, Long userId, DeletedStatus status);

Boolean existsByArticle_ArticleIdAndUser_UserIdAndDeletedStatus(UUID articleId, Long userId, DeletedStatus status);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.leets.xcellentbe.domain.user.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand Down Expand Up @@ -56,7 +57,7 @@ public ResponseEntity<GlobalResponseDto<String>> updateProfileImage(@RequestPara

}

@PatchMapping("/background-image")
@PatchMapping(value = "/background-image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "배경 이미지 수정", description = "사용자의 배경 이미지를 수정합니다.")
public ResponseEntity<GlobalResponseDto<String>> updateBackgroundImage(@RequestParam("file") MultipartFile file,
HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeHttpRequests(
authorize ->
authorize
.requestMatchers("/v3/api-docs", "/v3/api-docs/**", "/swagger-ui.html", "/swagger-ui/**",
"/swagger/**", "/index.html", "/api/auth/**", "/api/chat-room/**", "/dm").permitAll()
.requestMatchers( "/v3/api-docs/**", "/swagger-ui/**").permitAll() // swagger-ui 접근 허용
.requestMatchers("/api/auth/**").permitAll() // 로그인
.requestMatchers("/api/profile/**").permitAll() // 프로필
.requestMatchers("/api/article/**").permitAll() // 게시글
.requestMatchers( "/api/chat-room/**", "/dm/**", "/pub/**", "/sub/**").permitAll() // 채팅
.anyRequest().authenticated()
);
// .oauth2Login(oauth2 -> oauth2.successHandler(oAuthLoginSuccessHandler));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.leets.xcellentbe.global.config.swagger;

import java.lang.reflect.Type;

import org.springframework.http.MediaType;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class MultipartJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter {

public MultipartJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, MediaType.APPLICATION_OCTET_STREAM);
}

@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return false;
}

@Override
public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {
return false;
}

@Override
protected boolean canWrite(MediaType mediaType) {
return false;
}
}

0 comments on commit c751d01

Please sign in to comment.