Skip to content

Commit

Permalink
[Vacgom-142] 아기 삭제 API 구현 (#48)
Browse files Browse the repository at this point in the history
* feat: 아이 삭제 API 구현

* test: 아이 삭제 application 계층 테스트 코드 작성
  • Loading branch information
whereami2048 authored Jan 13, 2025
1 parent 671baec commit a85edc4
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class BabyCommandService(
}
}

fun deleteById(userId: UUID, babyId: UUID) {
babyManagerService.getBabyByIdAndUserIsAdmin(userId, babyId).let {
babyRepository.deleteBaby(it)
}
}

fun saveAll(babies: List<Baby>): List<Baby> {
return babyRepository.saveAll(babies)
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/kr/co/vacgom/api/baby/domain/Baby.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package kr.co.vacgom.api.baby.domain

import jakarta.persistence.*
import kr.co.vacgom.api.baby.domain.enums.Gender
import kr.co.vacgom.api.babymanager.domain.BabyManager
import kr.co.vacgom.api.carehistoryitem.domain.CareHistoryItem
import kr.co.vacgom.api.carehistoryitem.domain.enums.CareHistoryItemType
import kr.co.vacgom.api.global.common.domain.BaseTimeEntity
import kr.co.vacgom.api.global.util.UuidCreator
import kr.co.vacgom.api.vaccine.domain.UnclassifiedVaccination
import kr.co.vacgom.api.vaccine.domain.Vaccination
import org.hibernate.annotations.Comment
import java.time.LocalDate
import java.util.*
Expand Down Expand Up @@ -43,6 +48,19 @@ class Baby(
var birthday: LocalDate = birthday
protected set

@OneToMany(mappedBy = "baby", cascade = [(CascadeType.REMOVE)])
val babyManagers: List<BabyManager> = listOf()

@OneToMany(mappedBy = "baby", cascade = [(CascadeType.REMOVE)])
@MapKey(name = "itemType")
val careHistoryItems: Map<CareHistoryItemType, CareHistoryItem> = mapOf()

@OneToMany(mappedBy = "baby", cascade = [(CascadeType.REMOVE)])
val vaccinations: List<Vaccination> = listOf()

@OneToMany(mappedBy = "baby", cascade = [(CascadeType.REMOVE)])
val unclassifiedVaccinations: List<UnclassifiedVaccination> = listOf()

fun update(
name: String,
profileImg: String?,
Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/kr/co/vacgom/api/baby/presentation/BabyApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ interface BabyApi {
)
fun updateBaby(babyId: UUID, request: BabyDto.Request.Update): BaseResponse<BabyDto.Response.Detail>

@Operation(
summary = "아이 삭제 API",
operationId = "deleteBabyById",
description = """""",
responses = [
ApiResponse(responseCode = "200", description = "OK"),
ApiResponse(
responseCode = "400",
description = "Bad Request",
content = [Content(schema = Schema(implementation = ErrorResponse::class))]
),
]
)
fun deleteBabyById(babyId: UUID)

companion object {
const val BABY = "/babies"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ class BabyController(
): BaseResponse<BabyDto.Response.Detail> {
return babyCommandService.updateBabyInfo(babyId, request).let { BaseResponse.success(it) }
}

@DeleteMapping("/{babyId}")
override fun deleteBabyById(@PathVariable babyId: UUID) {
val userId = SecurityContextUtil.getPrincipal()
babyCommandService.deleteById(userId, babyId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ interface BabyRepository {
fun findBabiesById(ids: List<UUID>): List<Baby>
fun findById(id: UUID): Baby
fun findAll(): List<Baby>
fun deleteBaby(baby: Baby)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ class BabyRepositoryAdapter(private val babyJpaRepository: BabyJpaRepository) :
override fun findAll(): List<Baby> {
return babyJpaRepository.findAll()
}

override fun deleteBaby(baby: Baby) {
babyJpaRepository.delete(baby)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package kr.co.vacgom.api.baby.application

import io.kotest.assertions.throwables.shouldNotThrow
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.mockk
import kr.co.vacgom.api.baby.domain.Baby
import kr.co.vacgom.api.baby.domain.enums.Gender
import kr.co.vacgom.api.baby.exceptioin.BabyError
import kr.co.vacgom.api.baby.presentation.dto.BabyDto
import kr.co.vacgom.api.baby.repository.BabyRepository
import kr.co.vacgom.api.babymanager.application.BabyManagerService
import kr.co.vacgom.api.global.exception.error.BusinessException
import kr.co.vacgom.api.global.util.UuidCreator
import kr.co.vacgom.api.user.application.UserQueryService
import java.time.LocalDate

Expand All @@ -22,6 +27,7 @@ class BabyCommandServiceTest : DescribeSpec({
babyRepository = babyRepositoryMock,
userQueryService = userQueryServiceMock,
)

describe("아이 정보 수정 테스트") {
context("아이가 존재하는 경우") {
it("정상적으로 정보 수정이 이루어진다.") {
Expand All @@ -43,6 +49,27 @@ class BabyCommandServiceTest : DescribeSpec({
}
}
}

describe("아이 삭제 테스트") {
context("아이가 존재하는 경우") {
it("정상적으로 아이가 삭제된다.") {
val userId = UuidCreator.create()
every { babyManagerServiceMock.getBabyByIdAndUserIsAdmin(userId, baby.id) } returns baby

shouldNotThrow<BusinessException> { sut.deleteById(userId, baby.id) }
}
}

context("아이가 존재하지 않는 경우") {
val userId = UuidCreator.create()
every { babyManagerServiceMock.getBabyByIdAndUserIsAdmin(userId, baby.id) } throws BusinessException(BabyError.BABY_NOT_FOUND)

val result = shouldThrow<BusinessException> { sut.deleteById(userId, baby.id) }

result.errorCode shouldBe BabyError.BABY_NOT_FOUND
result.message shouldBe BabyError.BABY_NOT_FOUND.message
}
}
}) {
companion object {
val baby = Baby(
Expand Down

0 comments on commit a85edc4

Please sign in to comment.