diff --git a/src/main/java/com/nexters/goalpanzi/application/device/DeviceService.java b/src/main/java/com/nexters/goalpanzi/application/device/DeviceService.java index 3d56142..4c00a5f 100644 --- a/src/main/java/com/nexters/goalpanzi/application/device/DeviceService.java +++ b/src/main/java/com/nexters/goalpanzi/application/device/DeviceService.java @@ -26,14 +26,17 @@ public class DeviceService { @Transactional public void updateDeviceToken(final UpdateDeviceTokenCommand command) { - if (command.deviceIdentifier().isBlank() // TODO: 추후 앞의 조건 삭제 - || !deviceRepository.existsByDeviceIdentifier(command.deviceIdentifier())) { - createDevice(command.memberId(), command.deviceIdentifier(), command.deviceToken(), command.osType()); - } else { + if (existsMemberDevice(command.memberId(), command.deviceIdentifier())) { updateDevice(command.memberId(), command.deviceIdentifier(), command.deviceToken()); + } else { + createDevice(command.memberId(), command.deviceIdentifier(), command.deviceToken(), command.osType()); } } + private boolean existsMemberDevice(final Long memberId, final String deviceIdentifier) { + return !deviceIdentifier.isBlank() && deviceRepository.existsByMemberIdAndDeviceIdentifier(memberId, deviceIdentifier); + } + private void createDevice(final Long memberId, final String deviceIdentifier, final String deviceToken, final OsType osType) { Member member = memberRepository.getMember(memberId); Device device = deviceRepository.save(new Device(member, deviceIdentifier, deviceToken, osType)); diff --git a/src/main/java/com/nexters/goalpanzi/domain/device/repository/DeviceRepository.java b/src/main/java/com/nexters/goalpanzi/domain/device/repository/DeviceRepository.java index 5e2c037..7ea0204 100644 --- a/src/main/java/com/nexters/goalpanzi/domain/device/repository/DeviceRepository.java +++ b/src/main/java/com/nexters/goalpanzi/domain/device/repository/DeviceRepository.java @@ -18,7 +18,7 @@ public interface DeviceRepository extends JpaRepository { @Query("SELECT d FROM Device d JOIN FETCH d.member WHERE d.deviceIdentifier = :deviceIdentifier") List findAllWithMemberByDeviceIdentifier(final String deviceIdentifier); - boolean existsByDeviceIdentifier(final String deviceIdentifier); + boolean existsByMemberIdAndDeviceIdentifier(final Long memberId, final String deviceIdentifier); default Device getDevice(final Long deviceId) { return findById(deviceId)