-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(notification): change memory storage -> mongoDB storage (#210)
* ✨ feat: create UserDeviceRepository * ✨ chore: find device token by userId * ✨ chore: find all device tokens * ✨ feat: save token * ✨ feat: update token * ✨ feat: delete token * ✨ chore: combine saveToken and insertToken * ✨ chore: add Transactional annotation * ✨ chore: change TokenRepository -> UserDeviceRepository in PushService * ✨ chore: change TokenRepository -> UserDeviceRepository in TopicService * ✨ chore: delete TokenRepository and MemoryTokenRepository * ✨ chore: rename createToken -> saveToken * ✨ feat: save notification * 🐛 fix: fix Query annotation of findAllDeviceTokens * ♻️ refactor: delete UserDevice * ♻️ refactor: delete deleteToken service logic * ♻️ refactor: change findDeviceTokenByUserId -> findByUserId * ♻️ refactor: find tokens using findDeviceTokenByUserId -> findByUserId * ♻️ refactor: add transactional annotation * ♻️ refactor: extract getting tokens logic into private method * ♻️ refactor: give space * ♻️ refactor: change token not found error message * 🔥 chore: remove Param annotation * ♻️ refactor: find tokens using findByUserId -> findDeviceTokenByUserId * ♻️ refactor: change token not found error message * ♻️ refactor: remove unused import statement * ♻️ refactor: notificationList save -> saveAll
- Loading branch information
Showing
10 changed files
with
178 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
...streetdrop-notification/src/main/java/com/depromeet/repository/MemoryTokenRepository.java
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
backend/streetdrop-notification/src/main/java/com/depromeet/repository/TokenRepository.java
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
.../streetdrop-notification/src/main/java/com/depromeet/repository/UserDeviceRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.depromeet.repository; | ||
|
||
import com.depromeet.domain.UserDevice; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.data.mongodb.repository.Query; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface UserDeviceRepository extends MongoRepository<UserDevice, String> { | ||
|
||
@Query(value = "{ 'userId' : ?0 }") | ||
Optional<UserDevice> findByUserId(Long userId); | ||
|
||
@Query(value = "{ 'userId' : ?0 }", fields = "{ 'deviceToken' : 1 }") | ||
Optional<String> findDeviceTokenByUserId(Long userId); | ||
|
||
@Query(value = "{}", fields = "{ 'deviceToken' : 1 }") | ||
List<String> findAllDeviceTokens(); | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
backend/streetdrop-notification/src/main/java/com/depromeet/service/NotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.depromeet.service; | ||
|
||
import com.depromeet.domain.Notification; | ||
import com.depromeet.domain.Target; | ||
import com.depromeet.domain.User; | ||
import com.depromeet.domain.UserDevice; | ||
import com.depromeet.domain.vo.Channel; | ||
import com.depromeet.dto.request.AllPushRequestDto; | ||
import com.depromeet.dto.request.PushRequestDto; | ||
import com.depromeet.dto.request.TopicPushRequestDto; | ||
import com.depromeet.repository.NotificationRepository; | ||
import com.depromeet.repository.UserDeviceRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class NotificationService { | ||
|
||
private final NotificationRepository notificationRepository; | ||
private final UserDeviceRepository userDeviceRepository; | ||
|
||
@Transactional | ||
public void save(PushRequestDto pushRequestDto) { | ||
List<Notification> notificationList = new ArrayList<>(); | ||
|
||
for (Long userId : pushRequestDto.getUserIds()) { | ||
var userDevice = userDeviceRepository.findByUserId(userId) | ||
.orElseThrow(() -> new RuntimeException("Token not found for userId: " + userId)); | ||
User user = User.builder() | ||
.userId(userId) | ||
.deviceToken(userDevice.getDeviceToken()) | ||
.build(); | ||
|
||
Target target = Target.builder() | ||
.channel(Channel.GENERAL) | ||
.build(); | ||
|
||
Notification notification = Notification.builder() | ||
.user(user) | ||
.target(target) | ||
.title(pushRequestDto.getTitle()) | ||
.content(pushRequestDto.getContent()) | ||
.build(); | ||
|
||
notificationList.add(notification); | ||
} | ||
|
||
notificationRepository.saveAll(notificationList); | ||
} | ||
|
||
|
||
@Transactional | ||
public void save(AllPushRequestDto pushRequestDto) { | ||
List<Notification> notificationList = new ArrayList<>(); | ||
|
||
List<UserDevice> userDevices = userDeviceRepository.findAll(); | ||
for (UserDevice userDevice : userDevices) { | ||
User user = User.builder() | ||
.userId(userDevice.getUserId()) | ||
.deviceToken(userDevice.getDeviceToken()) | ||
.build(); | ||
|
||
Target target = Target.builder() | ||
.channel(Channel.GENERAL) | ||
.build(); | ||
|
||
Notification notification = Notification.builder() | ||
.user(user) | ||
.target(target) | ||
.title(pushRequestDto.getTitle()) | ||
.content(pushRequestDto.getContent()) | ||
.build(); | ||
|
||
notificationList.add(notification); | ||
} | ||
|
||
notificationRepository.saveAll(notificationList); | ||
} | ||
|
||
@Transactional | ||
public void save(TopicPushRequestDto tokenPushRequestDto) { | ||
// TODO: 토픽 저장에 따른 스키마 변경 후 구체화 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 23 additions & 9 deletions
32
backend/streetdrop-notification/src/main/java/com/depromeet/service/TokenService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,39 @@ | ||
package com.depromeet.service; | ||
|
||
import com.depromeet.domain.UserDevice; | ||
import com.depromeet.dto.request.TokenRequestDto; | ||
import com.depromeet.repository.TokenRepository; | ||
import com.depromeet.repository.UserDeviceRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TokenService { | ||
|
||
private final TokenRepository tokenRepository; | ||
|
||
public void createToken(TokenRequestDto tokenRequestDto) { | ||
tokenRepository.save(tokenRequestDto.getUserId(), tokenRequestDto.getToken()); | ||
} | ||
private final UserDeviceRepository userDeviceRepository; | ||
|
||
public void updateToken(TokenRequestDto tokenRequestDto) { | ||
tokenRepository.update(tokenRequestDto.getUserId(), tokenRequestDto.getToken()); | ||
@Transactional | ||
public void saveToken(TokenRequestDto tokenRequestDto) { | ||
Optional<UserDevice> userDevice = userDeviceRepository.findByUserId(tokenRequestDto.getUserId()); | ||
if (userDevice.isPresent()) { | ||
UserDevice updatedUserDevice = userDevice.get().updateDeviceToken(tokenRequestDto.getToken()); | ||
userDeviceRepository.save(updatedUserDevice); | ||
} else { | ||
UserDevice createdUserDevice = UserDevice.builder() | ||
.userId(tokenRequestDto.getUserId()) | ||
.deviceToken(tokenRequestDto.getToken()) | ||
.build(); | ||
userDeviceRepository.save(createdUserDevice); | ||
} | ||
} | ||
|
||
@Transactional | ||
public void deleteToken(Long userId) { | ||
tokenRepository.delete(userId); | ||
var userDevice = userDeviceRepository.findByUserId(userId) | ||
.orElseThrow(() -> new RuntimeException("Token not found for userId: " + userId)); | ||
userDeviceRepository.delete(userDevice); | ||
} | ||
} |
Oops, something went wrong.