Skip to content

Commit

Permalink
[LH-199] Fix get matched list api
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungJun-L committed Nov 17, 2023
1 parent ada985d commit 60c4b2c
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd-dev.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: cd
name: cd-dev
on:
workflow_run:
workflows: [ ci ]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cd-main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: cd
name: cd-main
on:
workflow_run:
workflows: [ ci ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.security.GeneralSecurityException;
import java.util.Objects;

@Profile({"local", "dev"})
@Profile("!test")
@RequiredArgsConstructor
@Service
public class GoogleIdTokenService implements IdTokenService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ExceptionAuthenticationFailureHandler implements AuthenticationFail

@Override
public void onAuthenticationFailure(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException ex) throws IOException {
log.warn("", ex);
log.warn("{}", ex.getMessage());

HttpStatus status;
String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.lighthouse.lingoswap.auth.config;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

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

@Slf4j
@RequiredArgsConstructor
@Component
public class LoggingFilter extends OncePerRequestFilter {

/**
* servletPath:/api/v1/user/form/interests
* <p>
* After request [GET /api/v1/user/form/country, client=0:0:0:0:0:0:0:1, headers=[user-agent:"PostmanRuntime/7.35.0", accept:"*{@literal /}*", host:"localhost:8080", accept-encoding:"gzip, deflate, br", connection:"keep-alive"]]
* <p>
* pathInfo:null
* <p>
* headers:
* <p>
* user-agent: PostmanRuntime/7.35.0
* <p>
* host
*/
@Override
protected void doFilterInternal(@NotNull final HttpServletRequest request,
@NotNull final HttpServletResponse response,
@NotNull final FilterChain filterChain) throws ServletException, IOException {
LogDto logDto = LogDto.from(request);
log.info("[REQUEST] {}", logDto);
filterChain.doFilter(request, response);
}

private record LogDto(String method,
String uri,
String queryString,
String realIp,
String region,
String userAgent,
String acceptLanguage) {

static LogDto from(HttpServletRequest request) {
return new LogDto(
request.getMethod(),
request.getRequestURI(),
request.getQueryString(),
request.getHeader("X-Real-IP"),
request.getHeader("Region"),
request.getHeader("User-Agent"),
request.getHeader("Accept-Language"));
}

@Override
public String toString() {
return "%s %s%s%n headers=[X-Real-IP: %s, Region: %s, User-Agent: %s, Accept-Language: %s]%n"
.formatted(
method,
uri,
Optional.ofNullable(queryString).map(q -> "?" + q).orElse(""),
realIp,
region,
userAgent,
acceptLanguage);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.security.web.session.DisableEncodeUrlFilter;

@RequiredArgsConstructor
@Configuration
Expand All @@ -18,11 +19,13 @@ public class SecurityConfig {

private final AuthenticationManager authenticationManager;
private final JwtAuthenticationFilter jwtAuthenticationFilter;
private final LoggingFilter loggingFilter;

@Bean
SecurityFilterChain securityFilterChain(final HttpSecurity http) throws Exception {
http
.authenticationManager(authenticationManager)
.addFilterBefore(loggingFilter, DisableEncodeUrlFilter.class)
.addFilterAt(jwtAuthenticationFilter, BasicAuthenticationFilter.class)
.csrf().disable()
.authorizeHttpRequests()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.List;
import java.util.Objects;

@Profile({"local", "dev"})
@Profile("!test")
@Service
public class SendbirdService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.time.LocalDate;

@Profile({"local", "dev"})
@Profile("!test")
@Component
public class LocalTimeHolder implements TimeHolder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.UUID;

@Profile({"local", "dev"})
@Profile("!test")
@Service
public class RandomUuidHolder implements UuidHolder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.net.URL;
import java.util.Date;

@Profile({"local", "dev"})
@Profile("!test")
@Service
public class S3Service implements ImageService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import org.springframework.transaction.annotation.Transactional;

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

import static java.util.stream.Collectors.*;

@RequiredArgsConstructor
@Service
Expand Down Expand Up @@ -47,14 +50,19 @@ public MatchedMemberProfilesResponse read(final String uuid, final Long nextId,
}

SliceDto<MatchedMember> sliceDto = matchedMemberQueryRepository.findAllByFromMemberId(fromMember.getId(), nextId, pageSize);
List<String> preferredInterests = preferredInterestsRepository.findAllByMember(fromMember).stream()
.map(PreferredInterests::getInterestsName)
.toList();
List<MemberSimpleProfile> results = sliceDto.content().stream()
List<MemberSimpleProfile> results = createSimpleProfiles(sliceDto.content());
return new MatchedMemberProfilesResponse(sliceDto.nextId(), results);
}

private List<MemberSimpleProfile> createSimpleProfiles(final List<MatchedMember> matchedMembers) {
List<Member> toMembers = matchedMembers.stream()
.map(MatchedMember::getToMember)
.map(m -> MemberSimpleProfile.of(m, m.getProfileImageUri(), m.getRegion(), preferredInterests))
.toList();
return new MatchedMemberProfilesResponse(sliceDto.nextId(), results);
Map<Member, List<String>> map = preferredInterestsRepository.findAllByMemberIn(toMembers).stream()
.collect(groupingBy(PreferredInterests::getMember, mapping(PreferredInterests::getInterestsName, toList())));
return map.entrySet().stream()
.map(e -> MemberSimpleProfile.of(e.getKey(), e.getValue()))
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MatchedMemberRepository extends JpaRepository<MatchedMember, Long> {

@Modifying(clearAutomatically = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ public record MemberSimpleProfile(String uuid,
List<String> preferredInterests) {

public static MemberSimpleProfile of(final Member member,
final String profileImageUri,
final String region,
final List<String> preferredInterests) {
return new MemberSimpleProfile(
member.getUuid(),
profileImageUri,
member.getProfileImageUri(),
member.getName(),
member.getDescription(),
region,
member.getRegion(),
preferredInterests
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
@Entity
public class PreferredInterests extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
Member member;

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Embedded
private PreferredInterestsMember member;


@Embedded
private InterestsInfo interests;

@Builder
public PreferredInterests(Member member, Interests interests) {
this.member = new PreferredInterestsMember(member);
this.member = member;
this.interests = new InterestsInfo(interests);
}

Expand All @@ -39,4 +40,8 @@ public Long getInterestsCategoryId() {
return interests.getCategoryId();
}

public Member getMember() {
return member;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

public interface PreferredInterestsRepository extends JpaRepository<PreferredInterests, Long> {

@Query("select p from PreferredInterests p where p.member.member = :member")
List<PreferredInterests> findAllByMember(@Param("member") final Member member);

List<PreferredInterests> findAllByMemberIn(@Param("member") final List<Member> member);

@Modifying
@Transactional
@Query("delete from PreferredInterests p where p.member.member = :member")
@Query("delete from PreferredInterests p where p.member = :member")
void deleteAllByMember(@Param("member") final Member member);

}
2 changes: 1 addition & 1 deletion src/main/resources/security
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.lighthouse.lingoswap.auth.config;

import com.lighthouse.lingoswap.ControllerTestSupport;
import org.junit.jupiter.api.Test;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

class LoggingFilterTest extends ControllerTestSupport {

// user-agent:"PostmanRuntime/7.35.0", accept:"*/*", host:"localhost:8080", accept-encoding:"gzip, deflate, br", connection:"keep-alive"
@Test
void test() throws Exception {
mockMvc.perform(
get("/api/v1/question")
.queryParam("categoryId", "1")
.header("user-agent", "PostmanRuntime/7.35.0")
.header("host", "PostmanRuntime/7.35.0")
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void login() throws Exception {
// when & then
mockMvc.perform(
post("/api/v1/auth/login")
.param("id_token", ID_TOKEN))
.queryParam("id_token", ID_TOKEN))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("20000"))
Expand Down Expand Up @@ -76,7 +76,7 @@ void loginWithBadIdToken() throws Exception {
// when & then
mockMvc.perform(
post("/api/v1/auth/login")
.param("id_token", ID_TOKEN))
.queryParam("id_token", ID_TOKEN))
.andDo(print())
.andExpect(status().isForbidden())
.andExpect(jsonPath("$.code").value("40300"))
Expand All @@ -95,7 +95,7 @@ void loginWithNotExistedMemberIdToken() throws Exception {
// when & then
mockMvc.perform(
post("/api/v1/auth/login")
.param("id_token", ID_TOKEN))
.queryParam("id_token", ID_TOKEN))
.andDo(print())
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.code").value("40401"))
Expand All @@ -115,7 +115,7 @@ void signup() throws Exception {
// when & then
mockMvc.perform(
post("/api/v1/auth/signup")
.param("id_token", ID_TOKEN)
.queryParam("id_token", ID_TOKEN)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andDo(print())
Expand Down Expand Up @@ -152,7 +152,7 @@ void signupWithBadIdToken() throws Exception {
// when & then
mockMvc.perform(
post("/api/v1/auth/signup")
.param("id_token", ID_TOKEN)
.queryParam("id_token", ID_TOKEN)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andDo(print())
Expand Down Expand Up @@ -181,7 +181,7 @@ void signupWithBadRequest() throws Exception {
// when & then
mockMvc.perform(
post("/api/v1/auth/signup")
.param("id_token", ID_TOKEN)
.queryParam("id_token", ID_TOKEN)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andDo(print())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.lighthouse.lingoswap.match.application;

import com.lighthouse.lingoswap.IntegrationTestSupport;

class MatchManagerTest extends IntegrationTestSupport {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.lighthouse.lingoswap.match.presentation;

import com.lighthouse.lingoswap.ControllerTestSupport;

class MatchControllerTest extends ControllerTestSupport {

}
Loading

0 comments on commit 60c4b2c

Please sign in to comment.