Skip to content

Commit

Permalink
๐Ÿ› Fix: ํ™ˆํ™”๋ฉด๊ณผ ํ‹ฐํด๋ฌธ๋‹ต ์˜ค๋ฅ˜ ์ˆ˜์ •
Browse files Browse the repository at this point in the history
ํ™ˆํ™”๋ฉด๊ณผ ํ‹ฐํด๋ฌธ๋‹ต ์˜ค๋ฅ˜ ์ˆ˜์ •
  • Loading branch information
lej8924 authored Jul 20, 2024
2 parents 6a2ce93 + cf2759e commit 1d637a3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
.cors(Customizer.withDefaults())
.sessionManagement(configurer -> configurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(authorize ->
authorize.requestMatchers("/users/sign-in", "/users/sign-up", "/swagger-ui/**", "/v3/api-docs/**", "/global/health-check", "/users/**", "/post/**", "/mypage/**").permitAll()
authorize.requestMatchers("/users/sign-in", "/users/sign-up", "/swagger-ui/**", "/v3/api-docs/**", "/global/health-check",
"/users/**", "/post/**", "/mypage/**", "/opinion/**", "/home/**").permitAll()
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.requestMatchers("/users/**","/mypage/**").hasRole("USER")
.anyRequest().authenticated())
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/ticle/server/global/dto/PageInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ticle.server.global.dto;

import org.springframework.data.domain.Page;

public record PageInfo(
int page,
int size,
long totalElements,
int totalPages
) {
public static PageInfo from(Page<?> page) {
return new PageInfo(
page.getNumber() + 1,
page.getSize(),
page.getTotalElements(),
page.getTotalPages()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.ticle.server.opinion.dto.response;

import com.ticle.server.global.dto.PageInfo;
import lombok.Builder;

import java.util.List;

@Builder
public record OpinionResponseList(
PageInfo pageInfo,
List<OpinionResponse> opinionResponseList
) {

public static OpinionResponseList from(List<OpinionResponse> opinionList) {
return new OpinionResponseList(opinionList);
public static OpinionResponseList of(PageInfo pageInfo, List<OpinionResponse> opinionList) {
return new OpinionResponseList(pageInfo, opinionList);
}
}
22 changes: 16 additions & 6 deletions src/main/java/com/ticle/server/opinion/service/OpinionService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ticle.server.opinion.service;

import com.ticle.server.global.dto.PageInfo;
import com.ticle.server.opinion.domain.Comment;
import com.ticle.server.opinion.domain.Opinion;
import com.ticle.server.opinion.domain.type.Order;
Expand All @@ -14,6 +15,7 @@
import com.ticle.server.opinion.repository.HeartRepository;
import com.ticle.server.opinion.repository.OpinionRepository;
import com.ticle.server.user.domain.User;
import com.ticle.server.user.exception.UserNotFoundException;
import com.ticle.server.user.repository.UserRepository;
import com.ticle.server.user.service.CustomUserDetails;
import lombok.RequiredArgsConstructor;
Expand All @@ -31,6 +33,7 @@
import static com.ticle.server.opinion.domain.type.Order.getOrder;
import static com.ticle.server.opinion.exception.errorcode.OpinionErrorCode.COMMENT_NOT_FOUND;
import static com.ticle.server.opinion.exception.errorcode.OpinionErrorCode.OPINION_NOT_FOUND;
import static com.ticle.server.user.exception.errorcode.UserErrorCode.USER_NOT_FOUND;

@Slf4j
@Service
Expand All @@ -48,7 +51,7 @@ public class OpinionService {
@Transactional
public void uploadComment(CommentUploadRequest request, Long talkId, CustomUserDetails userId) {
User user = userRepository.findById(userId.getUserId())
.orElseThrow(RuntimeException::new);
.orElseThrow(() -> new UserNotFoundException(USER_NOT_FOUND));
Opinion opinion = opinionRepository.findById(talkId)
.orElseThrow(() -> new OpinionNotFoundException(OPINION_NOT_FOUND));

Expand Down Expand Up @@ -80,8 +83,6 @@ public void heartComment(Long commentId, CustomUserDetails userDetails) {

@Transactional
public List<CommentResponse> getCommentsByOpinion(Long opinionId, CustomUserDetails userDetails, Order orderBy) {
User user = userRepository.findById(userDetails.getUserId())
.orElseThrow(RuntimeException::new);
Opinion opinion = opinionRepository.findByOpinionIdWithFetch(opinionId)
.orElseThrow(() -> new OpinionNotFoundException(OPINION_NOT_FOUND));

Expand All @@ -94,8 +95,16 @@ public List<CommentResponse> getCommentsByOpinion(Long opinionId, CustomUserDeta

return comments.stream()
.map(comment -> {
boolean isHeart = comment.getHearts().stream()
.anyMatch(heart -> heart.getUser().equals(user));
boolean isHeart = false;

if (userDetails != null) {
User user = userRepository.findById(userDetails.getUserId())
.orElseThrow(() -> new UserNotFoundException(USER_NOT_FOUND));

isHeart = comment.getHearts().stream()
.anyMatch(heart -> heart.getUser().equals(user));
}

return CommentResponse.of(comment, isHeart);
})
.toList();
Expand All @@ -105,11 +114,12 @@ public OpinionResponseList getOpinionsByPage(int page) {
Pageable pageable = PageRequest.of(page - 1, PAGE_SIZE, getOrder(TIME));

Page<Opinion> opinionPage = opinionRepository.findAll(pageable);
PageInfo pageInfo = PageInfo.from(opinionPage);

List<OpinionResponse> opinionResponseList = opinionPage.stream()
.map(OpinionResponse::from)
.toList();

return OpinionResponseList.from(opinionResponseList);
return OpinionResponseList.of(pageInfo, opinionResponseList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@Getter
@RequiredArgsConstructor
public enum UserErrorCode implements ErrorCode {
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "Usere not found"),
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "User not found"),
;

private final HttpStatus httpStatus;
Expand Down

0 comments on commit 1d637a3

Please sign in to comment.