Skip to content

Commit

Permalink
Merge pull request #10 from MEOGO-DSM/9-create-review
Browse files Browse the repository at this point in the history
🏄 :: (Meogo-9) create review
  • Loading branch information
meltapplee authored Sep 3, 2024
2 parents 5ed99c4 + 2260584 commit c1db107
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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?
)
Original file line number Diff line number Diff line change
@@ -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
)
)
}
}
23 changes: 23 additions & 0 deletions src/main/kotlin/org/meogo/global/config/FilterConfig.kt
Original file line number Diff line number Diff line change
@@ -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<DefaultSecurityFilterChain, HttpSecurity>() {

override fun configure(builder: HttpSecurity) {
builder.addFilterBefore(JwtTokenFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter::class.java)
builder.addFilterBefore(GlobalExceptionFilter(objectMapper), JwtTokenFilter::class.java)
}
}
11 changes: 10 additions & 1 deletion src/main/kotlin/org/meogo/global/config/SecurityConfig.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand All @@ -33,6 +39,9 @@ class SecurityConfig {
.exceptionHandling()
.authenticationEntryPoint(HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))

http
.apply(FilterConfig(objectMapper, jwtTokenProvider))

return http.build()
}

Expand Down

0 comments on commit c1db107

Please sign in to comment.