Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ„ :: (Meogo-9) create review #10

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading