From 6d853225f89b9ea78fbe75475f074221de01800e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=82=98=EB=93=A0?= <67811880+nathan29849@users.noreply.github.com> Date: Fri, 10 Feb 2023 23:45:34 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20type=EC=97=90=20=EB=94=B0=EB=A5=B8?= =?UTF-8?q?=20SpeakingLog=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0=20(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- be/config | 2 +- .../core/application/SpeakingLogService.java | 111 +++++++++++++----- .../example/be/core/domain/member/Member.java | 2 +- .../repository/member/FollowRepository.java | 2 + be/src/main/resources/db/default-data.sql | 36 +++--- 5 files changed, 103 insertions(+), 50 deletions(-) diff --git a/be/config b/be/config index 512a6efa..4eb8684c 160000 --- a/be/config +++ b/be/config @@ -1 +1 @@ -Subproject commit 512a6efad9b744d2f97fde1da2f3375628797baf +Subproject commit 4eb8684c197eb5669805410afb18a42751a3bc3f diff --git a/be/src/main/java/com/example/be/core/application/SpeakingLogService.java b/be/src/main/java/com/example/be/core/application/SpeakingLogService.java index bfae1104..85585f1f 100644 --- a/be/src/main/java/com/example/be/core/application/SpeakingLogService.java +++ b/be/src/main/java/com/example/be/core/application/SpeakingLogService.java @@ -10,19 +10,24 @@ import com.example.be.core.application.dto.response.SpeakingLogDetailResponse; import com.example.be.core.application.dto.response.SpeakingLogResponse; import com.example.be.core.application.dto.response.SpeakingLogsResponse; +import com.example.be.core.domain.member.Follow; import com.example.be.core.domain.member.Member; import com.example.be.core.domain.speakinglog.Favorite; import com.example.be.core.domain.speakinglog.SpeakingLog; +import com.example.be.core.domain.speakinglog.SpeakingLogType; +import com.example.be.core.repository.member.FollowRepository; import com.example.be.core.repository.member.MemberRepository; import com.example.be.core.repository.speakinglog.CommentRepository; import com.example.be.core.repository.speakinglog.FavoriteRepository; import com.example.be.core.repository.speakinglog.SpeakingLogRepository; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -38,14 +43,16 @@ public class SpeakingLogService { private final FavoriteRepository favoriteRepository; private final CommentRepository commentRepository; private final MemberRepository memberRepository; + private final FollowRepository followRepository; public SpeakingLogService(SpeakingLogRepository speakingLogRepository, FavoriteRepository favoriteRepository, CommentRepository commentRepository, - MemberRepository memberRepository) { + MemberRepository memberRepository, FollowRepository followRepository) { this.speakingLogRepository = speakingLogRepository; this.favoriteRepository = favoriteRepository; this.commentRepository = commentRepository; this.memberRepository = memberRepository; + this.followRepository = followRepository; } @Transactional @@ -78,38 +85,82 @@ public SpeakingLogDetailResponse create(Long memberId, public SpeakingLogsResponse find(Long memberId, SpeakingLogConditionRequest speakingLogConditionRequest) { log.debug("[스피킹 로그 전체 조회] SpeakingLogConditionRequest = {}",speakingLogConditionRequest.toString()); + LocalDate date = speakingLogConditionRequest.getDate(); + LocalDateTime startDateTime = LocalDateTime.of(date.minusDays(1), LocalTime.of(0,0,0)); + LocalDateTime endDateTime = LocalDateTime.of(date, LocalTime.of(23,59,59)); + + List allSpeakingLogs = speakingLogRepository.findAllByCreatedAtBetween(startDateTime, endDateTime); + SpeakingLogType type = speakingLogConditionRequest.getType(); + List speakingLogResponses; + + if (type == SpeakingLogType.MY) { + speakingLogResponses = getSpeakingLogResponses(memberId, allSpeakingLogs); + } else if (type == SpeakingLogType.MATE) { + List followings = followRepository.findAllByFollowerId(memberId); + Set mates = followings.stream() + .map(f -> f.getFollowing().getId()) + .collect(Collectors.toSet()); + speakingLogResponses = getSpeakingLogResponses(memberId, allSpeakingLogs, mates); + } else { + speakingLogResponses = allSpeakingLogs + .stream() + .map(s -> new SpeakingLogResponse( + s.getId(), + s.getTitle(), + s.getVoiceRecord(), + s.getVoiceText(), + getFavoriteCount(s.getId()), + getCommentCount(s.getId()), + presentOfFavorite(memberId, s), + s.getMember().getProfileImage(), + s.getId() + )) + .collect(Collectors.toList()); + } + + return new SpeakingLogsResponse(type, date, speakingLogResponses); + } + + private boolean presentOfFavorite(Long memberId, SpeakingLog speakingLog) { + return favoriteRepository + .findByMemberIdAndSpeakingLog(memberId, speakingLog) + .isPresent(); + } - LocalDateTime startDateTime = LocalDateTime.of(speakingLogConditionRequest.getDate().minusDays(1), - LocalTime.of(0,0,0)); - - LocalDateTime endDateTime = LocalDateTime.of(speakingLogConditionRequest.getDate(), LocalTime.of(23,59,59)); - - List speakingLogs = speakingLogRepository.findAllByCreatedAtBetween(startDateTime, endDateTime); - /** - * TODO: type에 따라 SpeakingLog 조회가 되어야 함 - */ - - List speakingLogResponses = speakingLogs.stream() - .map(speakingLog -> - new SpeakingLogResponse( - speakingLog.getId(), - speakingLog.getTitle(), - speakingLog.getVoiceRecord(), - speakingLog.getVoiceText(), - getFavoriteCount(speakingLog.getId()), - getCommentCount(speakingLog.getId()), - favoriteRepository.findByMemberIdAndSpeakingLog(memberId, speakingLog) - .isPresent(), - speakingLog.getMember().getProfileImage(), - speakingLog.getId() - )) + private List getSpeakingLogResponses(Long memberId, List allSpeakingLogs, Set mates) { + return allSpeakingLogs + .stream() + .filter(s -> mates.contains(s.getMember().getId())) + .map(s -> new SpeakingLogResponse( + s.getId(), + s.getTitle(), + s.getVoiceRecord(), + s.getVoiceText(), + getFavoriteCount(s.getId()), + getCommentCount(s.getId()), + presentOfFavorite(memberId, s), + s.getMember().getProfileImage(), + s.getMember().getId() + )) .collect(Collectors.toList()); + } - return new SpeakingLogsResponse( - speakingLogConditionRequest.getType(), - speakingLogConditionRequest.getDate(), - speakingLogResponses - ); + private List getSpeakingLogResponses(Long memberId, List allSpeakingLogs) { + return allSpeakingLogs + .stream() + .filter(s -> s.getMember().getId().equals(memberId)) + .map(s -> new SpeakingLogResponse( + s.getId(), + s.getTitle(), + s.getVoiceRecord(), + s.getVoiceText(), + getFavoriteCount(s.getId()), + getCommentCount(s.getId()), + presentOfFavorite(memberId, s), + s.getMember().getProfileImage(), + s.getMember().getId() + )) + .collect(Collectors.toList()); } diff --git a/be/src/main/java/com/example/be/core/domain/member/Member.java b/be/src/main/java/com/example/be/core/domain/member/Member.java index 6df8d5c0..6e44d7e3 100644 --- a/be/src/main/java/com/example/be/core/domain/member/Member.java +++ b/be/src/main/java/com/example/be/core/domain/member/Member.java @@ -36,7 +36,7 @@ public class Member extends BaseEntity { private String profileImage; - @Enumerated(EnumType.STRING) + @Enumerated(value = EnumType.STRING) private MemberType memberType; private Boolean alarmStatus; private Integer point; diff --git a/be/src/main/java/com/example/be/core/repository/member/FollowRepository.java b/be/src/main/java/com/example/be/core/repository/member/FollowRepository.java index 1f4ff762..7dd93923 100644 --- a/be/src/main/java/com/example/be/core/repository/member/FollowRepository.java +++ b/be/src/main/java/com/example/be/core/repository/member/FollowRepository.java @@ -1,6 +1,7 @@ package com.example.be.core.repository.member; import com.example.be.core.domain.member.Follow; +import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; @@ -12,4 +13,5 @@ public interface FollowRepository extends JpaRepository { Optional findByFollowerIdAndFollowingId(Long followerId, Long followingId); + List findAllByFollowerId(Long memberId); } diff --git a/be/src/main/resources/db/default-data.sql b/be/src/main/resources/db/default-data.sql index 3ac11600..5b3bcdb8 100644 --- a/be/src/main/resources/db/default-data.sql +++ b/be/src/main/resources/db/default-data.sql @@ -17,32 +17,32 @@ INSERT INTO goal(goal_id, content) VALUES (5, '어학 자격증 따기!'); -- SUBJECT -INSERT INTO subject(subject_id, content) -VALUES (1, '여행'); +INSERT INTO subject(subject_id, content, image) +VALUES (1, '여행', 'https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/static/travle.png'); -INSERT INTO subject(subject_id, content) -VALUES (2, '영화&음악'); +INSERT INTO subject(subject_id, content, image) +VALUES (2, '영화&음악', 'https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/static/movie_music.png'); -INSERT INTO subject(subject_id, content) -VALUES (3, '일&회사'); +INSERT INTO subject(subject_id, content, image) +VALUES (3, '일&회사', 'https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/static/company.png'); -INSERT INTO subject(subject_id, content) -VALUES (4, '쇼핑'); +INSERT INTO subject(subject_id, content, image) +VALUES (4, '쇼핑', 'https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/static/shopping.png'); -INSERT INTO subject(subject_id, content) -VALUES (5, '음식'); +INSERT INTO subject(subject_id, content, image) +VALUES (5, '음식', 'https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/static/food.png'); -INSERT INTO subject(subject_id, content) -VALUES (6, '가족&친구'); +INSERT INTO subject(subject_id, content, image) +VALUES (6, '가족&친구', 'https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/static/family.png'); -INSERT INTO subject(subject_id, content) -VALUES (7, '운동&건강'); +INSERT INTO subject(subject_id, content, image) +VALUES (7, '운동&건강', 'https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/static/workout.png'); -INSERT INTO subject(subject_id, content) -VALUES (8, '동네'); +INSERT INTO subject(subject_id, content, image) +VALUES (8, '동네', 'https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/static/town.png'); -INSERT INTO subject(subject_id, content) -VALUES (9, '연애'); +INSERT INTO subject(subject_id, content, image) +VALUES (9, '연애', 'https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/static/love.png'); SET AUTOCOMMIT = 1;