Skip to content

Commit

Permalink
✨ 유실물 상태 업데이트 API 기능 추가 (#296)
Browse files Browse the repository at this point in the history
* ✨ 유실물 상태 업데이트 API 추가 (#295)

* ♻️ import문 최적화 (#295)

* ♻️ command에서 status로 변경 (#295)

* ♻️ 유실물 상태 변경 시, Lost112 예외 처리 (#295)
  • Loading branch information
discphy authored Dec 19, 2024
1 parent 6a856de commit f3a18fd
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 3 deletions.
15 changes: 15 additions & 0 deletions application/src/docs/asciidoc/lost/posts.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ include::{snippets}/update-lost-post/http-response.adoc[]
.Response Fields
include::{snippets}/update-lost-post/response-fields.adoc[]

==== 유실물 상태 업데이트

.Request
include::{snippets}/update-lost-post-status/http-request.adoc[]
.Request Headers
include::{snippets}/update-lost-post-status/request-headers.adoc[]
.Path Parameters
include::{snippets}/update-lost-post-status/path-parameters.adoc[]
.Request Fields
include::{snippets}/update-lost-post-status/request-fields.adoc[]
.Response
include::{snippets}/update-lost-post-status/http-response.adoc[]
.Response Fields
include::{snippets}/update-lost-post-status/response-fields.adoc[]

==== 유실물 삭제

.Request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ class LostPostController(
return CommonResponse.success(lostPostService.updateLostPost(request.toCommand(lostId, imageFiles)))
}

@Authentication
@PatchMapping("/v1/lost-posts/{lostId}/status")
fun updateLostPostStatus(
@PathVariable("lostId") lostId: Long,
@RequestBody request: UpdateLostPostStatusDto.Request
): CommonResponse<UpdateLostPostStatusDto.Response> {
return CommonResponse.success(lostPostService.updateLostPostStatus(request.toCommand(lostId)))
}

@Authentication
@DeleteMapping("/v1/lost-posts/{lostId}")
fun deleteLostPost(@PathVariable("lostId") lostId: Long): CommonResponse<DeleteLostPostDto.Response> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package backend.team.ahachul_backend.api.lost.adapter.web.`in`.dto

import backend.team.ahachul_backend.api.lost.application.service.command.`in`.UpdateLostPostStatusCommand
import backend.team.ahachul_backend.api.lost.domain.entity.LostPostEntity
import backend.team.ahachul_backend.api.lost.domain.model.LostStatus

class UpdateLostPostStatusDto {

data class Request(
val status: LostStatus
) {
fun toCommand(id: Long): UpdateLostPostStatusCommand {
return UpdateLostPostStatusCommand(
id = id,
status = status
)
}
}

data class Response(
val id: Long
) {
companion object {
fun from(entity: LostPostEntity): Response {
return Response(
id = entity.id
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backend.team.ahachul_backend.api.lost.application.port.`in`

import backend.team.ahachul_backend.api.lost.adapter.web.`in`.dto.*
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.UpdateLostPostStatusCommand
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.CreateLostPostCommand
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.SearchLostPostCommand
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.UpdateLostPostCommand
Expand All @@ -16,5 +17,7 @@ interface LostPostUseCase {

fun updateLostPost(command: UpdateLostPostCommand): UpdateLostPostDto.Response

fun updateLostPostStatus(command: UpdateLostPostStatusCommand): UpdateLostPostStatusDto.Response

fun deleteLostPost(id: Long): DeleteLostPostDto.Response
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import backend.team.ahachul_backend.api.lost.application.port.out.LostPostWriter
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.CreateLostPostCommand
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.SearchLostPostCommand
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.UpdateLostPostCommand
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.UpdateLostPostStatusCommand
import backend.team.ahachul_backend.api.lost.application.service.command.out.GetRecommendLostPostsCommand
import backend.team.ahachul_backend.api.lost.application.service.command.out.GetSliceLostPostsCommand
import backend.team.ahachul_backend.api.lost.domain.entity.CategoryEntity
Expand All @@ -20,7 +21,9 @@ import backend.team.ahachul_backend.api.member.application.port.out.MemberReader
import backend.team.ahachul_backend.common.domain.entity.SubwayLineEntity
import backend.team.ahachul_backend.common.dto.ImageDto
import backend.team.ahachul_backend.common.dto.PageInfoDto
import backend.team.ahachul_backend.common.exception.CommonException
import backend.team.ahachul_backend.common.persistence.SubwayLineReader
import backend.team.ahachul_backend.common.response.ResponseCode
import backend.team.ahachul_backend.common.utils.RequestUtils
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand Down Expand Up @@ -193,6 +196,21 @@ class LostPostService(
}
}

@Transactional
override fun updateLostPostStatus(command: UpdateLostPostStatusCommand): UpdateLostPostStatusDto.Response {
val memberId = RequestUtils.getAttribute("memberId")!!
val entity = lostPostReader.getLostPost(command.id)

if (entity.origin == LostOrigin.LOST112) {
throw CommonException(ResponseCode.BAD_REQUEST)
}

entity.checkMe(memberId)

entity.updateStatus(command.status)
return UpdateLostPostStatusDto.Response.from(entity)
}

@Transactional
override fun deleteLostPost(id: Long): DeleteLostPostDto.Response {
val memberId = RequestUtils.getAttribute("memberId")!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,51 @@ class LostPostControllerDocsTest: CommonDocsTestConfig() {
))
}

@Test
fun updateLostPostStatus() {
// given
val response = UpdateLostPostStatusDto.Response(
id = 1
)

given(lostPostUseCase.updateLostPostStatus(any()))
.willReturn(response)

val request = UpdateLostPostStatusDto.Request(
status = LostStatus.COMPLETE
)

//when
val result = mockMvc.perform(
patch("/v1/lost-posts/{lostId}/status", 1L)
.header("Authorization", "Bearer <Access Token>")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request))
.accept(MediaType.APPLICATION_JSON)
)

//then
result.andExpect(status().isOk)
.andDo(document("update-lost-post-status",
getDocsRequest(),
getDocsResponse(),
requestHeaders(
headerWithName("Authorization").description("엑세스 토큰")
),
pathParameters(
parameterWithName("lostId").description("유실물 아이디")
),
requestFields(
fieldWithPath("status").type(JsonFieldType.STRING).description("유실물 찾기 완료 상태").attributes(getFormatAttribute( "PROGRESS / COMPLETE"))
),
responseFields(
*commonResponseFields(),
fieldWithPath("result.id").type(JsonFieldType.NUMBER).description("수정한 유실물 아이디")
)
)
)
}

@Test
fun deleteLostPost() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import backend.team.ahachul_backend.api.lost.application.port.`in`.LostPostUseCa
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.CreateLostPostCommand
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.SearchLostPostCommand
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.UpdateLostPostCommand
import backend.team.ahachul_backend.api.lost.application.service.command.`in`.UpdateLostPostStatusCommand
import backend.team.ahachul_backend.api.lost.domain.entity.CategoryEntity
import backend.team.ahachul_backend.api.lost.domain.model.LostPostType
import backend.team.ahachul_backend.api.lost.domain.model.LostStatus
import backend.team.ahachul_backend.api.lost.domain.model.LostType
import backend.team.ahachul_backend.api.lost.domain.entity.LostPostEntity
import backend.team.ahachul_backend.api.lost.domain.model.*
import backend.team.ahachul_backend.api.member.adapter.web.out.MemberRepository
import backend.team.ahachul_backend.api.member.domain.entity.MemberEntity
import backend.team.ahachul_backend.api.member.domain.model.GenderType
Expand Down Expand Up @@ -212,6 +212,53 @@ class LostPostServiceTest(
.hasMessage(ResponseCode.INVALID_AUTH.message)
}

@Test
@DisplayName("유실물 상태 수정 테스트")
fun updateLostPostStatus() {
// given
val createCommand = createLostPostCommand(subwayLine.id, "내용", "휴대폰")
val entity = lostPostUseCase.createLostPost(createCommand)

val updateCommand = UpdateLostPostStatusCommand(
id = entity.id,
status = LostStatus.COMPLETE
)

// when
val response = lostPostUseCase.updateLostPostStatus(updateCommand)

// then
assertThat(response.id).isEqualTo(entity.id)
}

@Test
@DisplayName("유실물 상태 수정 Lost112 에러")
fun updateLostPostStatusError() {
// given
val entity = lostPostRepository.save(
LostPostEntity(
title = "제목",
content = "내용",
subwayLine = subwayLine,
lostType = LostType.LOST,
category = category,
origin = LostOrigin.LOST112
)
)

val updateCommand = UpdateLostPostStatusCommand(
id = entity.id,
status = LostStatus.COMPLETE
)

// when & then
assertThatThrownBy {
lostPostUseCase.updateLostPostStatus(updateCommand)
}
.isExactlyInstanceOf(CommonException::class.java)
.hasMessage(ResponseCode.BAD_REQUEST.message)
}

@Test
@DisplayName("유실물 삭제 테스트 - 권한이 있는 경우")
fun deleteLostPost() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package backend.team.ahachul_backend.api.lost.application.service.command.`in`

import backend.team.ahachul_backend.api.lost.domain.model.LostStatus

class UpdateLostPostStatusCommand (
val id: Long,
val status: LostStatus
)
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ class LostPostEntity(
category?.let { this.category = category }
}

fun updateStatus(status: LostStatus) {
this.status = status
}

fun delete() {
type = LostPostType.DELETED
}
Expand Down

0 comments on commit f3a18fd

Please sign in to comment.