Skip to content

Commit

Permalink
refactor: type에 따른 SpeakingLog 조회 기능 개선 (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan29849 authored Feb 10, 2023
1 parent 5b4cf73 commit 6d85322
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 50 deletions.
2 changes: 1 addition & 1 deletion be/config
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<SpeakingLog> allSpeakingLogs = speakingLogRepository.findAllByCreatedAtBetween(startDateTime, endDateTime);
SpeakingLogType type = speakingLogConditionRequest.getType();
List<SpeakingLogResponse> speakingLogResponses;

if (type == SpeakingLogType.MY) {
speakingLogResponses = getSpeakingLogResponses(memberId, allSpeakingLogs);
} else if (type == SpeakingLogType.MATE) {
List<Follow> followings = followRepository.findAllByFollowerId(memberId);
Set<Long> 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<SpeakingLog> speakingLogs = speakingLogRepository.findAllByCreatedAtBetween(startDateTime, endDateTime);
/**
* TODO: type에 따라 SpeakingLog 조회가 되어야 함
*/

List<SpeakingLogResponse> 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<SpeakingLogResponse> getSpeakingLogResponses(Long memberId, List<SpeakingLog> allSpeakingLogs, Set<Long> 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<SpeakingLogResponse> getSpeakingLogResponses(Long memberId, List<SpeakingLog> 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());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,4 +13,5 @@ public interface FollowRepository extends JpaRepository<Follow, Long> {

Optional<Follow> findByFollowerIdAndFollowingId(Long followerId, Long followingId);

List<Follow> findAllByFollowerId(Long memberId);
}
36 changes: 18 additions & 18 deletions be/src/main/resources/db/default-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 6d85322

Please sign in to comment.