Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat/124] 알림 등록 메소드 구현 및 친구 요청 전송 시 알림 등록 #125

Merged
merged 7 commits into from
Aug 8, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class QNotification extends EntityPathBase<Notification> {

public final QNotificationType notificationType;

public final NumberPath<Long> sourceId = createNumber("sourceId", Long.class);

//inherited
public final DateTimePath<java.time.LocalDateTime> updatedAt = _super.updatedAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public class QNotificationType extends EntityPathBase<NotificationType> {

public final StringPath imgUrl = createString("imgUrl");

public final StringPath name = createString("name");
public final StringPath sourceUrl = createString("sourceUrl");

public final EnumPath<NotificationTypeTitle> title = createEnum("title", NotificationTypeTitle.class);

//inherited
public final DateTimePath<java.time.LocalDateTime> updatedAt = _super.updatedAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public enum ErrorStatus implements BaseErrorCode {
BLOCKED_BY_FRIEND_TARGET(HttpStatus.BAD_REQUEST, "FRIEND403",
"나를 차단한 회원입니다. 친구 요청을 보낼 수 없습니다."),

// 알림 관련 에러
NOTIFICATION_TYPE_NOT_FOUND(HttpStatus.NOT_FOUND, "NOTI401", "잘못된 알림 타입입니다."),
NOTIFICATION_METHOD_BAD_REQUEST(HttpStatus.BAD_REQUEST, "NOTI402", "알림 생성 메소드 호출이 잘못되었습니다."),

;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.gamegoo.apiPayload.exception.handler;

import com.gamegoo.apiPayload.code.BaseErrorCode;
import com.gamegoo.apiPayload.exception.GeneralException;

public class NotificationHandler extends GeneralException {

public NotificationHandler(BaseErrorCode code) {
super(code);
}
}
27 changes: 26 additions & 1 deletion src/main/java/com/gamegoo/controller/TestController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.gamegoo.controller;

import com.gamegoo.apiPayload.ApiResponse;
import com.gamegoo.apiPayload.code.status.ErrorStatus;
import com.gamegoo.apiPayload.exception.handler.TempHandler;
import com.gamegoo.domain.Member;
import com.gamegoo.domain.notification.NotificationTypeTitle;
import com.gamegoo.service.member.ProfileService;
import com.gamegoo.service.notification.NotificationService;
import com.gamegoo.util.JWTUtil;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -15,6 +22,9 @@
@Slf4j
public class TestController {

private final NotificationService notificationService;
private final ProfileService profileService;

@GetMapping("/test/hello")
@Operation(summary = "swagger 테스트용 API 입니다.", description = "simple API for swagger test!")
public String hello() {
Expand All @@ -26,5 +36,20 @@ public String hello() {
public String apiResponseTest() {
throw new TempHandler(ErrorStatus.TEMP_EXCEPTION);
}


@GetMapping("/test/send/notifications/{times}")
@Operation(summary = "테스트용 알림 생성 API 입니다.", description = "서버 테스트용 입니다!!")
public ApiResponse<String> sendTestNotifications(
@PathVariable(name = "times") int times
) {
Long memberId = JWTUtil.getCurrentUserId();
Member member = profileService.findMember(memberId);

for (int i = 0; i < times; i++) {
notificationService.createNotification(NotificationTypeTitle.TEST_ALARM, null, null,
member);
}
return ApiResponse.onSuccess("테스트 알림 생성 성공");
}

}
30 changes: 26 additions & 4 deletions src/main/java/com/gamegoo/domain/notification/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@

import com.gamegoo.domain.Member;
import com.gamegoo.domain.common.BaseDateTimeEntity;
import lombok.*;

import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Notification extends BaseDateTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "notification_id")
Expand All @@ -21,7 +32,9 @@ public class Notification extends BaseDateTimeEntity {
private String content;

@Column(nullable = false)
private Boolean isRead;
private boolean isRead;

private Long sourceId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
Expand All @@ -31,4 +44,13 @@ public class Notification extends BaseDateTimeEntity {
@JoinColumn(name = "notification_type_id", nullable = false)
private NotificationType notificationType;

// 연관관계 메소드
public void setMember(Member member) {
if (this.member != null) {
this.member.getNotificationList().remove(this);
}
this.member = member;
this.member.getNotificationList().add(this);
}

}
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
package com.gamegoo.domain.notification;

import com.gamegoo.domain.common.BaseDateTimeEntity;
import lombok.*;

import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class NotificationType extends BaseDateTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "notification_type_id")
private Long id;

@Column(nullable = false, length = 30)
private String name;
@Enumerated(EnumType.STRING)
@Column(columnDefinition = "VARCHAR(30)", nullable = false)
private NotificationTypeTitle title;

@Column(nullable = false, length = 400)
private String content;

@Column(nullable = false, length = 200)
private String imgUrl;

private String sourceUrl;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.gamegoo.domain.notification;

public enum NotificationTypeTitle {
FRIEND_REQUEST_SEND("님에게 친구 요청을 보냈어요.", null),
FRIEND_REQUEST_RECEIVED("님에게 친구 요청이 왔어요.", "/member/profile/"),
FRIEND_REQUEST_ACCEPTED("님이 친구를 수락했어요.", null),
FRIEND_REQUEST_REJECTED("님이 친구를 거절했어요.", null),
MANNER_LEVEL_UP("매너레벨이 n단계로 올라갔어요!", "/member/manner"),
MANNER_LEVEL_DOWN("매너레벨이 n단계로 떨어졌어요.", "/member/manner"),
MANNER_KEYWORD_RATED("지난 매칭에서 n 키워드를 받았어요.", "/member/manner"),
TEST_ALARM("TEST PUSH. NUMBER: ", null),
;

private final String content;
private final String sourceUrl;

NotificationTypeTitle(String content, String sourceUrl) {
this.content = content;
this.sourceUrl = sourceUrl;
}

public String getMessage() {
return content;
}

public String getSourceUrl() {
return sourceUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.gamegoo.repository.notification;

import com.gamegoo.domain.notification.Notification;
import org.springframework.data.jpa.repository.JpaRepository;

public interface NotificationRepository extends JpaRepository<Notification, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.gamegoo.repository.notification;

import com.gamegoo.domain.notification.NotificationType;
import com.gamegoo.domain.notification.NotificationTypeTitle;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface NotificationTypeRepository extends JpaRepository<NotificationType, Long> {

Optional<NotificationType> findNotificationTypeByTitle(NotificationTypeTitle title);

}
17 changes: 16 additions & 1 deletion src/main/java/com/gamegoo/service/member/FriendService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import com.gamegoo.domain.friend.Friend;
import com.gamegoo.domain.friend.FriendRequestStatus;
import com.gamegoo.domain.friend.FriendRequests;
import com.gamegoo.domain.notification.NotificationTypeTitle;
import com.gamegoo.repository.friend.FriendRepository;
import com.gamegoo.repository.friend.FriendRequestsRepository;
import com.gamegoo.service.notification.NotificationService;
import com.gamegoo.util.MemberUtils;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,6 +24,7 @@ public class FriendService {
private final FriendRepository friendRepository;
private final FriendRequestsRepository friendRequestsRepository;
private final ProfileService profileService;
private final NotificationService notificationService;

/**
* memberId에 해당하는 회원의 친구 목록 조회
Expand All @@ -47,6 +50,7 @@ public FriendRequests sendFriendRequest(Long memberId, Long targetMemberId) {

Member targetMember = profileService.findMember(targetMemberId);

// targetMember로 나 자신을 요청한 경우
if (member.equals(targetMember)) {
throw new FriendHandler(ErrorStatus.FRIEND_BAD_REQUEST);
}
Expand All @@ -67,6 +71,17 @@ public FriendRequests sendFriendRequest(Long memberId, Long targetMemberId) {
.toMember(targetMember)
.build();

return friendRequestsRepository.save(friendRequests);
FriendRequests savedFriendRequests = friendRequestsRepository.save(friendRequests);

// 친구 요청 알림 생성
// member -> targetMember
notificationService.createNotification(NotificationTypeTitle.FRIEND_REQUEST_SEND,
targetMember.getGameName(), null, member);

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

return savedFriendRequests;
}
}
Loading
Loading