-
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(messaging): 알림 발송을 위해 기기를 등록한다 (#183)
* feat(messaging): 알림 발송을 위해 기기를 등록한다 * feat(messaging): 한 기기는 한 계정당 한 번만 등록할 수 있다 * feat(messaging.notification): endpoint 수정 * feat(messaging.notification): endpoint 수정
- Loading branch information
Showing
11 changed files
with
189 additions
and
1 deletion.
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
25 changes: 25 additions & 0 deletions
25
...ain/java/com/snackgame/server/messaging/notification/controller/NotificationController.kt
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,25 @@ | ||
package com.snackgame.server.messaging.notification.controller | ||
|
||
import com.snackgame.server.auth.token.support.Authenticated | ||
import com.snackgame.server.member.domain.Member | ||
import com.snackgame.server.messaging.notification.service.NotificationService | ||
import com.snackgame.server.messaging.notification.service.dto.DeviceTokenRequest | ||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@Tag(name = "🔔 알림") | ||
@RestController | ||
class NotificationController(private val notificationService: NotificationService) { | ||
|
||
@Operation(summary = "기기 등록", description = "기기를 등록한다") | ||
@PostMapping("/notifications/devices") | ||
fun registerDevice( | ||
@Authenticated member: Member, | ||
@RequestBody deviceTokenRequest: DeviceTokenRequest | ||
) { | ||
notificationService.registerDeviceFor(member.id, deviceTokenRequest) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/snackgame/server/messaging/notification/domain/Device.kt
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,17 @@ | ||
package com.snackgame.server.messaging.notification.domain | ||
|
||
import javax.persistence.Entity | ||
import javax.persistence.GeneratedValue | ||
import javax.persistence.GenerationType | ||
import javax.persistence.Id | ||
import javax.persistence.Table | ||
import javax.persistence.UniqueConstraint | ||
|
||
@Entity | ||
@Table(uniqueConstraints = [UniqueConstraint(columnNames = ["ownerId", "token"])]) | ||
class Device( | ||
val ownerId: Long, | ||
val token: String, | ||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = 0 | ||
) |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/snackgame/server/messaging/notification/domain/DeviceRepository.kt
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,10 @@ | ||
package com.snackgame.server.messaging.notification.domain | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface DeviceRepository : JpaRepository<Device, Long> { | ||
|
||
fun existsByOwnerIdAndToken(ownerId: Long, token: String): Boolean | ||
|
||
fun findAllByOwnerId(ownerId: Long): List<Device> | ||
} |
5 changes: 5 additions & 0 deletions
5
...n/java/com/snackgame/server/messaging/notification/exception/DuplicatedDeviceException.kt
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,5 @@ | ||
package com.snackgame.server.messaging.notification.exception | ||
|
||
import com.snackgame.server.common.exception.Kind | ||
|
||
class DuplicatedDeviceException : NotificationException("이미 등록된 기기입니다", Kind.IGNORE) |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/snackgame/server/messaging/notification/exception/NotificationException.kt
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,9 @@ | ||
package com.snackgame.server.messaging.notification.exception | ||
|
||
import com.snackgame.server.common.exception.BusinessException | ||
import com.snackgame.server.common.exception.Kind | ||
|
||
abstract class NotificationException( | ||
message: String, | ||
kind: Kind = Kind.BAD_REQUEST | ||
) : BusinessException(kind, message) |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/snackgame/server/messaging/notification/service/NotificationService.kt
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,26 @@ | ||
package com.snackgame.server.messaging.notification.service | ||
|
||
import com.snackgame.server.messaging.notification.domain.Device | ||
import com.snackgame.server.messaging.notification.domain.DeviceRepository | ||
import com.snackgame.server.messaging.notification.exception.DuplicatedDeviceException | ||
import com.snackgame.server.messaging.notification.service.dto.DeviceResponse | ||
import com.snackgame.server.messaging.notification.service.dto.DeviceTokenRequest | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class NotificationService(private val deviceRepository: DeviceRepository) { | ||
|
||
fun registerDeviceFor(ownerId: Long, deviceToken: DeviceTokenRequest) { | ||
if (!deviceRepository.existsByOwnerIdAndToken(ownerId, deviceToken.token)) { | ||
Device(ownerId, deviceToken.token) | ||
.let { deviceRepository.save(it) } | ||
return | ||
} | ||
throw DuplicatedDeviceException() | ||
} | ||
|
||
fun getDevicesOf(ownerId: Long): List<DeviceResponse> { | ||
return deviceRepository.findAllByOwnerId(ownerId) | ||
.map { DeviceResponse.of(it) } | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/snackgame/server/messaging/notification/service/dto/DeviceResponse.kt
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,15 @@ | ||
package com.snackgame.server.messaging.notification.service.dto | ||
|
||
import com.snackgame.server.messaging.notification.domain.Device | ||
|
||
data class DeviceResponse( | ||
val ownerId: Long, | ||
val token: String, | ||
val id: Long | ||
) { | ||
|
||
companion object { | ||
|
||
fun of(device: Device) = DeviceResponse(device.ownerId, device.token, device.id) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/snackgame/server/messaging/notification/service/dto/DeviceTokenRequest.kt
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,10 @@ | ||
package com.snackgame.server.messaging.notification.service.dto | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator | ||
import com.fasterxml.jackson.annotation.JsonSetter | ||
import com.fasterxml.jackson.annotation.Nulls | ||
|
||
data class DeviceTokenRequest @JsonCreator constructor( | ||
@JsonSetter(nulls = Nulls.FAIL) | ||
val token: String | ||
) |
31 changes: 31 additions & 0 deletions
31
...java/com/snackgame/server/messaging/notification/controller/NotificationControllerTest.kt
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,31 @@ | ||
@file:Suppress("NonAsciiCharacters") | ||
|
||
package com.snackgame.server.messaging.notification.controller | ||
|
||
import com.snackgame.server.member.fixture.MemberFixture | ||
import com.snackgame.server.member.fixture.MemberFixture.땡칠_인증정보 | ||
import com.snackgame.server.messaging.notification.service.dto.DeviceTokenRequest | ||
import com.snackgame.server.support.restassured.RestAssuredTest | ||
import com.snackgame.server.support.restassured.RestAssuredUtil | ||
import io.restassured.http.ContentType | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.http.HttpStatus | ||
|
||
@RestAssuredTest | ||
class NotificationControllerTest { | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
MemberFixture.saveAll() | ||
} | ||
|
||
@Test | ||
fun `기기를 등록한다`() { | ||
RestAssuredUtil.givenAuthentication(땡칠_인증정보()) | ||
.contentType(ContentType.JSON) | ||
.body(DeviceTokenRequest("a_device_token")) | ||
.`when`().post("/notifications/devices") | ||
.then().statusCode(HttpStatus.OK.value()) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/com/snackgame/server/messaging/notification/service/NotificationServiceTest.kt
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,39 @@ | ||
@file:Suppress("NonAsciiCharacters") | ||
|
||
package com.snackgame.server.messaging.notification.service | ||
|
||
import com.snackgame.server.member.fixture.MemberFixture.땡칠 | ||
import com.snackgame.server.messaging.notification.exception.DuplicatedDeviceException | ||
import com.snackgame.server.messaging.notification.service.dto.DeviceTokenRequest | ||
import com.snackgame.server.support.general.ServiceTest | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
|
||
@ServiceTest | ||
class NotificationServiceTest { | ||
|
||
@Autowired | ||
private lateinit var notificationService: NotificationService | ||
|
||
@Test | ||
fun `기기를 등록한다`() { | ||
val deviceToken = "a_device_token" | ||
notificationService.registerDeviceFor(땡칠().id, DeviceTokenRequest(deviceToken)) | ||
|
||
assertThat(notificationService.getDevicesOf(땡칠().id)) | ||
.singleElement() | ||
.matches { it.ownerId == 땡칠().id } | ||
.matches { it.token == deviceToken } | ||
} | ||
|
||
@Test | ||
fun `한 기기는 한 계정당 한 번만 등록할 수 있다`() { | ||
val deviceToken = "a_device_token" | ||
notificationService.registerDeviceFor(땡칠().id, DeviceTokenRequest(deviceToken)) | ||
|
||
assertThatThrownBy { notificationService.registerDeviceFor(땡칠().id, DeviceTokenRequest(deviceToken)) } | ||
.isInstanceOf(DuplicatedDeviceException::class.java) | ||
} | ||
} |