Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team3.kummit.api;
package team3.kummit.api.profile;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team3.kummit.api;
package team3.kummit.api.profile;

import java.time.LocalDateTime;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team3.kummit.api;
package team3.kummit.api.profile;

import java.time.LocalDate;

Expand Down
69 changes: 15 additions & 54 deletions src/main/java/team3/kummit/controller/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -17,6 +16,10 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import team3.kummit.api.*;
import team3.kummit.api.profile.MemberBandResponse;
import team3.kummit.api.profile.MemberBandResponseDto;
import team3.kummit.api.profile.MemberProfileResponse;
import team3.kummit.controller.parser.MemberApiParser;
import team3.kummit.domain.EmotionBand;
import team3.kummit.domain.Member;
import team3.kummit.service.EmotionBandArchiveService;
Expand All @@ -36,6 +39,7 @@ public class MemberController {
private final EmotionBandService emotionBandService;
private final EmotionBandLikeService emotionBandLikeService;
private final EmotionBandArchiveService emotionBandArchiveService;
private final MemberApiParser memberApiParser;


@Operation(
Expand All @@ -50,9 +54,8 @@ public class MemberController {
public ResponseEntity<MemberLoginResponse> memberLogin(
@Parameter(description = "로그인 요청 데이터 (이메일과 비밀번호 포함)")
@RequestBody MemberLoginRequest memberLoginRequest) {
return memberService.findByName(memberLoginRequest.name())
.map(member -> ResponseEntity.ok(new MemberLoginResponse(member.getId())))
.orElse(ResponseEntity.status(HttpStatus.UNAUTHORIZED).build());
Member member = memberService.findByName(memberLoginRequest.name());
return ResponseEntity.ok(new MemberLoginResponse(member.getId()));
}

@Operation(
Expand All @@ -70,13 +73,7 @@ public ResponseEntity<MemberLoginResponse> memberLogin(
public ResponseEntity<MemberProfileResponse> memberProfileGet(
@Parameter(description = "사용자 ID") @RequestParam Long memberId) {
Member member = memberService.findById(memberId);
return ResponseEntity.status(HttpStatus.CREATED).body(new MemberProfileResponse(
member.getName(),
member.getSignUpDate(),
member.getBandCreateCount() != null ? member.getBandCreateCount() : 0,
member.getBandJoinCount() != null ? member.getBandJoinCount() : 0,
member.getLikeCount() != null ? member.getLikeCount() : 0,
member.getSongAddCount() != null ? member.getSongAddCount() : 0));
return ResponseEntity.ok(memberApiParser.mapToMemberProfileResponse(member));
}

@Operation(
Expand All @@ -93,9 +90,9 @@ public ResponseEntity<MemberProfileResponse> memberProfileGet(
@GetMapping("/my-band")
public ResponseEntity<MemberBandResponse> memberMyBand(
@Parameter(description = "사용자 ID") @RequestParam Long memberId) {
List<Long> bandIdListOfMember = emotionBandService.findEmotionBandIdListByCreator(memberId);
List<EmotionBand> allByEmotionBandIdList = emotionBandService.findAllByEmotionBandIdList(bandIdListOfMember);
List<MemberBandResponseDto> collect = allByEmotionBandIdList.stream().map(this::toDto).collect(Collectors.toList());
List<Long> bandIdListOfMember = emotionBandService.findEmotionBandIdListByMemberId(memberId);
List<EmotionBand> allByEmotionBandIdList = emotionBandService.findAllByEmotionBandIdListWithSongs(bandIdListOfMember);
List<MemberBandResponseDto> collect = allByEmotionBandIdList.stream().map(memberApiParser::mapToMemberBandResponse).collect(Collectors.toList());
return ResponseEntity.ok(new MemberBandResponse(collect));
}

Expand All @@ -114,8 +111,8 @@ public ResponseEntity<MemberBandResponse> memberMyBand(
public ResponseEntity<MemberBandResponse> memberLikeBand(
@Parameter(description = "사용자 ID") @RequestParam Long memberId) {
List<Long> bandIdListOfMember = emotionBandLikeService.findEmotionBandListByMemberId(memberId);
List<EmotionBand> allByEmotionBandIdList = emotionBandService.findAllByEmotionBandIdList(bandIdListOfMember);
List<MemberBandResponseDto> collect = allByEmotionBandIdList.stream().map(this::toDto).collect(Collectors.toList());
List<EmotionBand> allByEmotionBandIdList = emotionBandService.findAllByEmotionBandIdListWithSongs(bandIdListOfMember);
List<MemberBandResponseDto> collect = allByEmotionBandIdList.stream().map(memberApiParser::mapToMemberBandResponse).collect(Collectors.toList());
return ResponseEntity.ok(new MemberBandResponse(collect));
}

Expand All @@ -135,46 +132,10 @@ public ResponseEntity<MemberBandResponse> memberLikeBand(
public ResponseEntity<MemberBandResponse> memberArchiveBand(
@Parameter(description = "사용자 ID") @RequestParam Long memberId) {
List<Long> bandIdListOfMember = emotionBandArchiveService.findEmotionBandIdListByCreator(memberId);
List<EmotionBand> allByEmotionBandIdList = emotionBandService.findAllByEmotionBandIdList(bandIdListOfMember);
List<MemberBandResponseDto> collect = allByEmotionBandIdList.stream().map(this::toDto).collect(Collectors.toList());
List<EmotionBand> allByEmotionBandIdList = emotionBandService.findAllByEmotionBandIdListWithSongs(bandIdListOfMember);
List<MemberBandResponseDto> collect = allByEmotionBandIdList.stream().map(memberApiParser::mapToMemberBandResponse).collect(Collectors.toList());
return ResponseEntity.ok(new MemberBandResponse(collect));
}


private MemberBandResponseDto toDto(EmotionBand band) {
return new MemberBandResponseDto(
band.getEmotion(),
band.getCreatorName(),
band.getDescription(),
band.getSongs().stream()
.map(song -> new MemberBandResponseDto.Music(
song.getAlbumImageLink(),
song.getTitle(),
song.getArtist(),
song.getPreviewLink()
))
.collect(Collectors.toList()),
band.getLikeCount(),
band.getPeopleCount(),
band.getSongCount(),
band.getCommentCount(),
band.getEndTime()
);
}
















}
45 changes: 45 additions & 0 deletions src/main/java/team3/kummit/controller/parser/MemberApiParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package team3.kummit.controller.parser;

import java.util.stream.Collectors;

import org.springframework.stereotype.Component;

import team3.kummit.api.profile.MemberBandResponseDto;
import team3.kummit.api.profile.MemberProfileResponse;
import team3.kummit.domain.EmotionBand;
import team3.kummit.domain.Member;

@Component
public class MemberApiParser {

public MemberProfileResponse mapToMemberProfileResponse(Member member){
return new MemberProfileResponse(
member.getName(),
member.getSignUpDate(),
member.getBandCreateCount(),
member.getBandJoinCount(),
member.getLikeCount(),
member.getSongAddCount());
}

public MemberBandResponseDto mapToMemberBandResponse(EmotionBand band){
return new MemberBandResponseDto(
band.getEmotion(),
band.getCreatorName(),
band.getDescription(),
band.getSongs().stream()
.map(song -> new MemberBandResponseDto.Music(
song.getAlbumImageLink(),
song.getTitle(),
song.getArtist(),
song.getPreviewLink()
))
.collect(Collectors.toList()),
band.getLikeCount(),
band.getPeopleCount(),
band.getSongCount(),
band.getCommentCount(),
band.getEndTime()
);
}
}
6 changes: 6 additions & 0 deletions src/main/java/team3/kummit/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public class Member {
public void incrementBandCreateCount() {
this.bandCreateCount++;
}
public void incrementBandJoinCount() {
this.bandJoinCount++;
}
public void incrementLikeCount() {
this.likeCount++;
}
public void incrementSongAddCount() {
this.songAddCount++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ public interface EmotionBandArchiveRepository extends JpaRepository<EmotionBandA
@Query("SELECT eba.emotionBand.id FROM EmotionBandArchive eba WHERE eba.creator.id = :memberId")
List<Long> findEmotionBandIdListByMemberId(@Param("memberId") Long memberId);

@Query("SELECT eb.id FROM EmotionBand eb where eb.creator.id =:memberId")
List<Long> findEmotionBandIdListByCreator(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@

public interface EmotionBandLikeRepository extends JpaRepository<EmotionBandLike, Long> {
Optional<EmotionBandLike> findByCreatorIdAndEmotionBandId(Long memberId, Long emotionBandId);
Long countByEmotionBandId(Long emotionBandId);

// 사용자가 공감한 밴드 ID 목록 조회
@Query("SELECT ebl.emotionBand.id FROM EmotionBandLike ebl WHERE ebl.creator.id = :memberId")
List<Long> findEmotionBandIdListByMemberId(@Param("memberId") Long memberId);

@Query("SELECT eb.id FROM EmotionBand eb where eb.creator.id =:memberId")
List<Long> findEmotionBandIdListByCreator(Long memberId);
}
11 changes: 5 additions & 6 deletions src/main/java/team3/kummit/repository/EmotionBandRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import team3.kummit.domain.EmotionBand;

public interface EmotionBandRepository extends JpaRepository<EmotionBand, Long> {

// 현재 시간이 endTime을 지나지 않은 밴드들 중 좋아요 수 기준 상위 3개
@Query("SELECT e FROM EmotionBand e WHERE e.endTime > :currentTime ORDER BY e.likeCount DESC")
List<EmotionBand> findTop3ByLikeCountAndEndTimeAfter(@Param("currentTime") LocalDateTime currentTime);
List<EmotionBand> findAllByLikeCountAndEndTimeAfter(LocalDateTime currentTime);

// 현재 시간이 endTime을 지나지 않은 밴드들 중 endTime 기준 최신순 상위 10개
// 현재 시간이 endTime을 지나지 않은 밴드들 중 endTime 기준 최신순
@Query("SELECT e FROM EmotionBand e WHERE e.endTime > :currentTime ORDER BY e.endTime DESC")
List<EmotionBand> findTop10ByEndTimeDescAndEndTimeAfter(@Param("currentTime") LocalDateTime currentTime);
List<EmotionBand> findAllByEndTimeDescAndEndTimeAfter(LocalDateTime currentTime);

@Query("SELECT eb.id FROM EmotionBand eb where eb.creator.id =:memberId")
List<Long> findEmotionBandIdListByCreator(Long memberId);
List<Long> findPkListByCreator(Long memberId);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PK로 표현을 하시는군요ㅎㅎ

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

흠 당시에는 메소드 명이 길어서 수정해봤는데, 지금보니까 그냥 원래대로 두는게 더 나아보이네요.. ㅎ

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개인적으로 snake_case에 좀 더 익숙한데 camelCase가 길어지면 유난히 더 길어보이고 잘 안읽히는 것 같아요


@Query("SELECT eb FROM EmotionBand eb left join fetch eb.songs ebs " +
"WHERE eb.endTime > :currentTime and eb.id in :emotionBandIdList")
List<EmotionBand> findAllByEmotionBandIdList(List<Long> emotionBandIdList, LocalDateTime currentTime);
List<EmotionBand> findAllByEmotionBandIdListWithSongs(List<Long> emotionBandIdList, LocalDateTime currentTime);


}
2 changes: 0 additions & 2 deletions src/main/java/team3/kummit/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import team3.kummit.domain.Comment;
import team3.kummit.domain.EmotionBand;
import team3.kummit.domain.Member;
import team3.kummit.api.CommentRequest;
import team3.kummit.api.CommentResponse;
import team3.kummit.repository.CommentRepository;
import team3.kummit.repository.EmotionBandRepository;
import team3.kummit.repository.MemberRepository;
Expand Down
26 changes: 6 additions & 20 deletions src/main/java/team3/kummit/service/EmotionBandService.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public EmotionBandListResponse getEmotionBandLists(Long memberId) {
LocalDateTime currentTime = LocalDateTime.now();

// 인기 밴드 조회 (좋아요 수 기준 상위 3개)
List<EmotionBand> popularBands = emotionBandRepository.findTop3ByLikeCountAndEndTimeAfter(currentTime);
List<EmotionBand> popularBands = emotionBandRepository.findAllByLikeCountAndEndTimeAfter(currentTime);
List<EmotionBandResponse> popularBandResponses = popularBands.stream()
.limit(3) // 최대 3개만
.map(band -> {
Expand All @@ -73,7 +73,7 @@ public EmotionBandListResponse getEmotionBandLists(Long memberId) {
.collect(Collectors.toList());

// 전체 밴드 조회 (최신순 상위 10개)
List<EmotionBand> allBands = emotionBandRepository.findTop10ByEndTimeDescAndEndTimeAfter(currentTime);
List<EmotionBand> allBands = emotionBandRepository.findAllByEndTimeDescAndEndTimeAfter(currentTime);
List<EmotionBandResponse> allBandResponses = allBands.stream()
.limit(10) // 최대 10개만
.map(band -> {
Expand All @@ -92,19 +92,6 @@ public EmotionBandListResponse getEmotionBandLists(Long memberId) {
.build();
}

@Transactional(readOnly = true)
public EmotionBandResponse getEmotionBand(Long emotionBandId, Long memberId) {
EmotionBand band = emotionBandRepository.findById(emotionBandId)
.orElseThrow(() -> new ResourceNotFoundException("감정밴드를 찾을 수 없습니다."));

boolean isLiked = memberId != null && emotionBandLikeService.isLiked(emotionBandId, memberId);
List<SongResponse> songs = songRepository.findByEmotionBandIdOrderByCreatedAtDesc(emotionBandId)
.stream()
.map(SongResponse::from)
.collect(Collectors.toList());
return EmotionBandResponse.from(band, isLiked, songs);
}

@Transactional(readOnly = true)
public EmotionBandDetailResponse getEmotionBandDetail(Long emotionBandId, Long memberId) {
// 감정밴드 조회
Expand All @@ -129,12 +116,11 @@ public EmotionBandDetailResponse getEmotionBandDetail(Long emotionBandId, Long m
return EmotionBandDetailResponse.from(emotionBand, songs, comments, isArchived, isLiked);
}


public List<Long> findEmotionBandIdListByCreator(Long memberId){
return emotionBandRepository.findEmotionBandIdListByCreator(memberId);
public List<Long> findEmotionBandIdListByMemberId(Long memberId){
return emotionBandRepository.findPkListByCreator(memberId);
}

public List<EmotionBand> findAllByEmotionBandIdList(List<Long> emotionBandIdList){
return emotionBandRepository.findAllByEmotionBandIdList(emotionBandIdList, LocalDateTime.now());
public List<EmotionBand> findAllByEmotionBandIdListWithSongs(List<Long> emotionBandIdList){
return emotionBandRepository.findAllByEmotionBandIdListWithSongs(emotionBandIdList, LocalDateTime.now());
}
}
6 changes: 3 additions & 3 deletions src/main/java/team3/kummit/service/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package team3.kummit.service;

import java.util.Optional;

import org.springframework.stereotype.Service;

Expand All @@ -20,8 +19,9 @@ public Member findById(Long memberId){
.orElseThrow(() -> new ResourceNotFoundException("사용자를 찾을 수 없습니다."));
}

public Optional<Member> findByName(String name){
return memberRepository.findByName(name);
public Member findByName(String name){
return memberRepository.findByName(name)
.orElseThrow(() -> new ResourceNotFoundException("사용자를 찾을 수 없습니다."));
}

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package team3.kummit.repository;

import static org.junit.jupiter.api.Assertions.*;

import java.time.LocalDateTime;
import java.util.List;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import team3.kummit.domain.EmotionBand;


@SpringBootTest
@DataJpaTest
class EmotionBandRepositoryTest {

@Autowired
EmotionBandRepository repository;

@Test
void findAllByEmotionBandIdList() {
List<Long> emotionBandIdListByCreator = repository.findEmotionBandIdListByCreator(1L);
void findAllByEmotionBandIdListWithSongs() {
List<Long> emotionBandIdListByCreator = repository.findPkListByCreator(1L);
}
@Test
void indAllByEmotionBandIdList() {
List<EmotionBand> allByEmotionBandIdList = repository.findAllByEmotionBandIdList(List.of(1L, 2L, 3L), LocalDateTime.now());
void findAllByEmotionBandIdList() {
List<EmotionBand> allByEmotionBandIdList = repository.findAllByEmotionBandIdListWithSongs(List.of(1L, 2L, 3L), LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
import team3.kummit.domain.Comment;
import team3.kummit.domain.EmotionBand;
import team3.kummit.domain.Member;

import team3.kummit.api.CommentRequest;
import team3.kummit.api.CommentResponse;

import team3.kummit.repository.CommentRepository;
import team3.kummit.repository.EmotionBandRepository;
import team3.kummit.repository.MemberRepository;
Expand Down