-
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.
[Feature/21] 받은 보틀 목록 조회 API를 구현한다 (#24)
* refactor: BaseEntity를 global 패키지로 이동 * feat: 받은 보틀 목록 조회 API 구현 * refactor: 테이블 쿼리 TIMESTAMP를 DATETIME으로 변경 * chore: 날짜 타입 직렬화를 위한 JavaTimeModule 추가 * refactor: 리뷰 반영
- Loading branch information
Showing
12 changed files
with
170 additions
and
17 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/nexters/bottles/bottle/controller/BottleController.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.nexters.bottles.bottle.controller | ||
|
||
import com.nexters.bottles.bottle.facade.BottleFacade | ||
import com.nexters.bottles.bottle.facade.dto.BottleListResponseDto | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/bottles") | ||
class BottleController( | ||
private val bottleFacade: BottleFacade | ||
) { | ||
|
||
@GetMapping | ||
fun getBottlesList(): BottleListResponseDto { | ||
return bottleFacade.getBottles() | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/com/nexters/bottles/bottle/domain/Bottle.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,25 @@ | ||
package com.nexters.bottles.bottle.domain | ||
|
||
import com.nexters.bottles.global.BaseEntity | ||
import com.nexters.bottles.user.domain.User | ||
import java.time.LocalDateTime | ||
import javax.persistence.* | ||
|
||
@Entity | ||
class Bottle( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long? = null, | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "target_user_id") | ||
val targetUser: User, | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "source_user_id") | ||
val sourceUser: User, | ||
|
||
@Column | ||
val expiredAt: LocalDateTime = LocalDateTime.now().plusDays(1) | ||
) : BaseEntity() { | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/com/nexters/bottles/bottle/facade/BottleFacade.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,29 @@ | ||
package com.nexters.bottles.bottle.facade | ||
|
||
import com.nexters.bottles.bottle.facade.dto.BottleDto | ||
import com.nexters.bottles.bottle.facade.dto.BottleListResponseDto | ||
import com.nexters.bottles.bottle.service.BottleService | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class BottleFacade( | ||
private val bottleService: BottleService | ||
) { | ||
|
||
fun getBottles(): BottleListResponseDto { | ||
val bottles = bottleService.getBottles() | ||
|
||
return BottleListResponseDto( | ||
bottles.map { | ||
BottleDto( | ||
id = it.id!!, | ||
userName = it.sourceUser.name, | ||
age = 20, // TODO User에 age 추가된 후 수정 | ||
mbti = it.sourceUser.userProfile?.profileSelect?.mbti, | ||
keyword = it.sourceUser.userProfile?.profileSelect?.keyword, | ||
expiredAt = it.expiredAt | ||
) | ||
} | ||
) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/nexters/bottles/bottle/facade/dto/BottleDto.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.nexters.bottles.bottle.facade.dto | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat | ||
import java.time.LocalDateTime | ||
|
||
data class BottleDto( | ||
val id: Long, | ||
val userName: String?, | ||
val age: Int, | ||
val mbti: String?, | ||
val keyword: List<String>?, | ||
|
||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | ||
val expiredAt: LocalDateTime | ||
) |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/nexters/bottles/bottle/facade/dto/BottleListResponseDto.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.nexters.bottles.bottle.facade.dto | ||
|
||
data class BottleListResponseDto( | ||
val bottles: List<BottleDto> | ||
) |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/nexters/bottles/bottle/repository/BottleRepository.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.nexters.bottles.bottle.repository | ||
|
||
import com.nexters.bottles.bottle.domain.Bottle | ||
import com.nexters.bottles.user.domain.User | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Query | ||
import org.springframework.data.repository.query.Param | ||
import java.time.LocalDateTime | ||
|
||
interface BottleRepository : JpaRepository<Bottle, Long> { | ||
|
||
@Query( | ||
value = "SELECT b FROM Bottle b " + | ||
"JOIN FETCH b.sourceUser " + | ||
"WHERE b.targetUser = :targetUser AND b.expiredAt > :currentDateTime" | ||
) | ||
fun findByTargetUserAndNotExpired( | ||
@Param("targetUser") targetUser: User, | ||
@Param("currentDateTime") currentDateTime: LocalDateTime | ||
): List<Bottle> | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/nexters/bottles/bottle/service/BottleService.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,24 @@ | ||
package com.nexters.bottles.bottle.service | ||
|
||
import com.nexters.bottles.bottle.domain.Bottle | ||
import com.nexters.bottles.bottle.repository.BottleRepository | ||
import com.nexters.bottles.user.repository.UserRepository | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
import java.time.LocalDateTime | ||
|
||
@Service | ||
class BottleService( | ||
private val bottleRepository: BottleRepository, | ||
private val userRepository: UserRepository | ||
) { | ||
|
||
@Transactional(readOnly = true) | ||
fun getBottles(): List<Bottle> { | ||
// TODO User 회원 가입 기능 구현후 수정 | ||
val user = userRepository.findByIdOrNull(1L) ?: throw IllegalStateException("회원가입 상태를 문의해주세요") | ||
|
||
return bottleRepository.findByTargetUserAndNotExpired(user, LocalDateTime.now()) | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...nexters/bottles/user/domain/BaseEntity.kt → .../com/nexters/bottles/global/BaseEntity.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,30 @@ | ||
CREATE TABLE User ( | ||
id BIGINT AUTO_INCREMENT PRIMARY KEY, | ||
name VARCHAR(255) DEFAULT NULL, | ||
kakao_id VARCHAR(255) DEFAULT NULL, | ||
CREATE TABLE User | ||
( | ||
id BIGINT AUTO_INCREMENT PRIMARY KEY, | ||
name VARCHAR(255) DEFAULT NULL, | ||
kakao_id VARCHAR(255) DEFAULT NULL, | ||
phone_number VARCHAR(255) DEFAULT NULL, | ||
gender VARCHAR(10) DEFAULT 'MALE', | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | ||
gender VARCHAR(10) DEFAULT 'MALE', | ||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, | ||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | ||
); | ||
|
||
CREATE TABLE user_profile ( | ||
id BIGINT AUTO_INCREMENT PRIMARY KEY, | ||
user_id BIGINT NOT NULL, | ||
CREATE TABLE user_profile | ||
( | ||
id BIGINT AUTO_INCREMENT PRIMARY KEY, | ||
user_id BIGINT NOT NULL, | ||
profile_select JSON, | ||
introduction JSON, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL | ||
introduction JSON, | ||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL | ||
); | ||
|
||
CREATE TABLE bottle | ||
( | ||
id BIGINT AUTO_INCREMENT PRIMARY KEY, | ||
target_user_id BIGINT NOT NULL, | ||
source_user_id BIGINT NOT NULL, | ||
expired_at DATETIME NOT NULL, | ||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL | ||
) |