Skip to content

Commit

Permalink
Merge pull request #107 from studio-recoding/refact-main-api
Browse files Browse the repository at this point in the history
[🎨refact] 메인 API를 비동기로 변경
  • Loading branch information
JeonHaeseung authored Jun 4, 2024
2 parents 9a6f96b + 975d38c commit 94b63a7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
27 changes: 24 additions & 3 deletions src/main/java/Ness/Backend/domain/main/MainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,43 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Service
@RequiredArgsConstructor
@Transactional
public class MainService {
private final ReportService reportService;
private final TodoService todoService;

public GetMainDto getMain(Long memberId){
List<GetScheduleDto> scheduleDtos = todoService.getTodo(memberId);
PostFastApiAiRecommendActivityDto activityDtos = reportService.getRecommendActivity(memberId);
try {
CompletableFuture<List<GetScheduleDto>> scheduleFuture = CompletableFuture.supplyAsync(() -> todoService.getTodo(memberId));
CompletableFuture<PostFastApiAiRecommendActivityDto> activityFuture = CompletableFuture.supplyAsync(() -> reportService.getRecommendActivity(memberId));
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(scheduleFuture, activityFuture);

// 두 작업이 모두 완료될 때까지 기다린다.(각각 진행되는 것은 맞음, 가장 늦은 작업 완료에 맞춤)
combinedFuture.join();
List<GetScheduleDto> scheduleDtos = scheduleFuture.get();
PostFastApiAiRecommendActivityDto activityDtos = activityFuture.get();
return toEntity(scheduleDtos, activityDtos);

}
// 비동기 에러 발생시 동기 처리를 하더라도 메인 API 제공되도록 로직 설정
catch (Exception exception){
List<GetScheduleDto> scheduleDtos = todoService.getTodo(memberId);
PostFastApiAiRecommendActivityDto activityDtos = reportService.getRecommendActivity(memberId);
return toEntity(scheduleDtos, activityDtos);
}
}

//Entity -> DTO
public GetMainDto toEntity(List<GetScheduleDto> scheduleDtos, PostFastApiAiRecommendActivityDto activityDtos){
return GetMainDto.builder()
.recommendText(activityDtos.getAnswer())
.activityList(activityDtos.getActivityList())
.scheduleList(scheduleDtos)
.build();
}

}
70 changes: 49 additions & 21 deletions src/main/java/Ness/Backend/domain/report/ReportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import Ness.Backend.domain.member.MemberRepository;
import Ness.Backend.domain.member.entity.Member;
import Ness.Backend.domain.profile.ProfileRepository;
import Ness.Backend.domain.profile.entity.PersonaType;
import Ness.Backend.domain.profile.entity.Profile;
import Ness.Backend.domain.report.dto.request.PostFastApiUserMemoryDto;
import Ness.Backend.domain.report.dto.request.PostFastApiUserRecommendActivityDto;
import Ness.Backend.domain.report.dto.request.PostFastApiUserTagDto;
Expand Down Expand Up @@ -37,19 +40,22 @@ public class ReportService {
private final FastApiTagApi fastApiTagApi;
private final FastApiMemoryApi fastApiMemoryApi;
private final MemberRepository memberRepository;
private final ProfileRepository profileRepository;

/* 메모리 가져오는 로직 */
public GetReportMemoryListDto getMemory(Long id){
public GetReportMemoryListDto getMemory(Long memberId){
log.info("getMemory called by member: " + memberId);

// 오늘 날짜 가져오기
ZonedDateTime now = getToday();

ReportMemory reportMemory = reportMemoryRepository.findTodayReportMemoryByMember_Id(id);
ReportMemory reportMemory = reportMemoryRepository.findTodayReportMemoryByMember_Id(memberId);

if (reportMemory == null){
// 오늘치가 없다면 새롭게 생성하기
String memory = postNewAiMemory(id, now);
String memory = postNewAiMemory(memberId, now);

Member memberEntity = memberRepository.findMemberById(id);
Member memberEntity = memberRepository.findMemberById(memberId);

ReportMemory newMemory = ReportMemory.builder()
.createdDate(now)
Expand All @@ -61,7 +67,7 @@ public GetReportMemoryListDto getMemory(Long id){
}

// 2주치의 데이터 가져오기
List<ReportMemory> reportMemories = reportMemoryRepository.findTwoWeekUserMemoryByMember_Id(id);
List<ReportMemory> reportMemories = reportMemoryRepository.findTwoWeekUserMemoryByMember_Id(memberId);
return createReportMemoryListDto(reportMemories);
}

Expand All @@ -78,10 +84,11 @@ public GetReportMemoryListDto createReportMemoryListDto(List<ReportMemory> repor
return new GetReportMemoryListDto(getReportMemoryDtos);
}

public String postNewAiMemory(Long id, ZonedDateTime today){
public String postNewAiMemory(Long memberId, ZonedDateTime today){
String persona = getPersona(memberId);
PostFastApiUserMemoryDto userDto = PostFastApiUserMemoryDto.builder()
.member_id(id.intValue())
.user_persona("")
.member_id(memberId.intValue())
.user_persona(persona)
.schedule_datetime_start(today)
.schedule_datetime_end(today)
.build();
Expand All @@ -93,16 +100,18 @@ public String postNewAiMemory(Long id, ZonedDateTime today){
}

/* 테그 가져오는 로직 */
public GetReportTagListDto getTag(Long id){
public GetReportTagListDto getTag(Long memberId){
log.info("getTag called by member: " + memberId);

// 오늘 날짜 가져오기
ZonedDateTime now = getToday();

List<ReportTag> reportTags = reportTagRepository.findLastMonthReportTagByMember_Id(id);
List<ReportTag> reportTags = reportTagRepository.findLastMonthReportTagByMember_Id(memberId);

if (reportTags == null || reportTags.isEmpty()) {
PostFastApiAiTagListDto aiDto = postNewAiTag(id, getToday());
PostFastApiAiTagListDto aiDto = postNewAiTag(memberId, getToday());

Member memberEntity = memberRepository.findMemberById(id);
Member memberEntity = memberRepository.findMemberById(memberId);

for (PostFastApiAiTagDto tag : aiDto.getTags()) {
ReportTag reportTag = ReportTag.builder()
Expand All @@ -113,7 +122,7 @@ public GetReportTagListDto getTag(Long id){
.build();
reportTagRepository.save(reportTag);
}
return createReportTagListDto(reportTagRepository.findLastMonthReportTagByMember_Id(id));
return createReportTagListDto(reportTagRepository.findLastMonthReportTagByMember_Id(memberId));
} else {
return createReportTagListDto(reportTags);
}
Expand All @@ -132,14 +141,15 @@ public GetReportTagListDto createReportTagListDto(List<ReportTag> reportTags){
return new GetReportTagListDto(getReportTagDtos);
}

public PostFastApiAiTagListDto getAiTag(Long id){
return postNewAiTag(id, getToday());
public PostFastApiAiTagListDto getAiTag(Long memberId){
return postNewAiTag(memberId, getToday());
}

public PostFastApiAiTagListDto postNewAiTag(Long id, ZonedDateTime today){
public PostFastApiAiTagListDto postNewAiTag(Long memberId, ZonedDateTime today){
String persona = getPersona(memberId);
PostFastApiUserTagDto userDto = PostFastApiUserTagDto.builder()
.member_id(id.intValue())
.user_persona("")
.member_id(memberId.intValue())
.user_persona(persona)
.schedule_datetime_start(today)
.schedule_datetime_end(today)
.build();
Expand All @@ -150,6 +160,8 @@ public PostFastApiAiTagListDto postNewAiTag(Long id, ZonedDateTime today){

/* 한 줄 추천 및 엑티비티 가져오는 로직 */
public PostFastApiAiRecommendActivityDto getRecommendActivity(Long memberId){
log.info("getRecommendActivity called by member: " + memberId);

// 오늘 날짜 가져오기
ZonedDateTime now = getToday();
List<ReportRecommend> reportRecommends = reportRecommendRepository.findTodayReportRecommendByMember_Id(memberId);
Expand Down Expand Up @@ -203,10 +215,11 @@ public PostFastApiAiRecommendActivityDto getRecommendActivity(Long memberId){
}
}

public PostFastApiAiRecommendActivityDto postNewAiRecommend(Long id, ZonedDateTime today){
public PostFastApiAiRecommendActivityDto postNewAiRecommend(Long memberId, ZonedDateTime today){
String persona = getPersona(memberId);
PostFastApiUserRecommendActivityDto userDto = PostFastApiUserRecommendActivityDto.builder()
.member_id(id.intValue())
.user_persona("") // 빈 문자열은 default
.member_id(memberId.intValue())
.user_persona(persona)
.schedule_datetime_start(today)
.schedule_datetime_end(today)
.build();
Expand All @@ -223,4 +236,19 @@ public ZonedDateTime getToday(){
return ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
}

public String getPersona(Long memberId){
Profile profileEntity = profileRepository.findProfileByMember_Id(memberId);
PersonaType personaType = profileEntity.getPersonaType();

String persona = "default";
if (personaType == PersonaType.HARDNESS){
persona = "hard";
}
if (personaType == PersonaType.CALMNESS){
persona = "calm";
}

return persona;
}

}
2 changes: 2 additions & 0 deletions src/main/java/Ness/Backend/domain/todo/TodoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class TodoService {

/* 일정 관련 한 줄 추천 가져오는 로직 */
public List<GetScheduleDto> getTodo(Long memberId){
log.info("getTodo called by member: " + memberId);

Profile userProfile = profileRepository.findProfileByMember_Id(memberId);
// 오늘 날짜 가져오기
ZonedDateTime now = time.getToday();
Expand Down

0 comments on commit 94b63a7

Please sign in to comment.