Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(*): 북마크 조회 시 다른 사람의 북마크가 보이는 현상 수정 #72

Merged
merged 5 commits into from
Sep 14, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ResponseEntity<Void> reportProblem(
@GetMapping("/bookmarked")
public ResponseEntity<FindBookmarkedProblemsResponse> findBookmarkedProblems(
@AuthenticatedMember AuthMember member,
@RequestParam(name = "exam-id") Long examId,
@RequestParam(name = "exam-id", required = false) Long examId,
@RequestParam(name = "subject-id") List<Long> subjectIds,
int page
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
@Getter
public enum AnalysisErrorCode implements ErrorCode {

CANNOT_CALCULATE_VULNERABILITY("취약점 분석을 할 수 없습니다.", "LRN_001", ErrorCode.INTERNAL_SERVER_ERROR),
CANNOT_FIND_VECTOR("벡터를 찾을 수 없습니다.", "LRN_002", ErrorCode.INTERNAL_SERVER_ERROR),
CANNOT_CALCULATE_VULNERABILITY("취약점 분석을 할 수 없습니다.", "ANA_001", ErrorCode.INTERNAL_SERVER_ERROR),
CANNOT_FIND_VECTOR("벡터를 찾을 수 없습니다.", "ANA_002", ErrorCode.INTERNAL_SERVER_ERROR),
NOT_ENOUGH_SOLVED_PROBLEMS("문제를 충분히 풀지 않아서 분석할 수 없습니다.", "ANA_003", ErrorCode.BAD_REQUEST),
;

private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public List<Long> findVulnerableProblems(Member member, Certificate certificate)
private List<Float> findVulnerableVector(Member member, Certificate certificate) {
LocalDateTime oneYearAgo = LocalDateTime.now().minusYears(YEARS_OF_ANALYSIS);
// TODO: 어떤 쿼리가 더 효율적인지 테스트 필요
List<ProblemSolving> problemSolvings = problemSolvingRepository.findByMemberAndLearning_CertificateAndLearning_CreatedAtAfter(member, certificate, oneYearAgo);
List<ProblemSolving> problemSolvings = problemSolvingRepository.findByMemberAndCertificateAndCreatedAtAfterWithLearning(member, certificate, oneYearAgo);

if (problemSolvings.isEmpty()) {
throw new AnalysisBusinessException(AnalysisErrorCode.NOT_ENOUGH_SOLVED_PROBLEMS);
}

List<Long> distinctProblemIds = problemSolvings.stream()
.map(problemSolving -> problemSolving.getProblem().getId())
Expand All @@ -76,7 +80,6 @@ List<Float> calculateVulnerableVector(List<ProblemSolving> problemSolvings, Map<
.map(value -> (float) (value * weight))
.toList();
})
.parallel()
.reduce((vector1, vector2) ->
IntStream.range(0, vector1.size())
.mapToObj(i -> vector1.get(i) + vector2.get(i))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Learning {

@Id
@Tsid
@Column(name = "learning-id")
@Column(name = "learning_id")
private Long id;

@Enumerated(EnumType.STRING)
Expand All @@ -35,11 +35,11 @@ public class Learning {
private LocalDateTime createdAt;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member-id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
@JoinColumn(name = "member_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "certificate-id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
@JoinColumn(name = "certificate_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
private Certificate certificate;

@OneToMany(mappedBy = "learning")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.jabiseo.certificate.domain.Certificate;
import com.jabiseo.member.domain.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDateTime;
import java.util.List;

public interface ProblemSolvingRepository extends JpaRepository<ProblemSolving, Long> {

List<ProblemSolving> findByMemberAndLearning_CertificateAndLearning_CreatedAtAfter(Member member, Certificate learning_certificate, LocalDateTime createdAt);
@Query("select ps from ProblemSolving ps join fetch ps.learning l where ps.member = :member and l.certificate = :learning_certificate and l.createdAt > :createdAt")
List<ProblemSolving> findByMemberAndCertificateAndCreatedAtAfterWithLearning(Member member, Certificate learning_certificate, LocalDateTime createdAt);

}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Page<ProblemWithBookmarkSummaryQueryDto> findBookmarkedSummaryByExamIdAnd
.join(problem.exam, exam)
.join(problem.subject, subject)
.join(bookmark).on(bookmark.problem.id.eq(problem.id))
.where(examIdEq(examId), subjectIdsIn(subjectIds))
.where(memberIdEq(memberId), examIdEq(examId), subjectIdsIn(subjectIds))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
Expand Down Expand Up @@ -203,6 +203,10 @@ private BooleanExpression examIdEq(Long examId) {
return examId != null ? exam.id.eq(examId) : null;
}

private BooleanExpression memberIdEq(Long memberId) {
return memberId != null ? bookmark.member.id.eq(memberId) : null;
}

private Predicate isBookmarkedByMember(Long memberId, NumberPath<Long> id) {
if (memberId == null) {
return Expressions.FALSE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Map<Long, List<Float>> fetchVectors(List<Long> ids, String indexName, Str

private GetResponse<JsonData> fetchFromOpenSearch(Long id, String indexName, String vectorName) {
try {
return openSearchClient.get(GetRequest.of(getReq -> getReq
return openSearchClient.get(GetRequest.of(request -> request
.index(indexName)
.id(String.valueOf(id))
.sourceIncludes(vectorName)
Expand Down
Loading