-
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.
- Loading branch information
Showing
7 changed files
with
162 additions
and
0 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
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/com/vacgom/backend/notification/application/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,49 @@ | ||
package com.vacgom.backend.notification.application | ||
|
||
import com.vacgom.backend.global.exception.error.BusinessException | ||
import com.vacgom.backend.member.application.MemberService | ||
import com.vacgom.backend.member.exception.MemberError | ||
import com.vacgom.backend.notification.domain.Notification | ||
import com.vacgom.backend.notification.domain.NotificationRepository | ||
import org.springframework.stereotype.Service | ||
import java.util.* | ||
|
||
@Service | ||
class NotificationService( | ||
private val notificationRepository: NotificationRepository, | ||
private val memberService: MemberService, | ||
) { | ||
fun getAllNotifications(memberId: UUID): List<Notification> { | ||
return notificationRepository.findAllByMemberId(memberId) | ||
} | ||
|
||
fun getAllUnreadNotifications(memberId: UUID): List<Notification> { | ||
return notificationRepository.findAllByMemberId(memberId).filter { it.isRead == false } | ||
} | ||
|
||
fun setAsRead(notificationId: Long) { | ||
val notification = | ||
notificationRepository.findById(notificationId) | ||
.orElseThrow { throw BusinessException(MemberError.NOT_FOUND) } | ||
notification.isRead = true | ||
notificationRepository.save(notification) | ||
} | ||
|
||
fun setAllAsRead(memberId: UUID) { | ||
val notifications = | ||
notificationRepository.findAllByMemberId(memberId).map { | ||
it.isRead = true | ||
it | ||
} | ||
notificationRepository.saveAll(notifications) | ||
} | ||
|
||
fun sendNotification( | ||
memberId: UUID, | ||
content: String, | ||
type: String, | ||
): Notification { | ||
val member = memberService.findMember(memberId) ?: throw BusinessException(MemberError.NOT_FOUND) | ||
return notificationRepository.save(Notification(member = member, content = content, type = type)) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/com/vacgom/backend/notification/domain/Notification.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,20 @@ | ||
package com.vacgom.backend.notification.domain | ||
|
||
import com.vacgom.backend.global.auditing.BaseEntity | ||
import com.vacgom.backend.member.domain.Member | ||
import jakarta.persistence.* | ||
|
||
@Entity | ||
@Table(name = "t_notification") | ||
class Notification( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "notification_id") | ||
val id: Long? = null, | ||
val content: String, | ||
val type: String, | ||
@ManyToOne | ||
@JoinColumn(name = "member_id") | ||
val member: Member, | ||
var isRead: Boolean = false, | ||
) : BaseEntity() |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/vacgom/backend/notification/domain/NotificationRepository.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,8 @@ | ||
package com.vacgom.backend.notification.domain | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import java.util.* | ||
|
||
interface NotificationRepository : JpaRepository<Notification, Long> { | ||
fun findAllByMemberId(memberId: UUID): List<Notification> | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/kotlin/com/vacgom/backend/notification/presentation/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,56 @@ | ||
package com.vacgom.backend.notification.presentation | ||
|
||
import com.vacgom.backend.global.security.annotation.AuthId | ||
import com.vacgom.backend.member.application.MemberService | ||
import com.vacgom.backend.notification.application.NotificationService | ||
import com.vacgom.backend.notification.presentation.dto.NotificationRequest | ||
import com.vacgom.backend.notification.presentation.dto.NotificationResponse | ||
import org.springframework.web.bind.annotation.* | ||
import java.util.* | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/notifications") | ||
class NotificationController( | ||
val memberService: MemberService, | ||
val notificationService: NotificationService, | ||
) { | ||
@GetMapping("/") | ||
fun getAllNotifications( | ||
@AuthId id: UUID, | ||
): List<NotificationResponse> { | ||
return notificationService.getAllNotifications(id).map { | ||
NotificationResponse.of(it) | ||
} | ||
} | ||
|
||
@PostMapping("/users/{userId}") | ||
fun sendNotification( | ||
@PathVariable("userId") userId: String, | ||
@RequestBody() body: NotificationRequest, | ||
) { | ||
notificationService.sendNotification(UUID.fromString(userId), body.content, body.type) | ||
} | ||
|
||
@GetMapping("/unread") | ||
fun getUnreadNotifications( | ||
@AuthId id: UUID, | ||
): List<NotificationResponse> { | ||
return notificationService.getAllUnreadNotifications(id).map { | ||
NotificationResponse.of(it) | ||
} | ||
} | ||
|
||
@PostMapping("/markAllAsRead") | ||
fun markAllAsRead( | ||
@AuthId id: UUID, | ||
) { | ||
notificationService.setAllAsRead(id) | ||
} | ||
|
||
@PostMapping("/{notificationId}/markAsRead") | ||
fun markAsRead( | ||
@PathVariable("notificationId") notificationId: String, | ||
) { | ||
notificationService.setAsRead(notificationId.toLong()) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/vacgom/backend/notification/presentation/dto/NotificationRequest.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,6 @@ | ||
package com.vacgom.backend.notification.presentation.dto | ||
|
||
class NotificationRequest( | ||
val content: String, | ||
val type: String, | ||
) |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/vacgom/backend/notification/presentation/dto/NotificationResponse.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,19 @@ | ||
package com.vacgom.backend.notification.presentation.dto | ||
|
||
import com.vacgom.backend.notification.domain.Notification | ||
|
||
class NotificationResponse( | ||
val id: Long, | ||
val content: String, | ||
val type: String, | ||
) { | ||
companion object { | ||
fun of(notification: Notification): NotificationResponse { | ||
return NotificationResponse( | ||
id = notification.id!!, | ||
content = notification.content, | ||
type = notification.type, | ||
) | ||
} | ||
} | ||
} |