Skip to content

Commit

Permalink
Merge pull request #90 from dnd-side-project/fix/location-add
Browse files Browse the repository at this point in the history
[BLOOM-089] 위치등록시 위치명 중복 예외 추가
  • Loading branch information
Dompoo authored Aug 29, 2024
2 parents a32f981 + 938b883 commit 6d0453d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import io.swagger.v3.oas.annotations.tags.Tag
interface LocationApi {
@Operation(summary = "위치를 저장하는 API 입니다.")
@ApiResponse(responseCode = "200", description = "위치 저장 성공")
@ApiErrorResponse(errorType = ErrorType.BAD_REQUEST, description = "요청의 name이 null이거나 비어있을 때 에러입니다.")
@ApiErrorResponses(
[
ApiErrorResponse(ErrorType.BAD_REQUEST, "요청의 name이 null이거나 비어있을 때 에러입니다."),
ApiErrorResponse(ErrorType.LOCATION_COUNT_EXCEED, "위치의 개수가 3개가 넘었을 때 에러입니다."),
ApiErrorResponse(ErrorType.LOCATION_NAME_DUPLICATE, "위치의 이름이 중복되었을 때 에러입니다."),
],
)
fun saveLocation(
@RequestBody(description = "위치 저장 요청", required = true)
request: LocationSaveRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ class LocationService(
dto: LocationCreateDto,
user: User,
): LocationSaveResponse {
if (locationRepository.countByUser(user) >= MAX_LOCATION_LIMIT) {
throw BadRequestException(
ErrorType.LOCATION_COUNT_EXCEED,
)
}
val locations = locationRepository.findAllByUser(user)

validateLocationSizeNotExceed(locations.size)
validateLocationNameUnique(locations, dto.name)

val location = Location.createLocation(dto, user)

Expand Down Expand Up @@ -74,6 +73,22 @@ class LocationService(
}

companion object {
const val MAX_LOCATION_LIMIT = 3
private const val MAX_LOCATION_LIMIT = 3
}

private fun validateLocationNameUnique(
locations: List<Location>,
newLocationName: String,
) {
if (locations.any { location ->
location.name == newLocationName
}
) {
throw BadRequestException(ErrorType.LOCATION_NAME_DUPLICATE)
}
}

private fun validateLocationSizeNotExceed(locationsSize: Int) {
if (locationsSize >= MAX_LOCATION_LIMIT) throw BadRequestException(ErrorType.LOCATION_COUNT_EXCEED)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum class ErrorType(val status: HttpStatus, var message: String, val logLevel:

// Location
NOT_FOUND_LOCATION(HttpStatus.NOT_FOUND, "존재하지 않는 위치입니다.", LogLevel.DEBUG),
LOCATION_NAME_DUPLICATE(HttpStatus.NOT_FOUND, "이미 존재하는 위치명입니다.", LogLevel.DEBUG),
LOCATION_COUNT_EXCEED(HttpStatus.BAD_REQUEST, "위치는 최대 3개까지만 등록 가능합니다.", LogLevel.DEBUG),

// Auth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ interface LocationRepository : JpaRepository<Location, Long> {
id: Long,
user: User,
): Location?

fun countByUser(user: User): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import dnd11th.blooming.common.exception.NotFoundException
import dnd11th.blooming.domain.entity.Location
import dnd11th.blooming.domain.entity.user.AlarmTime
import dnd11th.blooming.domain.entity.user.User
import dnd11th.blooming.domain.repository.ImageRepository
import dnd11th.blooming.domain.repository.LocationRepository
import dnd11th.blooming.domain.repository.myplant.MyPlantRepository
import io.kotest.assertions.throwables.shouldThrow
Expand All @@ -23,7 +22,6 @@ class LocationServiceTest : DescribeSpec(
{
val locationRepository = mockk<LocationRepository>()
val myPlantRepository = mockk<MyPlantRepository>()
val imageRepository = mockk<ImageRepository>()

val locationService = LocationService(locationRepository, myPlantRepository)

Expand All @@ -36,8 +34,8 @@ class LocationServiceTest : DescribeSpec(
}

context("name이 전달되면") {
every { locationRepository.countByUser(USER) } returns
2
every { locationRepository.findAllByUser(USER) } returns
listOf(Location(name = LOCATION_NAME2))
val request =
LocationCreateDto(
name = "거실",
Expand All @@ -50,8 +48,12 @@ class LocationServiceTest : DescribeSpec(
}
}
context("위치를 4개 이상 저장하려고 하면") {
every { locationRepository.countByUser(USER) } returns
3
every { locationRepository.findAllByUser(USER) } returns
listOf(
Location("1"),
Location("2"),
Location("3"),
)
val request =
LocationCreateDto(
name = "거실",
Expand All @@ -66,6 +68,26 @@ class LocationServiceTest : DescribeSpec(
exception.message shouldBe "위치는 최대 3개까지만 등록 가능합니다."
}
}
context("중복된 위치명으로 저장하려고 하면") {
every { locationRepository.findAllByUser(USER) } returns
listOf(
Location(LOCATION_NAME),
Location(LOCATION_NAME2),
)
val request =
LocationCreateDto(
name = LOCATION_NAME,
)
it("에외가 발생해야 한다.") {
val exception =
shouldThrow<BadRequestException> {
locationService.saveLocation(request, USER)
}

exception.errorType shouldBe ErrorType.LOCATION_NAME_DUPLICATE
exception.message shouldBe "이미 존재하는 위치명입니다."
}
}
}

describe("위치 전체 조회") {
Expand Down

0 comments on commit 6d0453d

Please sign in to comment.