Skip to content

Commit

Permalink
Merge pull request #45 from soma-lighthouse/enhance/errorCorrection-L…
Browse files Browse the repository at this point in the history
…H-195

Enhance/error correction lh 195
  • Loading branch information
hyeonjun-An authored Sep 21, 2023
2 parents 51192f4 + 73e2702 commit f3011c2
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EnableJpaAuditing
public class LingoSwapApplication {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package com.lighthouse.lingoswap.match.api;

import com.lighthouse.lingoswap.common.dto.ResponseDto;
import com.lighthouse.lingoswap.match.dto.MatchedMemberProfilesResponse;
import com.lighthouse.lingoswap.match.service.MatchManager;
import com.lighthouse.lingoswap.member.dto.MemberSimpleProfile;
import com.lighthouse.lingoswap.member.entity.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
Expand All @@ -22,12 +16,11 @@ public class MatchController {
private final MatchManager matchManager;

@Transactional
@GetMapping("/{user_id}/matches")
public ResponseEntity<Slice<MemberSimpleProfile>> get(@PathVariable final String uuid
, @PageableDefault(size = 10, sort = "id") Pageable pageable) {
Slice<Member> slice = matchManager.read(uuid, pageable);
Slice<MemberSimpleProfile> sliceDto = slice.map(MemberSimpleProfile::from);
@GetMapping(path = "/{uuid}/matches")
public ResponseEntity<ResponseDto<MatchedMemberProfilesResponse>> get(@PathVariable final String uuid,
@RequestParam(required = false) Long next,
@RequestParam(defaultValue = "10") final int pageSize) {

return ResponseEntity.ok(sliceDto);
return ResponseEntity.ok(matchManager.read(uuid, next, pageSize));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package com.lighthouse.lingoswap.match.repository;

import com.lighthouse.lingoswap.common.dto.SliceDto;
import com.lighthouse.lingoswap.match.entity.MatchedMember;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.lighthouse.lingoswap.match.entity.QMatchedMember.matchedMember;
import static com.lighthouse.lingoswap.member.entity.QMember.member;

@Repository
public class MatchedMemberQueryRepository {
/*

private final JPAQueryFactory queryFactory;

public MatchedMemberQueryRepository(EntityManager em) {
Expand Down Expand Up @@ -38,6 +48,6 @@ private Long removeLastAndReturnNextIdByPageSize(final List<MatchedMember> match
matchedMembers.remove(pageSize);
lastId = matchedMembers.get(pageSize - 1).getId();
}
return lastId;*/

return lastId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,47 @@
public interface MatchedMemberRepository extends JpaRepository<MatchedMember, Long> {

@Modifying(clearAutomatically = true)
@Query(value = "DELETE FROM filtered_member as mm WHERE mm.from_member_id = :memberId", nativeQuery = true)
void deletePreviousFilteredMember(@Param("memberId") Long memberId);
@Query(value = "DELETE FROM matched_member as mm WHERE mm.from_member_id = :memberId", nativeQuery = true)
void deletePreviousMatchedMember(@Param("memberId") Long memberId);

@Modifying(clearAutomatically = true)
@Query(value = """
INSERT INTO filtered_member(from_member_id, to_member_id)
INSERT INTO matched_member(from_member_id, to_member_id)
SELECT :memberId, sub.mm_id
FROM
(
SELECT
mm.to_member_id AS mm_id,
m.id AS mm_id,
IF(m.region_id in (:preferredCountryIds), 20,0) AS score
FROM matched_member mm
JOIN Member m ON mm.to_member_id = m.id
FROM Member m
LEFT JOIN preferred_country pc ON m.id = pc.member_id
WHERE mm.from_member_id = :memberId
WHERE m.id != :memberId
UNION ALL
SELECT
mm.to_member_id AS mm_id,
m.id AS mm_id,
IF(ul.language_id IN (:languageIds), 5, 0) AS score
FROM matched_Member mm
JOIN Member m ON mm.to_member_id = m.id
JOIN used_language ul ON m.id = ul.member_id
WHERE mm.from_member_id = :memberId
FROM Member m
LEFT JOIN used_language ul ON m.id = ul.member_id
WHERE m.id != :memberId
UNION ALL
SELECT
mm.to_member_id AS mm_id,
m.id AS mm_id,
IF(i.category_id IN (:categoryIds) ,1 ,0) AS score
FROM matched_member mm
JOIN Member m ON mm.to_member_id = m.id
JOIN preferred_interests pi ON m.id = pi.member_id
JOIN interests i ON pi.interests_id = i.id
WHERE mm.from_member_id = :memberId
FROM Member m
LEFT JOIN preferred_interests pi ON m.id = pi.member_id
LEFT JOIN interests i ON pi.interests_id = i.id
WHERE m.id != :memberId
) AS sub
GROUP BY sub.mm_id
ORDER BY SUM(sub.score) DESC
""", nativeQuery = true)
void saveFilteredMembersWithPreferences(
void saveMatchedMembersWithPreferences(
@Param("memberId") Long memberId,
@Param("preferredCountryIds") List<Long> preferredCountryIds,
@Param("languageIds") List<Long> preferredLanguages,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.lighthouse.lingoswap.match.service;

import com.lighthouse.lingoswap.match.entity.FilteredMember;
import com.lighthouse.lingoswap.common.dto.ResponseDto;
import com.lighthouse.lingoswap.common.dto.SliceDto;
import com.lighthouse.lingoswap.match.dto.MatchedMemberProfilesResponse;
import com.lighthouse.lingoswap.match.entity.MatchedMember;
import com.lighthouse.lingoswap.member.dto.MemberSimpleProfile;
import com.lighthouse.lingoswap.member.entity.*;
import com.lighthouse.lingoswap.member.service.MemberService;
import com.lighthouse.lingoswap.member.service.PreferredCountryService;
import com.lighthouse.lingoswap.member.service.PreferredInterestsService;
import com.lighthouse.lingoswap.member.service.UsedLanguageService;
import com.lighthouse.lingoswap.question.entity.Category;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -19,15 +21,14 @@
public class MatchManager {

private final MatchService matchService;
private final FilterService filterService;
private final MemberService memberService;
private final PreferredCountryService preferredCountryService;
private final UsedLanguageService usedLanguageService;
private final PreferredInterestsService preferredInterestsService;

public Slice<Member> read(final String uuid, Pageable pageable) {
public ResponseDto<MatchedMemberProfilesResponse> read(final String uuid, final Long nextId, final int pageSize) {
Member fromMember = memberService.findByUuid(uuid);
if (pageable.getPageNumber() == 0) {
if (nextId == null) {
List<Long> usedLanguageIds = usedLanguageService.findByMember(fromMember)
.stream()
.map(UsedLanguage::getId)
Expand All @@ -45,12 +46,13 @@ public Slice<Member> read(final String uuid, Pageable pageable) {
.map(Category::getId)
.toList();

matchService.saveFilteredMembersWithPreferences(
matchService.saveMatchedMembersWithPreferences(
fromMember.getId(), preferredCountryIds, usedLanguageIds, categoryIds);
}

Slice<FilteredMember> filteredMembers = filterService.findFilteredMembers(fromMember, pageable);
return filteredMembers.map(FilteredMember::getToMember);
SliceDto<MatchedMember> matchedMembers = matchService.findFilteredMembers(fromMember.getId(), nextId, pageSize);
List<MemberSimpleProfile> results = matchedMembers.content().stream().map(MatchedMember::getToMember).map(MemberSimpleProfile::from).toList();
return ResponseDto.success(new MatchedMemberProfilesResponse(matchedMembers.nextId(), results));
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lighthouse.lingoswap.match.service;

import com.lighthouse.lingoswap.common.dto.SliceDto;
import com.lighthouse.lingoswap.match.entity.MatchedMember;
import com.lighthouse.lingoswap.match.repository.MatchedMemberQueryRepository;
import com.lighthouse.lingoswap.match.repository.MatchedMemberRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,14 +17,18 @@ public class MatchService {
private final MatchedMemberQueryRepository matchedMemberQueryRepository;
private final MatchedMemberRepository matchedMemberRepository;

public SliceDto<MatchedMember> findFilteredMembers(Long memberId, Long nextId, int pageSize) {
return matchedMemberQueryRepository.findAllByFromMemberId(memberId, nextId, pageSize);
}

@Transactional
public void saveFilteredMembersWithPreferences(
public void saveMatchedMembersWithPreferences(
Long memberId,
List<Long> preferredCountryIds,
List<Long> preferredLanguages,
List<Long> preferredInterests) {
matchedMemberRepository.deletePreviousFilteredMember(memberId);
matchedMemberRepository.saveFilteredMembersWithPreferences(
matchedMemberRepository.deletePreviousMatchedMember(memberId);
matchedMemberRepository.saveMatchedMembersWithPreferences(
memberId, preferredCountryIds, preferredLanguages, preferredInterests);
}

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

import java.util.List;

public record MemberProfileResponse(Long id,
public record MemberProfileResponse(String uuid,
String profileImageUri,
String name,
int age,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ResponseDto<MemberProfileResponse> read(final String uuid, Locale locale)
Member member = memberService.findByUuidWithRegionAndUsedLanguage(uuid);
Map<String, List<String>> interestsMap = groupInterestsByCategory(preferredInterestsService.findAllByMemberIdWithInterestsAndCategory(member.getId()));
List<UsedLanguage> usedLanguages = member.getUsedLanguages();
return ResponseDto.success(new MemberProfileResponse(member.getId(), distributionService.generateUri(profileKeyPrefix + member.getProfileImageUri()), member.getName(), member.calculateAge(), member.getDescription(), member.getRegion().getCode(),
return ResponseDto.success(new MemberProfileResponse(member.getAuthDetails().getUuid(), distributionService.generateUri(profileKeyPrefix + member.getProfileImageUri()), member.getName(), member.calculateAge(), member.getDescription(), member.getRegion().getCode(),
preferredCountryService.findAllByMemberIdWithCountry(member.getId()).stream().map(c -> new CountryFormResponseUnit(c.getCountry().getCode(), messageSource.getMessage(c.getCountry().getCode(), null, locale))).toList(),
usedLanguages.stream().map(MemberUsedLanguage::from).toList(),
interestsMap.entrySet().stream().map(entry -> MemberPreferredInterests.of(new CategoryDto(entry.getKey(), messageSource.getMessage(entry.getKey(), null, locale)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public ResponseEntity<ResponseDto<Object>> deleteLike(@PathVariable Long questio
}

@GetMapping(path = "/recommendation/{categoryId}")
public ResponseEntity<QuestionRecommendationListResponse> getRecommendation(@PathVariable Long categoryId,
@RequestParam(required = false) final Long next,
@RequestParam(defaultValue = "10") final int pageSize) {
public ResponseEntity<ResponseDto<QuestionRecommendationListResponse>> getRecommendation(@PathVariable Long categoryId,
@RequestParam(required = false) final Long next,
@RequestParam(defaultValue = "10") final int pageSize) {
return ResponseEntity.ok(questionManager.readRecommendation(categoryId, next, pageSize));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.lighthouse.lingoswap.question.dto;

public record QuestionDeleteLikeRequest(String userId) {
public record QuestionDeleteLikeRequest(String uuid) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.lighthouse.lingoswap.member.entity.Member;
import com.lighthouse.lingoswap.question.entity.Question;

public record QuestionDetail(Long questionId, Long userId, String profileImageUri, String name, String region,
public record QuestionDetail(Long questionId, String uuid, String profileImageUri, String name, String region,
String contents, Integer likes, boolean clicked) {

public static QuestionDetail of(Question question, Member member, boolean clicked) {
return new QuestionDetail(question.getId(), member.getId(), member.getProfileImageUri(), member.getName(), member.getRegion().getCode(), question.getContents(), question.getLikes(), clicked);
return new QuestionDetail(question.getId(), member.getAuthDetails().getUuid(), member.getProfileImageUri(), member.getName(), member.getRegion().getCode(), question.getContents(), question.getLikes(), clicked);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.lighthouse.lingoswap.question.dto;

public record QuestionUpdateLikeRequest(String userId) {
public record QuestionUpdateLikeRequest(String uuid) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ResponseDto<QuestionListResponse> read(final String uuid, final Long cate
@Transactional
public ResponseDto<Object> updateLike(final Long questionId, final QuestionUpdateLikeRequest questionUpdateLikeRequest) {
Question question = questionService.findById(questionId);
Member member = memberService.findByUuid(questionUpdateLikeRequest.userId());
Member member = memberService.findByUuid(questionUpdateLikeRequest.uuid());

LikeMember likeMember = LikeMember.of(member, question);
likeMemberService.save(likeMember);
Expand All @@ -59,19 +59,19 @@ public ResponseDto<Object> updateLike(final Long questionId, final QuestionUpdat
@Transactional
public ResponseDto<Object> deleteLike(final Long questionId, final QuestionDeleteLikeRequest questionDeleteLikeRequest) {
Question question = questionService.findById(questionId);
Member member = memberService.findByUuid(questionDeleteLikeRequest.userId());
Member member = memberService.findByUuid(questionDeleteLikeRequest.uuid());
likeMemberService.deleteByQuestionAndMember(question, member);

question.subtractOneLike();
questionService.save(question);
return ResponseDto.success(null);
}

public QuestionRecommendationListResponse readRecommendation(Long categoryId, Long nextId, int pageSize) {
public ResponseDto<QuestionRecommendationListResponse> readRecommendation(Long categoryId, Long nextId, int pageSize) {
SliceDto<Question> questionRecommendations = questionService.searchRecommendation(categoryId, nextId, pageSize);

List<String> results = new ArrayList<>();
questionRecommendations.content().stream().forEach(q -> results.add(q.getContents()));
return new QuestionRecommendationListResponse(questionRecommendations.nextId(), results);
return ResponseDto.success(new QuestionRecommendationListResponse(questionRecommendations.nextId(), results));
}
}

0 comments on commit f3011c2

Please sign in to comment.