From f21c9e8acf5e9606c999962f24f1d3639aae7020 Mon Sep 17 00:00:00 2001 From: ywonchae1 Date: Thu, 14 Nov 2024 16:43:44 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=ED=9A=8C=EC=9B=90=EC=97=90=20fcm?= =?UTF-8?q?=20=ED=86=A0=ED=81=B0=20=EC=BB=AC=EB=9F=BC=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=20(#115)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/domain/MemberEntity.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/ioteatime/meonghanyangserver/member/domain/MemberEntity.java b/src/main/java/org/ioteatime/meonghanyangserver/member/domain/MemberEntity.java index eefcf6cb..11ba884f 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/member/domain/MemberEntity.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/member/domain/MemberEntity.java @@ -1,16 +1,20 @@ package org.ioteatime.meonghanyangserver.member.domain; import jakarta.persistence.*; +import java.time.LocalDateTime; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import org.ioteatime.meonghanyangserver.common.exception.BadRequestException; import org.ioteatime.meonghanyangserver.common.type.AuthErrorType; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; @Getter @Entity @NoArgsConstructor @Table(name = "member") +@EntityListeners(AuditingEntityListener.class) public class MemberEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -27,14 +31,28 @@ public class MemberEntity { @Column private String profileImgUrl; + @Column private String fcmToken; + + @LastModifiedDate + @Column(nullable = false) + private LocalDateTime updatedAt; + @Builder public MemberEntity( - Long id, String email, String password, String nickname, String profileImgUrl) { + Long id, + String email, + String password, + String nickname, + String profileImgUrl, + String fcmToken, + LocalDateTime updatedAt) { this.id = id; this.email = email; this.password = password; this.nickname = nickname; this.profileImgUrl = profileImgUrl; + this.fcmToken = fcmToken; + this.updatedAt = updatedAt; } public void setPassword(String encodedPassword) { @@ -43,4 +61,9 @@ public void setPassword(String encodedPassword) { } this.password = encodedPassword; } + + public MemberEntity updateFcmToken(String token) { + this.fcmToken = token; + return this; + } } From 341d19ce7bd39093630000ef9b9d34ed3d05f3bb Mon Sep 17 00:00:00 2001 From: ywonchae1 Date: Thu, 14 Nov 2024 16:44:03 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20created=20at,=20updated=20at?= =?UTF-8?q?=EC=9D=84=20=EC=9C=84=ED=95=9C=20=EC=95=A0=EB=85=B8=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EC=B6=94=EA=B0=80=20(#115)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../meonghanyangserver/MeongHaNyangServerApplication.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/ioteatime/meonghanyangserver/MeongHaNyangServerApplication.java b/src/main/java/org/ioteatime/meonghanyangserver/MeongHaNyangServerApplication.java index a4dea894..4b79a9b3 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/MeongHaNyangServerApplication.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/MeongHaNyangServerApplication.java @@ -2,7 +2,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +@EnableJpaAuditing @SpringBootApplication public class MeongHaNyangServerApplication { From c7ddc64e9b5250c32466daf53698e668c0abc5b3 Mon Sep 17 00:00:00 2001 From: ywonchae1 Date: Thu, 14 Nov 2024 16:44:19 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20Fcm=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EA=B5=AC=ED=98=84=20(#115)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fcm/controller/FcmApi.java | 4 +++- .../fcm/controller/FcmController.java | 9 ++++++--- .../fcm/service/FcmService.java | 19 ++++++++++++------- .../member/repository/MemberRepository.java | 2 ++ .../repository/MemberRepositoryImpl.java | 13 ++++++------- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/ioteatime/meonghanyangserver/fcm/controller/FcmApi.java b/src/main/java/org/ioteatime/meonghanyangserver/fcm/controller/FcmApi.java index 9f861ba8..96c9ffd5 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/fcm/controller/FcmApi.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/fcm/controller/FcmApi.java @@ -3,11 +3,13 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.ioteatime.meonghanyangserver.common.api.Api; +import org.ioteatime.meonghanyangserver.common.utils.LoginMember; import org.ioteatime.meonghanyangserver.fcm.dto.request.SaveFcmTokenRequest; import org.springframework.web.bind.annotation.RequestBody; @Tag(name = "Fcm Api", description = "알림 관련 API 목록입니다.") public interface FcmApi { @Operation(summary = "FCM 토큰 저장", description = "담당자: 양원채") - Api saveToken(@RequestBody SaveFcmTokenRequest createFcmTokenRequest); + Api saveToken( + @LoginMember Long memberId, @RequestBody SaveFcmTokenRequest createFcmTokenRequest); } diff --git a/src/main/java/org/ioteatime/meonghanyangserver/fcm/controller/FcmController.java b/src/main/java/org/ioteatime/meonghanyangserver/fcm/controller/FcmController.java index 122bf345..c5645138 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/fcm/controller/FcmController.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/fcm/controller/FcmController.java @@ -3,21 +3,24 @@ import lombok.RequiredArgsConstructor; import org.ioteatime.meonghanyangserver.common.api.Api; import org.ioteatime.meonghanyangserver.common.type.FcmSuccessType; +import org.ioteatime.meonghanyangserver.common.utils.LoginMember; import org.ioteatime.meonghanyangserver.fcm.dto.request.SaveFcmTokenRequest; import org.ioteatime.meonghanyangserver.fcm.service.FcmService; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequiredArgsConstructor -@RequestMapping("/open-api/fcm") // 테스트 후 api로 변경하기 +@RequestMapping("/api/fcm") // 테스트 후 api로 변경하기 public class FcmController implements FcmApi { private final FcmService fcmService; @PostMapping("/token") - public Api saveToken(SaveFcmTokenRequest createFcmTokenRequest) { - fcmService.saveToken(createFcmTokenRequest.token()); + public Api saveToken( + @LoginMember Long memberId, @RequestBody SaveFcmTokenRequest createFcmTokenRequest) { + fcmService.saveToken(memberId, createFcmTokenRequest.token()); return Api.success(FcmSuccessType.SAVE); } } diff --git a/src/main/java/org/ioteatime/meonghanyangserver/fcm/service/FcmService.java b/src/main/java/org/ioteatime/meonghanyangserver/fcm/service/FcmService.java index 663fc9ba..0f668910 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/fcm/service/FcmService.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/fcm/service/FcmService.java @@ -1,22 +1,27 @@ package org.ioteatime.meonghanyangserver.fcm.service; +import jakarta.transaction.Transactional; import org.ioteatime.meonghanyangserver.clients.google.FcmClient; +import org.ioteatime.meonghanyangserver.common.exception.NotFoundException; +import org.ioteatime.meonghanyangserver.common.type.AuthErrorType; +import org.ioteatime.meonghanyangserver.member.repository.MemberRepository; import org.springframework.stereotype.Service; @Service public class FcmService { private final FcmClient fcmClient; + private final MemberRepository memberRepository; - public FcmService(FcmClient fcmClient) { + public FcmService(FcmClient fcmClient, MemberRepository memberRepository) { this.fcmClient = fcmClient; + this.memberRepository = memberRepository; fcmClient.init(); } - public void saveToken(String token) { - // TODO. 토큰을 DB에 저장하기 - // TODO. 로그인 계정 정보 불러오는 방식으로 구현하기 - // 현재는 테스트를 위해 token이 자동으로 hello Topic을 구독하고, 곧바로 hello Topic에 메시지를 보냄 - fcmClient.subTopic(token, "hello"); - fcmClient.sendPush("test-message", "test-content", "hello"); + @Transactional + public void saveToken(Long memberId, String token) { + memberRepository + .updateFcmTokenById(memberId, token) + .orElseThrow(() -> new NotFoundException(AuthErrorType.NOT_FOUND)); } } diff --git a/src/main/java/org/ioteatime/meonghanyangserver/member/repository/MemberRepository.java b/src/main/java/org/ioteatime/meonghanyangserver/member/repository/MemberRepository.java index dde144be..7ea7004d 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/member/repository/MemberRepository.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/member/repository/MemberRepository.java @@ -11,4 +11,6 @@ public interface MemberRepository { void deleteById(Long memberId); MemberEntity save(MemberEntity MemberEntity); + + Optional updateFcmTokenById(Long memberId, String token); } diff --git a/src/main/java/org/ioteatime/meonghanyangserver/member/repository/MemberRepositoryImpl.java b/src/main/java/org/ioteatime/meonghanyangserver/member/repository/MemberRepositoryImpl.java index 61c17db8..fa48f9a1 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/member/repository/MemberRepositoryImpl.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/member/repository/MemberRepositoryImpl.java @@ -1,6 +1,5 @@ package org.ioteatime.meonghanyangserver.member.repository; -// import static org.ioteatime.meonghanyangserver.groupmember.doamin.QDeviceEntity.deviceEntity; import static org.ioteatime.meonghanyangserver.member.domain.QMemberEntity.memberEntity; import com.querydsl.jpa.impl.JPAQueryFactory; @@ -8,7 +7,6 @@ import lombok.RequiredArgsConstructor; import org.ioteatime.meonghanyangserver.member.domain.MemberEntity; import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; @Repository @RequiredArgsConstructor @@ -27,16 +25,17 @@ public Optional findByEmail(String email) { } @Override - @Transactional public void deleteById(Long memberId) { - // - // queryFactory.delete(deviceEntity).where(deviceEntity.member.id.eq(memberId)).execute(); - - queryFactory.delete(memberEntity).where(memberEntity.id.eq(memberId)).execute(); + jpaMemberRepository.deleteById(memberId); } @Override public MemberEntity save(MemberEntity memberEntity) { return jpaMemberRepository.save(memberEntity); } + + @Override + public Optional updateFcmTokenById(Long memberId, String token) { + return jpaMemberRepository.findById(memberId).map(entity -> entity.updateFcmToken(token)); + } }