Skip to content

Commit

Permalink
Merge pull request #36 from KUSITMS-CORECORD/feat/#35
Browse files Browse the repository at this point in the history
[Feat/#35] 역량 키워드 리스트 조회 기능 구현
  • Loading branch information
daeun084 authored Oct 31, 2024
2 parents e10aa6e + 05538a9 commit ac8092e
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
public enum AnalysisSuccessStatus implements BaseSuccessStatus {
ANALYSIS_GET_SUCCESS(HttpStatus.CREATED, "S502", "역량별 경험 조회가 성공적으로 완료되었습니다."),
ANALYSIS_UPDATE_SUCCESS(HttpStatus.OK, "S701", "역량별 경험 수정이 성공적으로 완료되었습니다."),
ANALYSIS_DELETE_SUCCESS(HttpStatus.OK, "S702", "역량별 경험 삭제가 성공적으로 완료되었습니다.");
ANALYSIS_DELETE_SUCCESS(HttpStatus.OK, "S702", "역량별 경험 삭제가 성공적으로 완료되었습니다."),
KEYWORD_LIST_GET_SUCCESS(HttpStatus.OK, "S505", "역량 키워드 리스트 조회가 성공적으로 완료되었습니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ public ResponseEntity<ApiResponse<String>> deleteAnalysis(
return ApiResponse.success(AnalysisSuccessStatus.ANALYSIS_DELETE_SUCCESS);
}

@GetMapping("/keyword")
public ResponseEntity<ApiResponse<AnalysisResponse.KeywordListDto>> getKeywordList(
@UserId Long userId
) {
AnalysisResponse.KeywordListDto analysisResponse = analysisService.getKeywordList(userId);
return ApiResponse.success(AnalysisSuccessStatus.KEYWORD_LIST_GET_SUCCESS, analysisResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ public static AnalysisResponse.AnalysisDto toAnalysisDto(Analysis analysis) {
.createdAt(analysis.getCreatedAtFormatted())
.build();
}

public static AnalysisResponse.KeywordListDto toKeywordListDto(List<String> keywordList) {
return AnalysisResponse.KeywordListDto.builder()
.keywordList(keywordList)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public static class AnalysisDto {
private String comment;
private String createdAt;
}

@Builder
@Getter
@AllArgsConstructor
@Data
public static class KeywordListDto {
private List<String> keywordList;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
package corecord.dev.domain.analysis.repository;

import corecord.dev.domain.analysis.constant.Keyword;
import corecord.dev.domain.analysis.entity.Analysis;
import corecord.dev.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

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

@Repository
public interface AnalysisRepository extends JpaRepository<Analysis, Long> {

@Query("SELECT a " +
"FROM Analysis a " +
"JOIN FETCH a.record r " +
"JOIN FETCH a.abilityList al " +
"WHERE a.analysisId = :id")
Optional<Analysis> findAnalysisById(@Param(value = "id") Long id);

@Query("SELECT distinct a.keyword AS keyword " + // unique한 keyword list 반환
"FROM Ability a " +
"JOIN a.analysis ana " +
"WHERE a.user = :user " +
"GROUP BY a.keyword " +
"ORDER BY COUNT(a.keyword) DESC, MAX(ana.createdAt) DESC ") // 개수가 많은 순, 최근 생성 순 정렬
List<Keyword> getKeywordList(@Param(value = "user") User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Transactional(readOnly = true)
Expand Down Expand Up @@ -106,6 +107,19 @@ public void deleteAnalysis(Long userId, Long analysisId) {
analysisRepository.delete(analysis);
}

/*
* user의 역량 키워드 리스트를 반환
* @param userId
* @return
*/
@Transactional(readOnly = true)
public AnalysisResponse.KeywordListDto getKeywordList(Long userId) {
User user = findUserById(userId);
List<String> keywordList = findKeywordList(user);

return AnalysisConverter.toKeywordListDto(keywordList);
}

private void validIsUserAuthorizedForAnalysis(User user, Analysis analysis) {
if (!analysis.getRecord().getUser().equals(user))
throw new RecordException(RecordErrorStatus.USER_RECORD_UNAUTHORIZED);
Expand All @@ -127,13 +141,19 @@ private Ability findAbilityByKeyword(Analysis analysis, Keyword key) {
.orElseThrow(() -> new AnalysisException(AnalysisErrorStatus.INVALID_KEYWORD));
}

private List<String> findKeywordList(User user) {
return analysisRepository.getKeywordList(user).stream()
.map(Keyword::getValue)
.toList();
}

private User findUserById(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new GeneralException(ErrorStatus.UNAUTHORIZED));
}

private Analysis findAnalysisById(Long analysisId) {
return analysisRepository.findById(analysisId)
return analysisRepository.findAnalysisById(analysisId)
.orElseThrow(() -> new AnalysisException(AnalysisErrorStatus.ANALYSIS_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.stereotype.Repository;

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

@Repository
public interface RecordRepository extends JpaRepository<Record, Long> {
Expand Down Expand Up @@ -44,7 +45,13 @@ List<Record> findRecordsByFolder(
"AND a.keyword = :keyword " +
"AND r.folder is not null " + // 임시 저장 기록 제외
"ORDER BY r.createdAt DESC") // 최근 생성 순 정렬
List<Record> findRecordByKeyword(
List<Record> findRecordsByKeyword(
@Param(value = "keyword")Keyword keyword,
@Param(value = "user") User user);

@Query("SELECT r FROM Record r " +
"JOIN FETCH r.analysis a " +
"JOIN FETCH r.folder f " +
"WHERE r.recordId = :id")
Optional<Record> findRecordById(@Param(value = "id") Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private User findUserById(Long userId) {
}

private Record findRecordById(Long recordId) {
return recordRepository.findById(recordId)
return recordRepository.findRecordById(recordId)
.orElseThrow(() -> new RecordException(RecordErrorStatus.RECORD_NOT_FOUND));
}

Expand All @@ -209,7 +209,7 @@ private List<Record> getRecordList(User user) {
}

private List<Record> getRecordListByKeyword(User user, Keyword keyword) {
return recordRepository.findRecordByKeyword(keyword, user);
return recordRepository.findRecordsByKeyword(keyword, user);
}

private Keyword getKeyword(String keywordValue) {
Expand Down

0 comments on commit ac8092e

Please sign in to comment.