Skip to content

Commit

Permalink
conflict merged
Browse files Browse the repository at this point in the history
  • Loading branch information
dong-yxxn committed Nov 14, 2024
2 parents afe43b2 + 2087c60 commit 8c9fa61
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import org.springframework.data.repository.query.Param;
import team7.inplace.likedPlace.domain.LikedPlace;

import java.util.Optional;

public interface LikedPlaceRepository extends JpaRepository<LikedPlace, Long> {

Optional<LikedPlace> findByUserIdAndPlaceId(Long userId, Long placeId);

Page<LikedPlace> findByUserIdAndIsLikedTrue(Long userId, Pageable pageable);

@Query("SELECT l.place.id FROM LikedPlace l WHERE l.user.id = :userId AND l.isLiked = true")
Set<Long> findPlaceIdsByUserIdAndIsLikedTrue(@Param("userId") Long userId);

@Query("SELECT lp FROM LikedPlace lp JOIN FETCH lp.place WHERE lp.user.id = :userId AND lp.isLiked = true")
Page<LikedPlace> findByUserIdAndIsLikedTrueWithPlace(@Param("userId") Long userId,
Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ public PlaceMessageCommand getPlaceMessageCommand(Long placeId) {
}

public Page<LikedPlaceInfo> getLikedPlaceInfo(Long userId, Pageable pageable) {
Page<LikedPlace> placePage = likedPlaceRepository.findByUserIdAndIsLikedTrue(userId,
pageable);
Page<LikedPlace> placePage = likedPlaceRepository.findByUserIdAndIsLikedTrueWithPlace(
userId, pageable);
List<Long> placeIds = placePage.map(likedPlace -> likedPlace.getPlace().getId()).toList();
List<Video> videos = videoRepository.findByPlaceIdIn(placeIds);
List<Video> videos = videoRepository.findByPlaceIdInWithInfluencer(placeIds);
Map<Long, String> placeIdToInfluencerName = getMapPlaceIdToInfluencerName(videos);

List<LikedPlaceInfo> likedPlaceInfos = placePage.getContent().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void deleteReview(Long reviewId) {

@Transactional(readOnly = true)
public Page<MyReviewInfo> getMyReviews(Long userId, Pageable pageable) {
Page<Review> reviewPage = reviewRepository.findByUserId(userId, pageable);
Page<Review> reviewPage = reviewRepository.findByUserIdWithPlace(userId, pageable);

return reviewPage.map(MyReviewInfo::from);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import team7.inplace.review.domain.Review;

public interface ReviewRepository extends JpaRepository<Review, Long> {
Expand All @@ -11,9 +12,10 @@ public interface ReviewRepository extends JpaRepository<Review, Long> {

Page<Review> findByPlaceId(Long placeId, Pageable pageable);

Page<Review> findByUserId(Long userId, Pageable pageable);

Integer countByPlaceIdAndIsLikedTrue(Long placeId);

Integer countByPlaceIdAndIsLikedFalse(Long placeId);

@Query("SELECT r FROM Review r JOIN FETCH r.place WHERE r.user.id = :userId")
Page<Review> findByUserIdWithPlace(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public CorsFilter corsFilter() {
var source = new UrlBasedCorsConfigurationSource();
var config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("https://www.inplace.my");
config.addAllowedOriginPattern("https://www.inplace.my");
config.addAllowedOriginPattern("https://api.inplace.my");
config.addAllowedHeader("Origin");
config.addAllowedHeader("Accept");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

@Slf4j
public class LoginAuthenticationEntryPoint implements AuthenticationEntryPoint {

@Value("${spring.redirect.front-end-url}")
private String frontEndUrl;

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException {
AuthenticationException authException) throws IOException {
log.info("authentication entryPoint");
response.sendRedirect(frontEndUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
Expand All @@ -14,11 +19,7 @@
import team7.inplace.security.application.dto.CustomOAuth2User;
import team7.inplace.security.util.JwtUtil;

import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

@Slf4j
public class AuthorizationFilter extends OncePerRequestFilter {

private final JwtUtil jwtUtil;
Expand All @@ -29,10 +30,24 @@ public AuthorizationFilter(JwtUtil jwtUtil) {

@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain
) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
log.info("cookie exists");
Cookie accessToken = Arrays.stream(cookies)
.filter(
cookie -> cookie.getName().equals(TokenType.ACCESS_TOKEN.getValue()))
.findAny()
.orElse(null);
if (accessToken != null) {
log.info("Access token: {}", accessToken.getValue());
}
} else {
log.info("cookie is null");
}
if (hasNoTokenCookie(request)) {
filterChain.doFilter(request, response);
return;
Expand All @@ -52,21 +67,21 @@ protected void doFilterInternal(

private boolean hasNoTokenCookie(HttpServletRequest request) {
return Optional.ofNullable(request.getCookies())
.flatMap(cookies -> Arrays.stream(cookies)
.filter(
cookie -> cookie.getName().equals(TokenType.ACCESS_TOKEN.getValue())
|| cookie.getName().equals(TokenType.REFRESH_TOKEN.getValue())
)
.findAny())
.isEmpty();
.flatMap(cookies -> Arrays.stream(cookies)
.filter(
cookie -> cookie.getName().equals(TokenType.ACCESS_TOKEN.getValue())
|| cookie.getName().equals(TokenType.REFRESH_TOKEN.getValue())
)
.findAny())
.isEmpty();
}

private String getAccessToken(HttpServletRequest request) throws InplaceException {
Cookie accessTokenCookie = Arrays.stream(request.getCookies())
.filter(
cookie -> cookie.getName().equals(TokenType.ACCESS_TOKEN.getValue()))
.findAny()
.orElse(null);
.filter(
cookie -> cookie.getName().equals(TokenType.ACCESS_TOKEN.getValue()))
.findAny()
.orElse(null);
if (Objects.isNull(accessTokenCookie)) {
return null;
}
Expand All @@ -75,10 +90,10 @@ private String getAccessToken(HttpServletRequest request) throws InplaceExceptio

private String getRefreshToken(HttpServletRequest request) throws InplaceException {
Cookie refreshTokenCookie = Arrays.stream(request.getCookies())
.filter(
cookie -> cookie.getName().equals(TokenType.REFRESH_TOKEN.getValue()))
.findAny()
.orElse(null);
.filter(
cookie -> cookie.getName().equals(TokenType.REFRESH_TOKEN.getValue()))
.findAny()
.orElse(null);
if (Objects.isNull(refreshTokenCookie)) {
return null;
}
Expand All @@ -91,7 +106,7 @@ private void addUserToAuthentication(String token) throws InplaceException {
String roles = jwtUtil.getRoles(token);
CustomOAuth2User customOAuth2User = new CustomOAuth2User(username, id, roles);
Authentication authToken = new OAuth2AuthenticationToken(customOAuth2User,
customOAuth2User.getAuthorities(), customOAuth2User.getRegistrationId());
customOAuth2User.getAuthorities(), customOAuth2User.getRegistrationId());
SecurityContextHolder.getContext().setAuthentication(authToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import java.io.IOException;

@Slf4j
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

@Value("${spring.redirect.front-end-url}")
private String frontEndUrl;

@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException accessDeniedException) throws IOException {
AccessDeniedException accessDeniedException) throws IOException {
log.info("Access denied");
response.sendRedirect(frontEndUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
Expand All @@ -13,8 +14,6 @@
import team7.inplace.global.exception.InplaceException;
import team7.inplace.global.exception.code.AuthorizationErrorCode;

import java.io.IOException;

@Slf4j
public class CustomFailureHandler implements AuthenticationFailureHandler {

Expand All @@ -28,10 +27,11 @@ public CustomFailureHandler(ObjectMapper objectMapper) {

@Override
public void onAuthenticationFailure(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException exception
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException exception
) throws IOException {
log.info("Authentication failure");
String accept = request.getHeader("Accept");
if (StringUtils.hasText(accept) && accept.contains("text/html")) {
response.sendRedirect(frontEndUrl);
Expand All @@ -40,13 +40,13 @@ public void onAuthenticationFailure(
}

private void setErrorResponse(
HttpServletResponse response,
InplaceException inplaceException
HttpServletResponse response,
InplaceException inplaceException
) throws IOException {
response.setStatus(inplaceException.getHttpStatus().value());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(
inplaceException.getHttpStatus(), inplaceException.getMessage());
inplaceException.getHttpStatus(), inplaceException.getMessage());
response.getWriter().write(objectMapper.writeValueAsString(problemDetail));
}
}
13 changes: 7 additions & 6 deletions src/main/java/team7/inplace/security/util/CookieUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ public class CookieUtil {

public static ResponseCookie createCookie(String key, String value) {
return ResponseCookie.from(key, value)
.sameSite("None")
.secure(true)
.path("/")
.httpOnly(true)
.maxAge(60 * 60)
.build();
.sameSite("None")
.secure(true)
.path("/")
.httpOnly(true)
.domain("inplace.my")
.maxAge(60 * 60)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package team7.inplace.video.persistence;

import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import team7.inplace.place.domain.Place;
import team7.inplace.video.domain.Video;

import java.util.List;
import java.util.Optional;

public interface VideoRepository extends JpaRepository<Video, Long> {

@Query("SELECT v FROM Video v JOIN FETCH v.place JOIN FETCH v.influencer ORDER BY v.viewCountIncrease DESC")
List<Video> findTop10ByOrderByViewCountIncreaseDesc(Pageable pageable);

Expand All @@ -23,12 +23,15 @@ public interface VideoRepository extends JpaRepository<Video, Long> {
Optional<Video> findTopByPlaceOrderByIdDesc(Place place);

@Query(
value = "SELECT v FROM Video v JOIN FETCH v.influencer WHERE v.place IS NULL",
countQuery = "SELECT COUNT(v) FROM Video v"
value = "SELECT v FROM Video v JOIN FETCH v.influencer WHERE v.place IS NULL",
countQuery = "SELECT COUNT(v) FROM Video v"
)
Page<Video> findAllByPlaceIsNull(Pageable pageable);

List<Video> findByPlaceIdIn(List<Long> placeIds);

List<Video> findByPlaceId(Long placeId);

@Query("SELECT v FROM Video v JOIN FETCH v.influencer WHERE v.place.id IN :placeIds")
List<Video> findByPlaceIdInWithInfluencer(List<Long> placeIds);
}
2 changes: 1 addition & 1 deletion src/main/resources/sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ create table places
latitude text not null,
longitude text not null,
menu_img_url text null,
category enum ('CAFE', 'JAPANESE', 'KOREAN', 'NONE', 'RESTAURANT', 'WESTERN') not null
category enum ('CAFE', 'JAPANESE', 'KOREAN', 'NONE', 'RESTAURANT', 'WESTERN') not null,

FULLTEXT INDEX ft_name_ngram (name) WITH PARSER ngram,
INDEX idx_long_lat (longitude(15), latitude(15))
Expand Down

0 comments on commit 8c9fa61

Please sign in to comment.