Skip to content

Commit

Permalink
feat: notification 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
HyungJu committed Mar 23, 2024
1 parent 1848a37 commit b281539
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/com/vacgom/backend/member/domain/Member.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.vacgom.backend.auth.domain.constants.ProviderType
import com.vacgom.backend.auth.domain.constants.Role
import com.vacgom.backend.global.auditing.BaseEntity
import com.vacgom.backend.inoculation.domain.Inoculation
import com.vacgom.backend.notification.domain.Notification
import jakarta.persistence.*
import org.hibernate.annotations.GenericGenerator
import java.util.*
Expand Down Expand Up @@ -32,6 +33,9 @@ class Member(
@OneToMany(mappedBy = "member", fetch = FetchType.EAGER, cascade = [CascadeType.ALL])
var healthProfiles: MutableList<HealthProfile> = mutableListOf()

@OneToMany(mappedBy = "member", fetch = FetchType.EAGER, cascade = [CascadeType.ALL])
val notifications: MutableList<Notification> = mutableListOf()

fun addInoculations(inoculations: List<Inoculation>) {
this.inoculations.addAll(inoculations)
}
Expand Down
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))
}
}
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()
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>
}
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())
}
}
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,
)
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,
)
}
}
}

0 comments on commit b281539

Please sign in to comment.