From 1a0aa2f1a62b4767ac6cd4374236071df7c1a8d9 Mon Sep 17 00:00:00 2001 From: soohyeon Date: Tue, 3 Sep 2024 17:02:22 +0900 Subject: [PATCH 1/3] add :: reviewRequest --- .../domain/review/presentation/dto/ReviewRequest.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/kotlin/org/meogo/domain/review/presentation/dto/ReviewRequest.kt diff --git a/src/main/kotlin/org/meogo/domain/review/presentation/dto/ReviewRequest.kt b/src/main/kotlin/org/meogo/domain/review/presentation/dto/ReviewRequest.kt new file mode 100644 index 0000000..86a0f94 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/review/presentation/dto/ReviewRequest.kt @@ -0,0 +1,11 @@ +package org.meogo.domain.review.presentation.dto + +import javax.validation.constraints.Size + +data class ReviewRequest( + @field:Size(min = 1, max = 300) + val content: String, + val schoolId: Int, + val star: Long, + val image: String? +) From 30f8fce05da91c4fb8a1f7611d455bb2a0b71c14 Mon Sep 17 00:00:00 2001 From: soohyeon Date: Tue, 3 Sep 2024 17:02:37 +0900 Subject: [PATCH 2/3] add :: createReview api --- .../review/presentation/ReviewController.kt | 16 ++++++++- .../review/service/CreateReviewService.kt | 33 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/org/meogo/domain/review/service/CreateReviewService.kt diff --git a/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt b/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt index 3266b95..8b28b4e 100644 --- a/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt +++ b/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt @@ -1,10 +1,24 @@ package org.meogo.domain.review.presentation import lombok.RequiredArgsConstructor +import org.meogo.domain.review.presentation.dto.ReviewRequest +import org.meogo.domain.review.service.CreateReviewService +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 @RequiredArgsConstructor @RequestMapping("/review") -class ReviewController() +class ReviewController( + private val createReviewService: CreateReviewService +) { + @PostMapping + fun create( + @Valid @RequestBody + request: ReviewRequest + ) = + createReviewService.execute(request) +} diff --git a/src/main/kotlin/org/meogo/domain/review/service/CreateReviewService.kt b/src/main/kotlin/org/meogo/domain/review/service/CreateReviewService.kt new file mode 100644 index 0000000..2493ca6 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/review/service/CreateReviewService.kt @@ -0,0 +1,33 @@ +package org.meogo.domain.review.service + +import org.meogo.domain.review.domain.Review +import org.meogo.domain.review.presentation.dto.ReviewRequest +import org.meogo.domain.review.repository.ReviewRepository +import org.meogo.domain.user.exception.UserNotFoundException +import org.meogo.domain.user.facade.UserFacade +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional +import java.time.LocalDateTime + +@Service +class CreateReviewService( + private val reviewRepository: ReviewRepository, + private val userFacade: UserFacade +) { + + @Transactional + fun execute(request: ReviewRequest) { + val user = userFacade.currentUser() ?: throw UserNotFoundException + + reviewRepository.save( + Review( + date = LocalDateTime.now(), + userId = user.id!!, + schoolId = request.schoolId, + star = request.star, + content = request.content, + picture = request.image + ) + ) + } +} From 22605846d1252d119f030def49b1bfe1648a5a60 Mon Sep 17 00:00:00 2001 From: soohyeon Date: Tue, 3 Sep 2024 17:02:53 +0900 Subject: [PATCH 3/3] add :: filterConfig --- .../org/meogo/global/config/FilterConfig.kt | 23 +++++++++++++++++++ .../org/meogo/global/config/SecurityConfig.kt | 11 ++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/org/meogo/global/config/FilterConfig.kt diff --git a/src/main/kotlin/org/meogo/global/config/FilterConfig.kt b/src/main/kotlin/org/meogo/global/config/FilterConfig.kt new file mode 100644 index 0000000..45c877b --- /dev/null +++ b/src/main/kotlin/org/meogo/global/config/FilterConfig.kt @@ -0,0 +1,23 @@ +package org.meogo.global.config + +import com.fasterxml.jackson.databind.ObjectMapper +import org.meogo.global.error.GlobalExceptionFilter +import org.meogo.global.jwt.JwtTokenFilter +import org.meogo.global.jwt.JwtTokenProvider +import org.springframework.context.annotation.Configuration +import org.springframework.security.config.annotation.SecurityConfigurerAdapter +import org.springframework.security.config.annotation.web.builders.HttpSecurity +import org.springframework.security.web.DefaultSecurityFilterChain +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter + +@Configuration +class FilterConfig( + private val objectMapper: ObjectMapper, + private val jwtTokenProvider: JwtTokenProvider +) : SecurityConfigurerAdapter() { + + override fun configure(builder: HttpSecurity) { + builder.addFilterBefore(JwtTokenFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter::class.java) + builder.addFilterBefore(GlobalExceptionFilter(objectMapper), JwtTokenFilter::class.java) + } +} diff --git a/src/main/kotlin/org/meogo/global/config/SecurityConfig.kt b/src/main/kotlin/org/meogo/global/config/SecurityConfig.kt index 1586a54..db886b3 100644 --- a/src/main/kotlin/org/meogo/global/config/SecurityConfig.kt +++ b/src/main/kotlin/org/meogo/global/config/SecurityConfig.kt @@ -1,5 +1,7 @@ package org.meogo.global.config +import com.fasterxml.jackson.databind.ObjectMapper +import org.meogo.global.jwt.JwtTokenProvider import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.http.HttpStatus @@ -10,10 +12,14 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder import org.springframework.security.web.SecurityFilterChain import org.springframework.security.web.authentication.HttpStatusEntryPoint import org.springframework.web.cors.CorsUtils +import javax.servlet.FilterConfig @Configuration @EnableWebSecurity -class SecurityConfig { +class SecurityConfig( + private val objectMapper: ObjectMapper, + private val jwtTokenProvider: JwtTokenProvider +) { @Bean protected fun filterChain(http: HttpSecurity): SecurityFilterChain { @@ -33,6 +39,9 @@ class SecurityConfig { .exceptionHandling() .authenticationEntryPoint(HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)) + http + .apply(FilterConfig(objectMapper, jwtTokenProvider)) + return http.build() }