Skip to content

Commit

Permalink
feat: #39 device token ์ €์žฅ API
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Feb 27, 2024
1 parent 55ad7ff commit 1b07fc9
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import kr.co.fitapet.api.apis.profile.dto.DeviceTokenReq;
import kr.co.fitapet.api.apis.profile.usecase.MemberAccountUseCase;
import kr.co.fitapet.api.common.response.SuccessResponse;
import kr.co.fitapet.api.common.security.authentication.CustomUserDetails;
Expand Down Expand Up @@ -51,6 +52,14 @@ public ResponseEntity<?> getProfile(@PathVariable("id") Long id) {
return ResponseEntity.ok(SuccessResponse.from(member));
}

@Operation(summary = "๋””๋ฐ”์ด์Šค ํ† ํฐ ๋“ฑ๋ก")
@PostMapping("/{id}/device-token")
@PreAuthorize("isAuthenticated() and #id == principal.userId")
public ResponseEntity<?> postDeviceToken(@PathVariable("id") Long id, @RequestBody @Valid DeviceTokenReq req) {
memberAccountUseCase.registerDeviceToken(id, req);
return ResponseEntity.ok(SuccessResponse.noContent());
}

@Operation(summary = "ํ”„๋กœํ•„ ๊ฒ€์ƒ‰")
@Parameter(name = "search", description = "๊ฒ€์ƒ‰ํ•  ๋‹‰๋„ค์ž„", in = ParameterIn.QUERY, required = true)
@GetMapping("")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kr.co.fitapet.api.apis.profile.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import kr.co.fitapet.domain.domains.device.domain.DeviceToken;
import kr.co.fitapet.domain.domains.member.domain.Member;

@Schema(description = "๋””๋ฐ”์ด์Šค ํ† ํฐ ๋“ฑ๋ก ์š”์ฒญ")
public record DeviceTokenReq(
@Schema(description = "๋””๋ฐ”์ด์Šค ํ† ํฐ", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank
String deviceToken,
@Schema(description = "๋””๋ฐ”์ด์Šค OS", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank
String os,
@Schema(description = "๋””๋ฐ”์ด์Šค ๋ชจ๋ธ๋ช…", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank
String deviceModel
) {
public DeviceToken toEntity(Member member) {
return DeviceToken.of(deviceToken, os, deviceModel, member);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package kr.co.fitapet.api.apis.profile.usecase;

import kr.co.fitapet.api.apis.auth.mapper.SmsRedisMapper;
import kr.co.fitapet.api.apis.profile.dto.AccountSearchReq;
import kr.co.fitapet.api.apis.profile.dto.DeviceTokenReq;
import kr.co.fitapet.api.apis.profile.dto.ProfilePatchReq;
import kr.co.fitapet.common.annotation.UseCase;
import kr.co.fitapet.common.execption.BaseErrorCode;
import kr.co.fitapet.common.execption.GlobalErrorException;
import kr.co.fitapet.domain.common.redis.sms.type.SmsPrefix;
import kr.co.fitapet.domain.domains.device.domain.DeviceToken;
import kr.co.fitapet.domain.domains.device.service.DeviceTokenSaveService;
import kr.co.fitapet.domain.domains.device.service.DeviceTokenSearchService;
import kr.co.fitapet.domain.domains.manager.domain.Manager;
import kr.co.fitapet.domain.domains.manager.service.ManagerSearchService;
import kr.co.fitapet.domain.domains.member.domain.Member;
import kr.co.fitapet.domain.domains.member.domain.MemberNickname;
import kr.co.fitapet.domain.domains.member.dto.AccountProfileRes;
import kr.co.fitapet.api.apis.profile.dto.AccountSearchReq;
import kr.co.fitapet.api.apis.profile.dto.ProfilePatchReq;
import kr.co.fitapet.domain.domains.member.dto.MemberInfo;
import kr.co.fitapet.domain.domains.member.dto.UidRes;
import kr.co.fitapet.domain.domains.member.exception.AccountErrorCode;
Expand Down Expand Up @@ -41,9 +45,11 @@
@Slf4j
public class MemberAccountUseCase {
private final MemberSearchService memberSearchService;

private final ManagerSearchService managerSearchService;

private final DeviceTokenSearchService deviceTokenSearchService;
private final DeviceTokenSaveService deviceTokenSaveService;

private final ScheduleSearchService scheduleSearchService;
private final MemoSearchService memoSearchService;

Expand All @@ -57,6 +63,17 @@ public AccountProfileRes getProfile(Long userId) {
return AccountProfileRes.from(member);
}

@Transactional
public void registerDeviceToken(Long memberId, DeviceTokenReq req) {
Member member = memberSearchService.findById(memberId);

if (!deviceTokenSearchService.isExistByMemberIdAndDeviceToken(memberId, req.deviceToken())) {
log.info("๋””๋ฐ”์ด์Šค ํ† ํฐ ๋“ฑ๋ก: {}", req);
DeviceToken deviceToken = req.toEntity(member);
deviceTokenSaveService.save(deviceToken);
}
}

@Transactional(readOnly = true)
public MemberInfo searchProfile(Long requesterId, String search) {
return memberSearchService.findMemberInfo(requesterId, search);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DeviceToken {
private Long id;
private String deviceToken;
private String os;
private String deviceName;
private String deviceModel;

@CreatedDate
private LocalDateTime createdAt;
Expand All @@ -29,10 +29,10 @@ public class DeviceToken {
@ManyToOne(fetch = FetchType.LAZY)
private Member member;

private DeviceToken(String deviceToken, String os, String deviceName, Member member) {
private DeviceToken(String deviceToken, String os, String deviceModel, Member member) {
this.deviceToken = deviceToken;
this.os = os;
this.deviceName = deviceName;
this.deviceModel = deviceModel;
this.member = member;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

public interface DeviceTokenRepository extends JpaRepository<DeviceToken, Long> {
List<DeviceToken> findAllByMember_Id(Long userId);
boolean existsByDeviceToken(String deviceToken);
boolean existsByMember_idAndDeviceToken(Long memberId, String deviceToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kr.co.fitapet.domain.domains.device.service;

import kr.co.fitapet.common.annotation.DomainService;
import kr.co.fitapet.domain.domains.device.domain.DeviceToken;
import kr.co.fitapet.domain.domains.device.repository.DeviceTokenRepository;
import lombok.RequiredArgsConstructor;

@DomainService
@RequiredArgsConstructor
public class DeviceTokenSaveService {
private final DeviceTokenRepository deviceTokenRepository;

public void save(DeviceToken deviceToken) {
deviceTokenRepository.save(deviceToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public List<DeviceToken> findAll() {
}

@Transactional(readOnly = true)
public boolean isExistDeviceToken(String deviceToken) {
return deviceTokenRepository.existsByDeviceToken(deviceToken);
public boolean isExistByMemberIdAndDeviceToken(Long memberId, String deviceToken) {
return deviceTokenRepository.existsByMember_idAndDeviceToken(memberId, deviceToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

/**
* FCM ๋ฉ”์‹œ์ง€ ์ „์†ก ์š”์ฒญ
* @param tokens : ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์„ ๋””๋ฐ”์ด์Šค ํ† ํฐ ๋ฆฌ์ŠคํŠธ
* @param topic : ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์„ ํ† ํ”ฝ (tokens๊ฐ€ ์—†์„ ๋•Œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)
* @param token : ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์„ ๋””๋ฐ”์ด์Šค ํ† ํฐ ๋ฆฌ์ŠคํŠธ
* @param title : ๋ฉ”์‹œ์ง€ ์ œ๋ชฉ
* @param content : ๋ฉ”์‹œ์ง€ ๋‚ด์šฉ
*
*/
@Builder
public record NotificationSingleRequest(
Expand Down

0 comments on commit 1b07fc9

Please sign in to comment.