Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

알림 시간 설정 API 구현 #17

Merged
merged 15 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-validation")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.ondosee.common.adapter.notification

import com.ondosee.common.spi.notification.CommandNotificationPort
import com.ondosee.common.spi.notification.QueryNotificationPort
import com.ondosee.domain.notification.domain.entity.Notification
import com.ondosee.domain.notification.domain.repository.NotificationRepository
import org.springframework.stereotype.Component

@Component
class CommandNotificationAdapter(
private val queryNotificationPort: QueryNotificationPort
private val notificationRepository: NotificationRepository
) : CommandNotificationPort {
override fun deleteByDeviceToken(deviceToken: String) {
notificationRepository.deleteByDeviceToken(deviceToken)
}

override fun saveDeviceToken(deviceToken: String) {

override fun saveAlarm(deviceToken: String, alarmTime: String) {
val notification = Notification(
deviceToken = deviceToken,
alarmTime = null
alarmTime = alarmTime
)

queryNotificationPort.saveNotification(notification)
notificationRepository.save(notification)
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.ondosee.common.adapter.notification

import com.ondosee.common.spi.notification.QueryNotificationPort
import com.ondosee.domain.notification.domain.entity.Notification
import com.ondosee.domain.notification.domain.repository.NotificationRepository
import org.springframework.stereotype.Component

@Component
class QueryNotificationAdapter(
private val notificationRepository: NotificationRepository
) : QueryNotificationPort {

override fun saveNotification(notification: Notification): Notification {

return notificationRepository.save(notification)
override fun existByDeviceToken(deviceToken: String): Boolean {
return notificationRepository.existsByDeviceToken(deviceToken)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ondosee.common.spi.notification

interface CommandNotificationPort {
fun saveDeviceToken(deviceToken: String)
fun deleteByDeviceToken(deviceToken: String)
fun saveAlarm(deviceToken: String, alarmTime: String)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.ondosee.common.spi.notification

import com.ondosee.domain.notification.domain.entity.Notification

interface QueryNotificationPort {
fun saveNotification(notification: Notification): Notification
fun existByDeviceToken(deviceToken: String): Boolean
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.ondosee.domain.notification.domain.entity

import java.time.LocalDateTime
import javax.persistence.*

@Entity
Expand All @@ -12,8 +11,8 @@ data class Notification(
val id: Long = 0,

@Column(name = "device_token")
val deviceToken: String,
val deviceToken: String = "",

@Column(name = "alarm_time")
var alarmTime: LocalDateTime?
val alarmTime: String? = null
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.ondosee.domain.notification.domain.repository

import com.ondosee.domain.notification.domain.entity.Notification
import org.springframework.data.repository.CrudRepository
import org.springframework.data.jpa.repository.JpaRepository

interface NotificationRepository : CrudRepository<Notification, Long> {
interface NotificationRepository : JpaRepository<Notification, Long> {
fun existsByDeviceToken(deviceToken: String): Boolean
fun deleteByDeviceToken(deviceToken: String)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.ondosee.domain.notification.presentation

import com.ondosee.domain.notification.presentation.web.req.SetAlarmWebRequest
import com.ondosee.domain.notification.service.NotificationService
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
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.RequestParam
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid

@RestController
@RequestMapping("/notification")
class NotificationController(
private val notificationService: NotificationService
) {
@PostMapping
fun saveNotification(@RequestParam deviceToken: String): ResponseEntity<Void> =
notificationService.saveNotification(deviceToken)
fun setAlarm(@Valid @RequestBody webRequest: SetAlarmWebRequest): ResponseEntity<Unit> =
notificationService.setAlarm(webRequest)
.let { ResponseEntity.status(HttpStatus.CREATED).build() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ondosee.domain.notification.presentation.web.req

import javax.validation.constraints.Pattern

data class SetAlarmWebRequest(
val deviceToken: String,

@field:Pattern(regexp = "^([01]\\d|2[0-3]):([0-5]\\d)$")
val alarmTime: String
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ondosee.domain.notification.service

import com.ondosee.domain.notification.presentation.web.req.SetAlarmWebRequest

interface NotificationService {
fun saveNotification(deviceToken: String)
fun setAlarm(webRequest: SetAlarmWebRequest)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package com.ondosee.domain.notification.service

import com.ondosee.common.spi.notification.CommandNotificationPort
import com.ondosee.common.spi.notification.QueryNotificationPort
import com.ondosee.domain.notification.presentation.web.req.SetAlarmWebRequest
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional(rollbackFor = [Exception::class])
class NotificationServiceImpl(
private val queryNotificationPort: QueryNotificationPort,
private val commandNotificationPort: CommandNotificationPort
) : NotificationService {
override fun saveNotification(deviceToken: String) {
commandNotificationPort.saveDeviceToken(deviceToken)
override fun setAlarm(webRequest: SetAlarmWebRequest) {
with(webRequest) {
if (queryNotificationPort.existByDeviceToken(deviceToken)) {
commandNotificationPort.deleteByDeviceToken(deviceToken)
}

commandNotificationPort.saveAlarm(deviceToken, alarmTime)
}
}
}
Loading