Skip to content

Commit

Permalink
feat: #58 멤버 탈퇴시 트랙 및 플레이리스트 삭제, 피드, 트랙 맵에서 트랙 조회 불가, 플리이리스트 디테일 조회 에…
Browse files Browse the repository at this point in the history
…서는 트랙 노래 정보만 보임
  • Loading branch information
dgh06175 committed Nov 2, 2024
1 parent dfe489f commit fd17b91
Show file tree
Hide file tree
Showing 13 changed files with 439 additions and 183 deletions.
27 changes: 19 additions & 8 deletions src/main/java/com/cabin/plat/domain/member/entity/Member.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.cabin.plat.domain.member.entity;

import com.cabin.plat.global.common.BaseEntity;
import jakarta.persistence.*;
import java.time.LocalDateTime;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.util.Objects;
import lombok.*;
import org.hibernate.annotations.SQLDelete;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.SQLRestriction;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

@Getter
@Builder
Expand Down Expand Up @@ -56,8 +63,12 @@ public class Member extends BaseEntity {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Member)) return false;
if (this == o) {
return true;
}
if (!(o instanceof Member)) {
return false;
}
Member member = (Member) o;
return id.equals(member.id) && clientId.equals(member.clientId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
package com.cabin.plat.domain.member.service;

import com.cabin.plat.domain.member.dto.MemberResponse;
import com.cabin.plat.domain.member.dto.MemberResponse.*;
import com.cabin.plat.domain.member.entity.Member;
import com.cabin.plat.domain.member.entity.StreamType;
import com.cabin.plat.domain.member.mapper.MemberMapper;
import com.cabin.plat.domain.member.repository.MemberRepository;
import com.cabin.plat.global.exception.RestApiException;
import com.cabin.plat.global.exception.errorCode.MemberErrorCode;
import com.cabin.plat.config.jwt.dto.TokenInfo;
import com.cabin.plat.config.jwt.service.JwtUtil;
import com.cabin.plat.domain.member.dto.MemberRequest;
import com.cabin.plat.domain.member.dto.MemberResponse;
import com.cabin.plat.domain.member.dto.MemberResponse.MemberId;
import com.cabin.plat.domain.member.dto.MemberResponse.ProfileInfo;
import com.cabin.plat.domain.member.dto.MemberResponse.ProfileStreamType;
import com.cabin.plat.domain.member.entity.Member;
import com.cabin.plat.domain.member.entity.PermissionRole;
import com.cabin.plat.domain.member.entity.RefreshToken;
import com.cabin.plat.domain.member.entity.SocialType;
import com.cabin.plat.domain.member.entity.StreamType;
import com.cabin.plat.domain.member.mapper.AuthenticationMapper;
import com.cabin.plat.domain.member.mapper.MemberMapper;
import com.cabin.plat.domain.member.repository.MemberRepository;
import com.cabin.plat.domain.member.repository.RefreshTokenRepository;
import com.cabin.plat.domain.playlist.entity.PlaylistTrack;
import com.cabin.plat.domain.playlist.repository.PlaylistRepository;
import com.cabin.plat.domain.playlist.repository.PlaylistTrackRepository;
import com.cabin.plat.domain.track.entity.Track;
import com.cabin.plat.domain.track.repository.TrackRepository;
import com.cabin.plat.global.exception.RestApiException;
import com.cabin.plat.global.exception.errorCode.MemberErrorCode;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;


@Service
@RequiredArgsConstructor
Expand All @@ -33,6 +39,9 @@ public class MemberServiceImpl implements MemberService {
private final AuthenticationMapper authenticationMapper;
private final RefreshTokenRepository refreshTokenRepository;
private final MemberMapper memberMapper;
private final TrackRepository trackRepository;
private final PlaylistTrackRepository playlistTrackRepository;
private final PlaylistRepository playlistRepository;

@Override
public MemberResponse.MemberSignIn appleSocialSignIn(
Expand Down Expand Up @@ -75,17 +84,16 @@ public MemberResponse.MemberSignIn signIn(MemberRequest.MemberSignInByEncryptedU
if (jwtUtil.isExpired(refreshToken)) {
return generateNewToken(member, isServiced);
}
String newAccessToken = jwtUtil.createAccessToken(member.getId(), member.getClientId(), member.getPermissionRole());
String newAccessToken = jwtUtil.createAccessToken(member.getId(), member.getClientId(),
member.getPermissionRole());
TokenInfo tokenInfo = authenticationMapper.toTokenInfo(newAccessToken, refreshToken);


refreshTokenRepository.save(new RefreshToken(member.getId(), refreshToken, newAccessToken));
return memberMapper.toMemberSignIn(member, tokenInfo, isServiced);
}

TokenInfo tokenInfo = authenticationMapper.toTokenInfo(accessToken, refreshToken);
return memberMapper.toMemberSignIn(member, tokenInfo, isServiced);

}

@Override
Expand Down Expand Up @@ -135,7 +143,8 @@ private MemberResponse.MemberSignIn generateNewToken(Member member, Boolean isSe

MemberResponse.MemberTokens memberTokens = jwtUtil.refreshTokens(memberId, clientId, permissionRole);

TokenInfo tokenInfo = authenticationMapper.toTokenInfo(memberTokens.getAccessToken(), memberTokens.getRefreshToken());
TokenInfo tokenInfo = authenticationMapper.toTokenInfo(memberTokens.getAccessToken(),
memberTokens.getRefreshToken());

return memberMapper.toMemberSignIn(member, tokenInfo, isServiced);
}
Expand All @@ -144,6 +153,11 @@ private MemberResponse.MemberSignIn generateNewToken(Member member, Boolean isSe
@Transactional
public MemberResponse.MemberId resign(Member member) {
Member deleteMember = findMemberById(member.getId());
playlistRepository.findAllByMember(deleteMember).forEach(playlist -> {
playlistTrackRepository.findAllByPlaylistIs(playlist).forEach(PlaylistTrack::delete);
playlist.delete();
});
trackRepository.findAllByMember(deleteMember).forEach(Track::delete);
deleteMember.delete();
memberRepository.save(deleteMember);
return memberMapper.toMemberId(deleteMember.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cabin.plat.domain.playlist.mapper;

import com.cabin.plat.domain.member.dto.MemberResponse;
import com.cabin.plat.domain.member.entity.Member;
import com.cabin.plat.domain.playlist.dto.PlaylistRequest.PlaylistUpload;
import com.cabin.plat.domain.playlist.dto.PlaylistResponse;
Expand Down Expand Up @@ -52,27 +51,35 @@ public PlaylistResponse.Playlists toPlaylists(List<PlaylistInfo> playlistInfos)
.build();
}

public PlaylistResponse.Playlists.PlaylistInfo toPlaylistInfo(Playlist playlist, List<PlaylistTrack> playlistTracks) {
public PlaylistResponse.Playlists.PlaylistInfo toPlaylistInfo(Playlist playlist,
List<PlaylistTrack> playlistTracks) {
return PlaylistInfo.builder()
.playlistId(playlist.getId())
.title(playlist.getTitle())
.playlistImageUrl(playlist.getPlaylistImageUrl())
.createdAt(playlist.getCreatedAt())
.uploaderNicknames(playlistTracks.stream()
.map(PlaylistTrack::getTrack)
.map(track -> track.getMember().getNickname())
.map(track -> {
if (track.getMember() == null) {
return "알수없음";
}
return track.getMember().getNickname();
})
.collect(Collectors.toUnmodifiableSet()))
.build();
}

public PlaylistResponse.TrackDetailOrder toTrackDetailOrder(PlaylistTrack playlistTrack, TrackResponse.TrackDetail trackDetail) {
public PlaylistResponse.TrackDetailOrder toTrackDetailOrder(PlaylistTrack playlistTrack,
TrackResponse.TrackDetail trackDetail) {
return PlaylistResponse.TrackDetailOrder.builder()
.orderIndex(playlistTrack.getOrderIndex())
.trackDetail(trackDetail)
.build();
}

public PlaylistResponse.PlaylistDetail toPlaylistDetail(Playlist playlist, List<TrackDetailOrder> trackDetailOrders) {
public PlaylistResponse.PlaylistDetail toPlaylistDetail(Playlist playlist,
List<TrackDetailOrder> trackDetailOrders) {
return PlaylistResponse.PlaylistDetail.builder()
.playlistId(playlist.getId())
.title(playlist.getTitle())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

public interface PlaylistRepository extends JpaRepository<Playlist, Long> {
Page<Playlist> findAllByMember(Member member, Pageable pageable);
List<Playlist> findAllByMember(Member member);
Page<Playlist> findAllByMemberAndTitleContainingIgnoreCase(Member member, String title, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
import com.cabin.plat.global.exception.RestApiException;
import com.cabin.plat.global.exception.errorCode.PlaylistErrorCode;
import com.cabin.plat.global.exception.errorCode.TrackErrorCode;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -101,6 +105,7 @@ public PlaylistResponse.Playlists getSearchedPlaylists(Member member, String tit
@Override
public PlaylistResponse.PlayListId deletePlaylist(Member member, Long playlistId) {
Playlist playlist = findPlaylistByIdWithValidation(playlistId, member);
playlistTrackRepository.findAllByPlaylistIs(playlist).forEach(PlaylistTrack::delete);
playlist.delete();
return playlistMapper.toPlaylistId(playlistId);
}
Expand All @@ -113,7 +118,8 @@ public PlaylistResponse.PlaylistDetail getPlaylistDetail(Member member, Long pla
playlist);
List<PlaylistResponse.TrackDetailOrder> trackDetailOrders = playlistTracks.stream()
.map(playlistTrack -> {
TrackResponse.TrackDetail trackDetail = trackService.getTrackById(member, playlistTrack.getTrack().getId());
TrackResponse.TrackDetail trackDetail = trackService.getTrackById(member,
playlistTrack.getTrack().getId());
return playlistMapper.toTrackDetailOrder(playlistTrack, trackDetail);
})
.sorted(Comparator.comparingInt(PlaylistResponse.TrackDetailOrder::getOrderIndex))
Expand All @@ -123,7 +129,8 @@ public PlaylistResponse.PlaylistDetail getPlaylistDetail(Member member, Long pla

@Transactional
@Override
public PlaylistResponse.PlayListId updatePlaylistTitleAndImage(Member member, Long playlistId, PlaylistRequest.PlaylistEdit playlistEdit) {
public PlaylistResponse.PlayListId updatePlaylistTitleAndImage(Member member, Long playlistId,
PlaylistRequest.PlaylistEdit playlistEdit) {
Playlist playlist = findPlaylistByIdWithValidation(playlistId, member);

// 제목 및 이미지 변경
Expand All @@ -134,7 +141,8 @@ public PlaylistResponse.PlayListId updatePlaylistTitleAndImage(Member member, Lo

@Transactional
@Override
public PlaylistResponse.PlayListId addTrackToPlaylist(Member member, Long playlistId, PlaylistRequest.TrackId trackId) {
public PlaylistResponse.PlayListId addTrackToPlaylist(Member member, Long playlistId,
PlaylistRequest.TrackId trackId) {
Playlist playlist = findPlaylistByIdWithValidation(playlistId, member);
Track track = findTrackById(trackId.getTrackId());

Expand Down Expand Up @@ -169,7 +177,7 @@ public PlayListId updateTrackOrders(Member member, Long playlistId, PlaylistOrde
validateTrackOrderCount(playlistOrders, playlistTracks);
validateTrackIds(playlistOrders, playlistTracks);

for (PlaylistTrack playlistTrack: playlistTracks) {
for (PlaylistTrack playlistTrack : playlistTracks) {
Long trackId = playlistTrack.getTrack().getId();
int newOrder = trackOrderMap.get(trackId);
playlistTrack.setOrderIndex(newOrder);
Expand Down Expand Up @@ -202,7 +210,8 @@ private List<PlaylistTrack> findPlaylistTracksInPlaylist(Playlist playlist) {
}

private static void validateTrackDuplicateInPlaylist(TrackId trackId, List<PlaylistTrack> playlistTracks) {
if (playlistTracks.stream().anyMatch(playlistTrack -> playlistTrack.getTrack().getId().equals(trackId.getTrackId()))) {
if (playlistTracks.stream()
.anyMatch(playlistTrack -> playlistTrack.getTrack().getId().equals(trackId.getTrackId()))) {
throw new RestApiException(PlaylistErrorCode.PLAYLIST_TRACK_DUPLICATE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import java.util.List;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class TrackResponse {
public class TrackResponse {

@Getter
@Builder
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/cabin/plat/domain/track/entity/Location.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package com.cabin.plat.domain.track.entity;

import com.cabin.plat.global.common.BaseEntity;
import jakarta.persistence.*;
import lombok.*;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLRestriction;


@Getter
@Builder
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@SQLRestriction("deleted_at is null")
public class Location extends BaseEntity {

@Id
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/com/cabin/plat/domain/track/entity/Track.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

import com.cabin.plat.domain.member.entity.Member;
import com.cabin.plat.global.common.BaseEntity;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.SQLRestriction;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@SQLRestriction("deleted_at is null")
public class Track extends BaseEntity {

@Id
Expand All @@ -20,11 +29,11 @@ public class Track extends BaseEntity {
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "location_id", nullable = false)
@JoinColumn(name = "location_id")
private Location location;

@Column(nullable = false)
Expand All @@ -35,4 +44,14 @@ public class Track extends BaseEntity {

@Column(name = "image_url")
private String imageUrl;

@Override
public void delete() {
super.delete();
this.member = null;
this.content = "삭제된 게시글 입니다";
this.imageUrl = "";
this.location.delete();
this.location = null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cabin.plat.domain.track.repository;

import com.cabin.plat.domain.member.entity.Member;
import com.cabin.plat.domain.track.entity.Track;
import io.lettuce.core.dynamic.annotation.Param;
import java.util.List;
Expand All @@ -22,4 +23,5 @@ List<Track> findAllTracksWithinBounds(
@Param("minLongitude") double minLongitude,
@Param("maxLongitude") double maxLongitude);
Page<Track> findAll(Pageable pageable);
List<Track> findAllByMember(Member member);
}
Loading

0 comments on commit fd17b91

Please sign in to comment.