-
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.
Merge pull request #77 from le2sky/feat/71
[기능 구현] 신규 가게 등록 기능 구현(issue#71)
- Loading branch information
Showing
40 changed files
with
694 additions
and
54 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
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
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
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
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
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
26 changes: 26 additions & 0 deletions
26
mealkitary-api/src/main/kotlin/com/mealkitary/shop/web/RegisterShopController.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,26 @@ | ||
package com.mealkitary.shop.web | ||
|
||
import com.mealkitary.common.utils.HttpResponseUtils | ||
import com.mealkitary.shop.application.port.input.RegisterShopUseCase | ||
import com.mealkitary.shop.web.request.RegisterShopWebRequest | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import javax.validation.Valid | ||
|
||
@RestController | ||
@RequestMapping("/shops") | ||
class RegisterShopController( | ||
private val registerShopUseCase: RegisterShopUseCase | ||
) { | ||
|
||
@PostMapping | ||
fun registerShop(@Valid @RequestBody registerShopWebRequest: RegisterShopWebRequest): ResponseEntity<Unit> { | ||
val resourceId = registerShopUseCase.register(registerShopWebRequest.mapToServiceRequest()) | ||
val location = HttpResponseUtils.createResourceUri(resourceId) | ||
|
||
return ResponseEntity.created(location).build() | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
mealkitary-api/src/main/kotlin/com/mealkitary/shop/web/request/RegisterShopWebRequest.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,15 @@ | ||
package com.mealkitary.shop.web.request | ||
|
||
import com.mealkitary.shop.application.port.input.RegisterShopRequest | ||
import javax.validation.constraints.NotBlank | ||
|
||
data class RegisterShopWebRequest( | ||
@field:NotBlank(message = "등록 대상 가게의 이름은 필수입니다.") | ||
val title: String? = null, | ||
|
||
@field:NotBlank(message = "사업자 번호는 필수입니다.") | ||
val brn: String? = null | ||
) { | ||
|
||
fun mapToServiceRequest() = RegisterShopRequest(title!!, brn!!) | ||
} |
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
55 changes: 55 additions & 0 deletions
55
mealkitary-api/src/test/kotlin/com/docs/shop/RegisterShopControllerDocsTest.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,55 @@ | ||
package com.docs.shop | ||
|
||
import com.docs.RestDocsSupport | ||
import com.mealkitary.shop.application.port.input.RegisterShopUseCase | ||
import com.mealkitary.shop.web.RegisterShopController | ||
import com.mealkitary.shop.web.request.RegisterShopWebRequest | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import org.springframework.http.MediaType | ||
import org.springframework.restdocs.headers.HeaderDocumentation.headerWithName | ||
import org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders | ||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation | ||
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders | ||
import org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest | ||
import org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse | ||
import org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint | ||
import org.springframework.restdocs.payload.JsonFieldType | ||
import org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath | ||
import org.springframework.restdocs.payload.PayloadDocumentation.requestFields | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.header | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
|
||
class RegisterShopControllerDocsTest : RestDocsSupport() { | ||
|
||
private val registerShopUseCase = mockk<RegisterShopUseCase>() | ||
|
||
@Test | ||
fun `api docs test - registerShop`() { | ||
every { registerShopUseCase.register(any()) } answers { 1L } | ||
|
||
val registerShopWebRequest = RegisterShopWebRequest("집밥뚝딱 안양점", "123-23-12345") | ||
|
||
mvc.perform( | ||
RestDocumentationRequestBuilders.post("/shops") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(registerShopWebRequest)) | ||
) | ||
.andExpect(status().isCreated) | ||
.andExpect(header().string("Location", "http://localhost/shops/1")) | ||
.andDo( | ||
MockMvcRestDocumentation.document( | ||
"shop-post", | ||
preprocessRequest(prettyPrint()), | ||
preprocessResponse(prettyPrint()), | ||
requestFields( | ||
fieldWithPath("title").type(JsonFieldType.STRING).description("등록 대상 가게 이름"), | ||
fieldWithPath("brn").type(JsonFieldType.STRING).description("사업자 번호"), | ||
), | ||
responseHeaders(headerWithName("Location").description("생성된 가게 리소스 URI")), | ||
) | ||
) | ||
} | ||
|
||
override fun initController() = RegisterShopController(registerShopUseCase) | ||
} |
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
72 changes: 72 additions & 0 deletions
72
mealkitary-api/src/test/kotlin/com/mealkitary/shop/web/RegisterShopControllerTest.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,72 @@ | ||
package com.mealkitary.shop.web | ||
|
||
import com.mealkitary.WebIntegrationTestSupport | ||
import com.mealkitary.shop.web.request.RegisterShopWebRequest | ||
import io.mockk.every | ||
import org.springframework.http.MediaType | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.header | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
|
||
class RegisterShopControllerTest : WebIntegrationTestSupport() { | ||
|
||
@Test | ||
fun `api integration test - registerShop`() { | ||
every { registerShopUseCase.register(any()) } answers { 1L } | ||
|
||
val registerShopWebRequest = RegisterShopWebRequest("집밥뚝딱 안양점", "123-23-12345") | ||
|
||
mvc.perform( | ||
MockMvcRequestBuilders.post("/shops") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(registerShopWebRequest)) | ||
) | ||
.andExpect(status().isCreated) | ||
.andExpect(header().string("Location", "http://localhost/shops/1")) | ||
} | ||
|
||
@Test | ||
fun `api integration test - 가게 이름이 누락된 경우 400 에러를 발생한다`() { | ||
val registerShopWebRequest = RegisterShopWebRequest(brn = "123-23-12345") | ||
|
||
mvc.perform( | ||
MockMvcRequestBuilders.post("/shops") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(registerShopWebRequest)) | ||
) | ||
.andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.status").value("400")) | ||
.andExpect(jsonPath("$.message").value("잘못된 입력값입니다.")) | ||
.andExpect(jsonPath("$..errors[0].field").value("title")) | ||
.andExpect(jsonPath("$..errors[0].reason").value("등록 대상 가게의 이름은 필수입니다.")) | ||
} | ||
|
||
@Test | ||
fun `api integration test - 사업자 번호가 누락된 경우 400 에러를 발생한다`() { | ||
val registerShopWebRequest = RegisterShopWebRequest(title = "집밥뚝딱 안양점") | ||
|
||
mvc.perform( | ||
MockMvcRequestBuilders.post("/shops") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(registerShopWebRequest)) | ||
) | ||
.andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.status").value("400")) | ||
.andExpect(jsonPath("$.message").value("잘못된 입력값입니다.")) | ||
.andExpect(jsonPath("$..errors[0].field").value("brn")) | ||
.andExpect(jsonPath("$..errors[0].reason").value("사업자 번호는 필수입니다.")) | ||
} | ||
|
||
@Test | ||
fun `api integration test - JSON 형식이 아닌 경우 400 에러가 발생한다`() { | ||
mvc.perform( | ||
MockMvcRequestBuilders.post("/shops") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString("}{")) | ||
) | ||
.andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.status").value("400")) | ||
.andExpect(jsonPath("$.message").value("JSON 형식이 잘못되었습니다.")) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...ication/src/main/kotlin/com/mealkitary/shop/application/port/input/RegisterShopRequest.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.mealkitary.shop.application.port.input | ||
|
||
data class RegisterShopRequest( | ||
val title: String, | ||
val brn: String | ||
) |
6 changes: 6 additions & 0 deletions
6
...ication/src/main/kotlin/com/mealkitary/shop/application/port/input/RegisterShopUseCase.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.mealkitary.shop.application.port.input | ||
|
||
interface RegisterShopUseCase { | ||
|
||
fun register(registerShopRequest: RegisterShopRequest): Long | ||
} |
8 changes: 8 additions & 0 deletions
8
...y-application/src/main/kotlin/com/mealkitary/shop/application/port/output/SaveShopPort.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.mealkitary.shop.application.port.output | ||
|
||
import com.mealkitary.shop.domain.shop.Shop | ||
|
||
interface SaveShopPort { | ||
|
||
fun saveOne(shop: Shop): Long | ||
} |
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
26 changes: 26 additions & 0 deletions
26
...pplication/src/main/kotlin/com/mealkitary/shop/application/service/RegisterShopService.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,26 @@ | ||
package com.mealkitary.shop.application.service | ||
|
||
import com.mealkitary.shop.application.port.input.RegisterShopRequest | ||
import com.mealkitary.shop.application.port.input.RegisterShopUseCase | ||
import com.mealkitary.shop.application.port.output.SaveShopPort | ||
import com.mealkitary.shop.domain.shop.factory.ShopBusinessNumberValidator | ||
import com.mealkitary.shop.domain.shop.factory.ShopFactory | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
class RegisterShopService( | ||
private val saveShopPort: SaveShopPort, | ||
shopBusinessNumberValidator: ShopBusinessNumberValidator | ||
) : RegisterShopUseCase { | ||
|
||
private val shopFactory = ShopFactory(shopBusinessNumberValidator) | ||
|
||
@Transactional | ||
override fun register(registerShopRequest: RegisterShopRequest): Long { | ||
val shop = shopFactory.createOne(registerShopRequest.title, registerShopRequest.brn) | ||
|
||
return saveShopPort.saveOne(shop) | ||
} | ||
} |
Oops, something went wrong.