Skip to content

Commit

Permalink
[Feature/21] 받은 보틀 목록 조회 API를 구현한다 (#24)
Browse files Browse the repository at this point in the history
* refactor: BaseEntity를 global 패키지로 이동

* feat: 받은 보틀 목록 조회 API 구현

* refactor: 테이블 쿼리 TIMESTAMP를 DATETIME으로 변경

* chore: 날짜 타입 직렬화를 위한 JavaTimeModule 추가

* refactor: 리뷰 반영
  • Loading branch information
miseongk authored Jul 23, 2024
1 parent 9f6e52b commit 7b89cdc
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 17 deletions.
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 src/main/kotlin/com/nexters/bottles/bottle/domain/Bottle.kt
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 src/main/kotlin/com/nexters/bottles/bottle/facade/BottleFacade.kt
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 src/main/kotlin/com/nexters/bottles/bottle/facade/dto/BottleDto.kt
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
)
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>
)
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>
}
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())
}
}
7 changes: 4 additions & 3 deletions src/main/kotlin/com/nexters/bottles/config/JacksonConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.nexters.bottles.config

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.KotlinFeature
import com.fasterxml.jackson.module.kotlin.KotlinModule
import org.springframework.context.annotation.Bean
Expand All @@ -21,8 +22,8 @@ class JacksonConfig {

@Bean
fun objectMapper(): ObjectMapper {
return ObjectMapper().registerModule(
kotlinModule
)
return ObjectMapper()
.registerModule(kotlinModule)
.registerModule(JavaTimeModule())
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nexters.bottles.user.domain
package com.nexters.bottles.global

import org.hibernate.annotations.CreationTimestamp
import org.hibernate.annotations.UpdateTimestamp
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/com/nexters/bottles/user/domain/User.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nexters.bottles.user.domain

import com.nexters.bottles.global.BaseEntity
import com.nexters.bottles.user.domain.enum.Gender
import javax.persistence.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nexters.bottles.user.domain

import com.nexters.bottles.global.BaseEntity
import com.nexters.bottles.user.facade.dto.InterestDto
import com.nexters.bottles.user.facade.dto.RegionDto
import com.nexters.bottles.user.repository.converter.QuestionAndAnswerConverter
Expand Down
38 changes: 25 additions & 13 deletions src/main/resources/sql/table_query.sql
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
)

0 comments on commit 7b89cdc

Please sign in to comment.