Skip to content

Commit

Permalink
Merge pull request #116 from IoTeaTime/feature/115-fcm-token-refresh
Browse files Browse the repository at this point in the history
feat: FCM 토큰 저장 구현
  • Loading branch information
ywonchae1 authored Nov 14, 2024
2 parents 739c412 + c7ddc64 commit d345a61
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) {
Expand All @@ -43,4 +61,9 @@ public void setPassword(String encodedPassword) {
}
this.password = encodedPassword;
}

public MemberEntity updateFcmToken(String token) {
this.fcmToken = token;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface MemberRepository {
void deleteById(Long memberId);

MemberEntity save(MemberEntity MemberEntity);

Optional<MemberEntity> updateFcmTokenById(Long memberId, String token);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.ioteatime.meonghanyangserver.member.domain.MemberEntity;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@RequiredArgsConstructor
Expand All @@ -27,16 +25,17 @@ public Optional<MemberEntity> 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<MemberEntity> updateFcmTokenById(Long memberId, String token) {
return jpaMemberRepository.findById(memberId).map(entity -> entity.updateFcmToken(token));
}
}

0 comments on commit d345a61

Please sign in to comment.