-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
233 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/com/software/ott/friend/dto/FriendRecommendResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.software.ott.friend.dto; | ||
|
||
import com.software.ott.content.entity.Content; | ||
|
||
public record FriendRecommendResponse( | ||
Long RecommendId, | ||
String senderName, | ||
String senderEmail, | ||
Content content, | ||
String reason | ||
) { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/software/ott/friend/dto/RecommendContentRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.software.ott.friend.dto; | ||
|
||
public record RecommendContentRequest( | ||
Long contentId, | ||
String reason | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/software/ott/friend/entity/FriendRecommend.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.software.ott.friend.entity; | ||
|
||
import com.software.ott.content.entity.Content; | ||
import com.software.ott.member.entity.Member; | ||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.*; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Entity | ||
@Builder | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@EntityListeners(AuditingEntityListener.class) | ||
public class FriendRecommend { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
@NotNull | ||
@ManyToOne | ||
@JoinColumn(name = "sender_id") | ||
private Member sender; | ||
@NotNull | ||
@ManyToOne | ||
@JoinColumn(name = "receiver_id") | ||
private Member receiver; | ||
@NotNull | ||
@OneToOne | ||
@JoinColumn(name = "content_id") | ||
private Content recommendContent; | ||
@CreatedDate | ||
@NotNull | ||
private LocalDateTime recommendTime; | ||
private String reason; | ||
|
||
public boolean NotAuth(Member member) { | ||
return !(sender.equals(member) || receiver.equals(member)); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/software/ott/friend/repository/FriendRecommendRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.software.ott.friend.repository; | ||
|
||
import com.software.ott.content.entity.Content; | ||
import com.software.ott.friend.entity.FriendRecommend; | ||
import com.software.ott.member.entity.Member; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface FriendRecommendRepository extends JpaRepository<FriendRecommend, Long> { | ||
|
||
boolean existsBySenderAndReceiverAndRecommendContent(Member sender, Member receiver, Content recommendContent); | ||
|
||
List<FriendRecommend> getAllByReceiver(Member receiver); | ||
} |
103 changes: 103 additions & 0 deletions
103
src/main/java/com/software/ott/friend/service/FriendRecommendService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.software.ott.friend.service; | ||
|
||
import com.software.ott.common.exception.BadRequestException; | ||
import com.software.ott.common.exception.NotFoundException; | ||
import com.software.ott.content.entity.Content; | ||
import com.software.ott.content.repository.ContentRepository; | ||
import com.software.ott.friend.dto.FriendRecommendResponse; | ||
import com.software.ott.friend.dto.RecommendContentRequest; | ||
import com.software.ott.friend.entity.Friend; | ||
import com.software.ott.friend.entity.FriendRecommend; | ||
import com.software.ott.friend.repository.FriendRecommendRepository; | ||
import com.software.ott.friend.repository.FriendRepository; | ||
import com.software.ott.member.entity.Member; | ||
import com.software.ott.member.repository.MemberRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class FriendRecommendService { | ||
|
||
private final FriendRecommendRepository friendRecommendRepository; | ||
private final MemberRepository memberRepository; | ||
private final FriendRepository friendRepository; | ||
private final ContentRepository contentRepository; | ||
|
||
@Transactional | ||
public void sendContentRecommend(Long memberId, Long friendRequestId, RecommendContentRequest recommendContentRequest) { | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new NotFoundException("id에 해당하는 멤버가 없습니다.")); | ||
|
||
Friend friend = friendRepository.findById(friendRequestId) | ||
.orElseThrow(() -> new NotFoundException("friendRequestId에 해당하는 친구 요청(친구)가 없습니다.")); | ||
|
||
Content content = contentRepository.findById(recommendContentRequest.contentId()) | ||
.orElseThrow(() -> new NotFoundException("contentId에 해당하는 컨텐츠가 없습니다.")); | ||
|
||
if (!friendRepository.existsByRequesterIdOrAccepterId(memberId, friend.getAccepter().getId()) && !friendRepository.existsByRequesterIdOrAccepterId(friend.getRequester().getId(), memberId)) { | ||
throw new BadRequestException("서로 친구 관계가 아닙니다."); | ||
} | ||
|
||
Member friendMember = null; | ||
|
||
if (friend.getAccepter().equals(member)) { | ||
friendMember = friend.getRequester(); | ||
} | ||
|
||
if (friend.getRequester().equals(member)) { | ||
friendMember = friend.getAccepter(); | ||
} | ||
|
||
if (friendMember == null) { | ||
throw new BadRequestException("정상적인 접근이 아닙니다."); | ||
} | ||
|
||
if (friendRecommendRepository.existsBySenderAndReceiverAndRecommendContent(member, friendMember, content)) { | ||
throw new BadRequestException("이미 해당 컨텐츠를 상대방에게 추천했습니다."); | ||
} | ||
|
||
friendRecommendRepository.save( | ||
FriendRecommend.builder() | ||
.sender(member) | ||
.receiver(friendMember) | ||
.recommendContent(content) | ||
.reason(recommendContentRequest.reason()) | ||
.build()); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<FriendRecommendResponse> getAllRecommendedContents(Long memberId) { | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new NotFoundException("id에 해당하는 멤버가 없습니다.")); | ||
|
||
return friendRecommendRepository.getAllByReceiver(member) | ||
.stream().map( | ||
FriendRecommend -> new FriendRecommendResponse( | ||
FriendRecommend.getId(), | ||
FriendRecommend.getSender().getName(), | ||
FriendRecommend.getSender().getEmail(), | ||
FriendRecommend.getRecommendContent(), | ||
FriendRecommend.getReason())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Transactional | ||
public void deleteRecommend(Long memberId, Long recommendId) { | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new NotFoundException("id에 해당하는 멤버가 없습니다.")); | ||
|
||
FriendRecommend friendRecommend = friendRecommendRepository.findById(recommendId) | ||
.orElseThrow(() -> new NotFoundException("recommendId에 해당하는 친구의 추천이 없습니다.")); | ||
|
||
if (friendRecommend.NotAuth(member)) { | ||
throw new BadRequestException("추천 발송인이나 수신인만 삭제가 가능합니다."); | ||
} | ||
|
||
friendRecommendRepository.delete(friendRecommend); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters