Skip to content

Commit

Permalink
Merge pull request #216 from Gamegoo-repo/refactor/215
Browse files Browse the repository at this point in the history
[Refactor/215] μ•Œλ¦Ό μ—”ν‹°ν‹° μˆ˜μ • 및 νƒˆν‡΄ν•œ μ‚¬μš©μž μ†Œν™˜μ‚¬λͺ… ν‘œμ‹œ μΆ”κ°€
  • Loading branch information
Eunjin3395 authored Sep 18, 2024
2 parents ff0289c + 17027da commit 4df7889
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class QNotification extends EntityPathBase<Notification> {

public final QNotificationType notificationType;

public final NumberPath<Long> sourceId = createNumber("sourceId", Long.class);
public final com.gamegoo.domain.member.QMember sourceMember;

//inherited
public final DateTimePath<java.time.LocalDateTime> updatedAt = _super.updatedAt;
Expand All @@ -62,6 +62,7 @@ public QNotification(Class<? extends Notification> type, PathMetadata metadata,
super(type, metadata, inits);
this.member = inits.isInitialized("member") ? new com.gamegoo.domain.member.QMember(forProperty("member")) : null;
this.notificationType = inits.isInitialized("notificationType") ? new QNotificationType(forProperty("notificationType")) : null;
this.sourceMember = inits.isInitialized("sourceMember") ? new com.gamegoo.domain.member.QMember(forProperty("sourceMember")) : null;
}

}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/com/gamegoo/converter/NotificationConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,29 @@ public static NotificationResponse.notificationDTO toNotificationDTO(
StringBuilder urlBuilder = new StringBuilder(
notification.getNotificationType().getSourceUrl());

if (notification.getSourceId() != null) {
urlBuilder.append(notification.getSourceId());
if (notification.getSourceMember() != null) {
urlBuilder.append(notification.getSourceMember().getId());
}

pageUrl = urlBuilder.toString();
}

String content = notification.getContent();

// sourceMember λ‹‰λ„€μž„ ν‘œμ‹œ
if (notification.getSourceMember() != null) {
if (notification.getSourceMember().getBlind()) { // sourceMemberκ°€ νƒˆν‡΄ν•œ νšŒμ›μΈ 경우
content = "(νƒˆν‡΄ν•œ μ‚¬μš©μž)" + content;
} else {
content = notification.getSourceMember().getGameName() + content;
}

}

return NotificationResponse.notificationDTO.builder()
.notificationId(notification.getId())
.notificationType(notification.getNotificationType().getId().intValue())
.content(notification.getContent())
.content(content)
.pageUrl(pageUrl)
.read(notification.isRead())
.createdAt(notification.getCreatedAt().withNano(0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public class Notification extends BaseDateTimeEntity {
@Column(nullable = false)
private boolean isRead;

private Long sourceId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "source_member_id")
private Member sourceMember;


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/gamegoo/service/manner/MannerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ public MannerRating insertManner(MannerRequest.mannerInsertDTO request, Long mem

// λ§€λ„ˆν‰κ°€ 등둝됨 μ•Œλ¦Ό 전솑
// λ“±λ‘λœ λ§€λ„ˆ 평가 ν‚€μ›Œλ“œ string 생성
String mannerKeywordString = mannerRatingKeywordList.stream()
.map(MannerKeyword::getContents)
.collect(Collectors.joining(", "));
String mannerKeywordString = mannerRatingKeywordList.get(0).getContents();
if (mannerRatingKeywordList.size() > 1) {
mannerKeywordString += " μ™Έ " + (mannerRatingKeywordList.size() - 1) + "개의";
}

Notification ratedNotification = notificationService.createNotification(
NotificationTypeTitle.MANNER_KEYWORD_RATED,
Expand Down Expand Up @@ -198,11 +199,12 @@ public MannerRating insertBadManner(MannerRequest.mannerInsertDTO request, Long
mannerRatingKeywordRepository.save(mannerRatingKeyword);
});

// λ§€λ„ˆν‰κ°€ 등둝됨 μ•Œλ¦Ό 전솑
// λΉ„λ§€λ„ˆν‰κ°€ 등둝됨 μ•Œλ¦Ό 전솑
// λ“±λ‘λœ λ§€λ„ˆ 평가 ν‚€μ›Œλ“œ string 생성
String mannerKeywordString = mannerRatingKeywordList.stream()
.map(MannerKeyword::getContents)
.collect(Collectors.joining(", "));
String mannerKeywordString = mannerRatingKeywordList.get(0).getContents();
if (mannerRatingKeywordList.size() > 1) {
mannerKeywordString += " μ™Έ " + (mannerRatingKeywordList.size() - 1) + "개의";
}

Notification ratedNotification = notificationService.createNotification(
NotificationTypeTitle.MANNER_KEYWORD_RATED,
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/gamegoo/service/member/FriendService.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ public void sendFriendRequest(Long memberId, Long targetMemberId) {
// 친ꡬ μš”μ²­ μ•Œλ¦Ό 생성
// member -> targetMember
notificationService.createNotification(NotificationTypeTitle.FRIEND_REQUEST_SEND,
targetMember.getGameName(), null, member);
null, targetMember, member);

// targetMember -> member
notificationService.createNotification(NotificationTypeTitle.FRIEND_REQUEST_RECEIVED,
member.getGameName(), member.getId(), targetMember);
null, member, targetMember);
}

/**
Expand Down Expand Up @@ -226,7 +226,7 @@ public void acceptFriendRequest(Long memberId, Long targetMemberId) {

// targetMemberμ—κ²Œ 친ꡬ μš”μ²­ 수락 μ•Œλ¦Ό 생성
notificationService.createNotification(NotificationTypeTitle.FRIEND_REQUEST_ACCEPTED,
member.getGameName(), null, targetMember);
member.getGameName(), member, targetMember);
}

/**
Expand Down Expand Up @@ -259,7 +259,7 @@ public void rejectFriendRequest(Long memberId, Long targetMemberId) {

// targetMemberμ—κ²Œ 친ꡬ μš”μ²­ 거절 μ•Œλ¦Ό 생성
notificationService.createNotification(NotificationTypeTitle.FRIEND_REQUEST_REJECTED,
member.getGameName(), null, targetMember);
member.getGameName(), member, targetMember);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,33 @@ public class NotificationService {
* μƒˆλ‘œμš΄ μ•Œλ¦Ό 생성 및 μ €μž₯ λ©”μ†Œλ“œ
*
* @param notificationTypeTitle
* @param content 각 μ•Œλ¦Όμ— ν¬ν•¨λ˜μ–΄μ–΄μ•Ό ν•  정보 (μ‚¬μš©μž λ‹‰λ„€μž„, λ§€λ„ˆλ ˆλ²¨ 단계, ν‚€λ‘¬λ“œ)
* @param sourceId 이동해야할 url의 고유 id νŒŒλΌλ―Έν„°, FRIEND_REQUEST_RECEIVEDμ—μ„œλ§Œ ν•„μš”
* @param content 각 μ•Œλ¦Όμ— ν¬ν•¨λ˜μ–΄μ–΄μ•Ό ν•  정보 (λ§€λ„ˆλ ˆλ²¨ 단계, ν‚€μ›Œλ“œ)
* @param sourceMember μ•Œλ¦Ό source νšŒμ›
* @param member μ•Œλ¦Όμ„ 받을 λŒ€μƒ νšŒμ›
* @return
*/
public Notification createNotification(NotificationTypeTitle notificationTypeTitle,
String content, Long sourceId, Member member) {
String content, Member sourceMember, Member member) {

NotificationType notificationType = notificationTypeRepository.findNotificationTypeByTitle(
notificationTypeTitle)
.orElseThrow(() -> new NotificationHandler(ErrorStatus.NOTIFICATION_TYPE_NOT_FOUND));
NotificationType notificationType = findNotificationType(notificationTypeTitle);
String notificationContent = generateNotificationContent(notificationType, content,
sourceMember);

return saveNotification(notificationType, notificationContent, sourceMember, member);
}

String notificationContent = switch (notificationTypeTitle) {
private String generateNotificationContent(NotificationType notificationType, String content,
Member sourceMember) {
return switch (notificationType.getTitle()) {
// μ•Œλ¦Ό νƒ€μž…λ³„λ‘œ λΆ„κΈ° 처리
case FRIEND_REQUEST_SEND, FRIEND_REQUEST_ACCEPTED, FRIEND_REQUEST_REJECTED ->
content + notificationType.getContent();
notificationType.getContent();

case FRIEND_REQUEST_RECEIVED -> {
if (sourceId == null) {
if (sourceMember == null) {
throw new NotificationHandler(ErrorStatus.NOTIFICATION_METHOD_BAD_REQUEST);
}
yield content + notificationType.getContent();
yield notificationType.getContent();
}

case MANNER_LEVEL_UP, MANNER_LEVEL_DOWN, MANNER_KEYWORD_RATED ->
Expand All @@ -67,14 +73,15 @@ public Notification createNotification(NotificationTypeTitle notificationTypeTit
int i = random.nextInt(1000) + 1;
yield notificationType.getContent() + i;
}

default -> throw new NotificationHandler(ErrorStatus.NOTIFICATION_TYPE_NOT_FOUND);
};
}

private Notification saveNotification(NotificationType type, String content,
Member sourceMember, Member member) {
Notification notification = Notification.builder()
.notificationType(notificationType)
.content(notificationContent)
.sourceId(sourceId)
.notificationType(type)
.content(content)
.sourceMember(sourceMember)
.isRead(false)
.build();
notification.setMember(member);
Expand Down Expand Up @@ -155,4 +162,9 @@ public long countUnreadNotification(Long memberId) {
.filter(notification -> !notification.isRead())
.count();
}

private NotificationType findNotificationType(NotificationTypeTitle title) {
return notificationTypeRepository.findNotificationTypeByTitle(title)
.orElseThrow(() -> new NotificationHandler(ErrorStatus.NOTIFICATION_TYPE_NOT_FOUND));
}
}

0 comments on commit 4df7889

Please sign in to comment.