Skip to content

Commit

Permalink
feat(store): getStoresByUserId, getStore api 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sm9171 committed Oct 9, 2023
1 parent d07378d commit 2b7aee9
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 17 deletions.
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)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hanghae.commerce.store.domain

import org.springframework.stereotype.Component
import java.util.*

@Component
class StoreReader(
Expand All @@ -9,4 +10,12 @@ class StoreReader(
fun countSameStoreName(name: String): Int {
return storeRepository.countSameStoreName(name)
}

fun findStoresByUserId(userId: String): List<Store> {
return storeRepository.findStoresByUserId(userId)
}

fun findStoreById(id: String): Store? {
return storeRepository.findStoreById(id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ interface StoreRepository {
fun save(store: Store): Store
fun countSameStoreName(name: String): Int
fun allDelete()
fun findStoreById(id: String): Store?
fun findStoresByUserId(userId: String): List<Store>
}
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)
}
}
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,
)
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,
)
}
}
}
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,
)
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,
)
}
}
}
}

This file was deleted.

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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ import org.springframework.data.jpa.repository.Query
interface JpaStoreRepository : JpaRepository<StoreEntity, String> {
@Query("select count (s.id) from StoreEntity s where s.name = :name")
fun countSameStoreName (name : String): Int

@Query("select s from StoreEntity s where s.userId = :userId")
fun findStoresByUserId(userId: String): List<StoreEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ data class StoreEntity(
) : PrimaryKeyEntity(identifier) {
fun toDomain(): Store {
return Store(
id = this.identifier,
id = this.id,
name = this.name,
userId = this.userId,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.hanghae.commerce.data.domain.store

import com.hanghae.commerce.store.domain.Store
import com.hanghae.commerce.store.domain.StoreRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Repository

@Repository
Expand All @@ -19,4 +20,13 @@ class StoreEntityRepository(
override fun allDelete() {
jpaStoreRepository.deleteAllInBatch()
}

override fun findStoreById(id: String): Store? {
return jpaStoreRepository.findByIdOrNull(id)?.toDomain()
}

override fun findStoresByUserId(userId: String): List<Store> {
val ㅇㅇ = jpaStoreRepository.findStoresByUserId(userId).get(0)
return jpaStoreRepository.findStoresByUserId(userId).map { storeEntity -> storeEntity.toDomain()}
}
}

0 comments on commit 2b7aee9

Please sign in to comment.