-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): getStoresByUserId, getStore api 추가
- Loading branch information
Showing
13 changed files
with
228 additions
and
17 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
commerce-api/src/main/kotlin/com/hanghae/commerce/store/application/StoreReadService.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,27 @@ | ||
package com.hanghae.commerce.store.application | ||
|
||
import com.hanghae.commerce.store.domain.StoreReader | ||
import com.hanghae.commerce.store.presentation.dto.GetStoreResponse | ||
import com.hanghae.commerce.store.presentation.dto.GetStoresByUserIdResponse | ||
import com.hanghae.commerce.user.domain.UserReader | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
class StoreReadService ( | ||
private val storeReader: StoreReader, | ||
private val userReader: UserReader, | ||
){ | ||
@Transactional(readOnly = true) | ||
fun getStoresByUserId(userId: String): List<GetStoresByUserIdResponse> { | ||
val user = userReader.findById(userId) ?: throw IllegalArgumentException() | ||
val stores = storeReader.findStoresByUserId(user.id) | ||
return GetStoresByUserIdResponse.listOf(stores) | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
fun getStore(storeId: String): GetStoreResponse { | ||
val store = storeReader.findStoreById(storeId) ?: throw IllegalArgumentException() | ||
return GetStoreResponse.of(store) | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
commerce-api/src/main/kotlin/com/hanghae/commerce/store/presentation/StoreController.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,27 @@ | ||
package com.hanghae.commerce.store.presentation | ||
|
||
import com.hanghae.commerce.store.application.StoreCreateService | ||
import com.hanghae.commerce.store.application.StoreReadService | ||
import com.hanghae.commerce.store.presentation.dto.* | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RestController | ||
class StoreController ( | ||
private val storeCreateService: StoreCreateService, | ||
private val storeReadService: StoreReadService, | ||
){ | ||
@PostMapping("/store") | ||
fun createStore(@RequestBody request: CreateStoreRequest) : CreateStoreResponse { | ||
return storeCreateService.createStore(request) | ||
} | ||
|
||
@GetMapping("/store") | ||
fun getStore(@RequestBody request: GetStoreRequest) : GetStoreResponse { | ||
return storeReadService.getStore(request.storeId) | ||
} | ||
|
||
@GetMapping("/stores") | ||
fun getStoresByUserId(@RequestBody request: GetStoresByUserIdRequest) : List<GetStoresByUserIdResponse> { | ||
return storeReadService.getStoresByUserId(request.userId) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
commerce-api/src/main/kotlin/com/hanghae/commerce/store/presentation/dto/GetStoreRequest.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,5 @@ | ||
package com.hanghae.commerce.store.presentation.dto | ||
|
||
data class GetStoreRequest ( | ||
val storeId: String, | ||
) |
19 changes: 19 additions & 0 deletions
19
commerce-api/src/main/kotlin/com/hanghae/commerce/store/presentation/dto/GetStoreResponse.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,19 @@ | ||
package com.hanghae.commerce.store.presentation.dto | ||
|
||
import com.hanghae.commerce.store.domain.Store | ||
|
||
data class GetStoreResponse ( | ||
val id: String, | ||
val name: String, | ||
val userId: String, | ||
){ | ||
companion object { | ||
fun of(store: Store): GetStoreResponse { | ||
return GetStoreResponse( | ||
id = store.id, | ||
name = store.name, | ||
userId = store.userId, | ||
) | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...i/src/main/kotlin/com/hanghae/commerce/store/presentation/dto/GetStoresByUserIdRequest.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,5 @@ | ||
package com.hanghae.commerce.store.presentation.dto | ||
|
||
data class GetStoresByUserIdRequest( | ||
val userId: String, | ||
) |
21 changes: 21 additions & 0 deletions
21
.../src/main/kotlin/com/hanghae/commerce/store/presentation/dto/GetStoresByUserIdResponse.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,21 @@ | ||
package com.hanghae.commerce.store.presentation.dto | ||
|
||
import com.hanghae.commerce.store.domain.Store | ||
|
||
data class GetStoresByUserIdResponse ( | ||
val id: String, | ||
val name: String, | ||
val userId: String, | ||
){ | ||
companion object { | ||
fun listOf(stores: List<Store>): List<GetStoresByUserIdResponse> { | ||
return stores.map { store -> | ||
GetStoresByUserIdResponse( | ||
id = store.id, | ||
name = store.name, | ||
userId = store.userId, | ||
) | ||
} | ||
} | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
commerce-api/src/main/kotlin/com/hanghae/commerce/store/presentation/dto/StoreController.kt
This file was deleted.
Oops, something went wrong.
99 changes: 99 additions & 0 deletions
99
commerce-api/src/test/kotlin/com/hanghae/commerce/store/application/StoreReadServiceTest.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,99 @@ | ||
package com.hanghae.commerce.store.application | ||
|
||
import com.hanghae.commerce.store.domain.Store | ||
import com.hanghae.commerce.store.domain.StoreWriter | ||
import com.hanghae.commerce.testconfiguration.IntegrationTest | ||
import com.hanghae.commerce.user.domain.User | ||
import com.hanghae.commerce.user.domain.UserType | ||
import com.hanghae.commerce.user.domain.UserWriter | ||
import org.assertj.core.api.Assertions | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
|
||
@IntegrationTest | ||
class StoreReadServiceTest ( | ||
@Autowired | ||
private val storeReadService: StoreReadService, | ||
@Autowired | ||
private val storeWriter: StoreWriter, | ||
@Autowired | ||
private val userWriter: UserWriter, | ||
|
||
){ | ||
|
||
@AfterEach | ||
fun tearDown() { | ||
userWriter.allDelete() | ||
storeWriter.allDelete() | ||
} | ||
|
||
@Test | ||
fun getStoresByUserId() { | ||
|
||
val user = userWriter.save( | ||
User( | ||
id = "1", | ||
name = "sangmin", | ||
age = 20, | ||
email = "hanghae@gmail.com", | ||
address = "seoul", | ||
userType = UserType.SELLER, | ||
) | ||
) | ||
|
||
storeWriter.save( | ||
Store( | ||
id = "1", | ||
name = "상점1", | ||
"1" | ||
) | ||
) | ||
|
||
storeWriter.save( | ||
Store( | ||
id = "2", | ||
name = "상점2", | ||
"1" | ||
) | ||
) | ||
val results = storeReadService.getStoresByUserId(user.id) | ||
|
||
Assertions.assertThat(results).hasSize(2) | ||
Assertions.assertThat(results).extracting("id") | ||
.containsExactlyInAnyOrder("1", "2") | ||
Assertions.assertThat(results).extracting("name") | ||
.containsExactlyInAnyOrder("상점1", "상점2") | ||
Assertions.assertThat(results).extracting("userId") | ||
.containsExactlyInAnyOrder("1", "1") | ||
} | ||
|
||
@Test | ||
fun getStore() { | ||
|
||
userWriter.save( | ||
User( | ||
id = "1", | ||
name = "sangmin", | ||
age = 20, | ||
email = "hanghae@gmail.com", | ||
address = "seoul", | ||
userType = UserType.SELLER, | ||
) | ||
) | ||
|
||
val store = storeWriter.save( | ||
Store( | ||
id = "1", | ||
name = "상점1", | ||
"1" | ||
) | ||
) | ||
|
||
val result = storeReadService.getStore(store.id) | ||
|
||
Assertions.assertThat(result.id).isEqualTo("1") | ||
Assertions.assertThat(result.name).isEqualTo("상점1") | ||
Assertions.assertThat(result.userId).isEqualTo("1") | ||
} | ||
} |
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