diff --git a/src/main/java/corecord/dev/domain/ability/application/AbilityServiceImpl.java b/src/main/java/corecord/dev/domain/ability/application/AbilityServiceImpl.java index 5a533a0..ccf951b 100644 --- a/src/main/java/corecord/dev/domain/ability/application/AbilityServiceImpl.java +++ b/src/main/java/corecord/dev/domain/ability/application/AbilityServiceImpl.java @@ -71,9 +71,8 @@ public void parseAndSaveAbilities(Map keywordList, Analysis anal Ability ability = AbilityConverter.toAbility(keyword, entry.getValue(), analysis, user); abilityDbService.saveAbility(ability); + analysis.addAbility(ability); - if (analysis.getAbilityList() != null) - analysis.addAbility(ability); abilityCount++; } validAbilityCount(abilityCount); diff --git a/src/main/java/corecord/dev/domain/analysis/domain/entity/Analysis.java b/src/main/java/corecord/dev/domain/analysis/domain/entity/Analysis.java index 0dc0ab9..58dda41 100644 --- a/src/main/java/corecord/dev/domain/analysis/domain/entity/Analysis.java +++ b/src/main/java/corecord/dev/domain/analysis/domain/entity/Analysis.java @@ -10,6 +10,7 @@ import lombok.NoArgsConstructor; import org.hibernate.annotations.BatchSize; +import java.util.ArrayList; import java.util.List; @Entity @@ -37,8 +38,8 @@ public class Analysis extends BaseEntity { private Record record; @BatchSize(size = 3) - @OneToMany(mappedBy = "analysis", cascade = CascadeType.ALL, fetch = FetchType.LAZY) - private List abilityList; + @OneToMany(mappedBy = "analysis", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + private List abilityList = new ArrayList<>(); public void updateContent(String content) { if (content != null && !content.isEmpty()) @@ -52,9 +53,11 @@ public void updateComment(String comment) { } public void addAbility(Ability ability) { + if (abilityList == null) + abilityList = new ArrayList<>(); + if (ability != null) { this.abilityList.add(ability); } } - } diff --git a/src/main/java/corecord/dev/domain/record/application/RecordDbService.java b/src/main/java/corecord/dev/domain/record/application/RecordDbService.java index d3fbf6e..e03ec2f 100644 --- a/src/main/java/corecord/dev/domain/record/application/RecordDbService.java +++ b/src/main/java/corecord/dev/domain/record/application/RecordDbService.java @@ -43,8 +43,12 @@ public void deleteRecordByFolder(Folder folder) { recordRepository.deleteRecordByFolder(folder); } - public int getRecordCount(User user) { - return recordRepository.getRecordCount(user); + public int getRecordCount(Long userId) { + return recordRepository.getRecordCount(userId); + } + + public int getRecordCountByChatType(Long userId) { + return recordRepository.getRecordCountByType(userId); } @Transactional diff --git a/src/main/java/corecord/dev/domain/record/application/RecordServiceImpl.java b/src/main/java/corecord/dev/domain/record/application/RecordServiceImpl.java index 22b402e..5d3dd88 100644 --- a/src/main/java/corecord/dev/domain/record/application/RecordServiceImpl.java +++ b/src/main/java/corecord/dev/domain/record/application/RecordServiceImpl.java @@ -4,6 +4,7 @@ import corecord.dev.domain.ability.exception.AbilityException; import corecord.dev.domain.ability.status.AbilityErrorStatus; import corecord.dev.domain.analysis.application.AnalysisService; +import corecord.dev.domain.analysis.domain.entity.Analysis; import corecord.dev.domain.chat.application.ChatDbService; import corecord.dev.domain.chat.domain.entity.ChatRoom; import corecord.dev.domain.folder.application.FolderDbService; @@ -37,13 +38,13 @@ public class RecordServiceImpl implements RecordService { private final int listSize = 30; /** - * user의 MEMO ver. 경험을 기록하고 폴더를 지정한 후 생성된 경험 기록 정보를 반환합니다. + * user의 경험을 기록하고 폴더를 지정한 후 생성된 경험 기록 정보를 반환합니다. * * @param userId, recordDto * @return recordId, title, content, folderName, createdAt */ @Override - public RecordResponse.MemoRecordDto createMemoRecord(Long userId, RecordRequest.RecordDto recordDto) { + public RecordResponse.RecordAnalysisDto createRecord(Long userId, RecordRequest.RecordDto recordDto) { User user = userDbService.findUserById(userId); String title = recordDto.getTitle(); String content = recordDto.getContent(); @@ -56,10 +57,12 @@ public RecordResponse.MemoRecordDto createMemoRecord(Long userId, RecordRequest. Record record = createRecordBasedOnType(recordDto, user, folder); // 역량 분석 레포트 생성 - analysisService.createAnalysis(record, user); + Analysis analysis = analysisService.createAnalysis(record, user); recordDbService.saveRecord(record); - return RecordConverter.toMemoRecordDto(record); + // Chat 경험 기록 개수 카운트 + int chatRecordCount = getChatRecordCount(record, userId); + return RecordConverter.toRecordAnalysisDto(analysis, chatRecordCount); } private Record createRecordBasedOnType(RecordRequest.RecordDto recordDto, User user, Folder folder) { @@ -70,6 +73,12 @@ private Record createRecordBasedOnType(RecordRequest.RecordDto recordDto, User u return RecordConverter.toChatRecordEntity(recordDto.getTitle(), recordDto.getContent(), user, folder, chatRoom); } + private int getChatRecordCount(Record record, Long userId) { + return record.isMemoType() + ? 0 + : recordDbService.getRecordCountByChatType(userId); + } + /** * recordId를 받아 MEMO ver. 경험 기록의 상세 정보를 반환합니다. * diff --git a/src/main/java/corecord/dev/domain/record/domain/repository/RecordRepository.java b/src/main/java/corecord/dev/domain/record/domain/repository/RecordRepository.java index f816384..04233b8 100644 --- a/src/main/java/corecord/dev/domain/record/domain/repository/RecordRepository.java +++ b/src/main/java/corecord/dev/domain/record/domain/repository/RecordRepository.java @@ -69,9 +69,16 @@ List findRecordsByKeyword( @Query("SELECT COUNT(r) " + "FROM Record r " + - "WHERE r.user = :user " + + "WHERE r.user.userId = :userId " + "AND r.folder is not null") // 임시 저장 기록 제외 - int getRecordCount(@Param(value = "user") User user); + int getRecordCount(@Param(value = "userId") Long userId); + + @Query("SELECT COUNT(r) " + + "FROM Record r " + + "WHERE r.user.userId = :userId " + + "AND r.folder is not null " + + "AND r.type = 'CHAT' ") + int getRecordCountByType(@Param(value = "userId") Long userId); @Modifying @Query("DELETE " + diff --git a/src/test/java/corecord/dev/user/service/UserServiceTest.java b/src/test/java/corecord/dev/user/service/UserServiceTest.java index 0a0890f..1ecade9 100644 --- a/src/test/java/corecord/dev/user/service/UserServiceTest.java +++ b/src/test/java/corecord/dev/user/service/UserServiceTest.java @@ -82,7 +82,7 @@ void registerUser() { void getUserInfo() { // Given when(userDbService.getUser(newUser.getUserId())).thenReturn(newUser); - when(recordDbService.getRecordCount(newUser)).thenReturn(0); + when(recordDbService.getRecordCount(newUser.getUserId())).thenReturn(0); // When UserResponse.UserInfoDto userInfoDto = userService.getUserInfo(newUser.getUserId());