Skip to content

Commit

Permalink
Merge pull request #72 from GSM-MSG/70-feat/post-faq
Browse files Browse the repository at this point in the history
 70 �FAQ 등록 api
  • Loading branch information
JuuuuHong authored Oct 25, 2023
2 parents 4d93c9a + 9081db8 commit 5d12315
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package team.msg.domain.faq.mapper

import team.msg.domain.faq.presentation.data.request.CreateFaqRequest
import team.msg.domain.faq.presentation.web.CreateFaqWebRequest

interface FaqRequestMapper {
fun createFaqWebRequestToDto(createFaqWebRequest: CreateFaqWebRequest): CreateFaqRequest
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package team.msg.domain.faq.mapper

import org.springframework.stereotype.Component
import team.msg.domain.faq.presentation.data.request.CreateFaqRequest
import team.msg.domain.faq.presentation.web.CreateFaqWebRequest

@Component
class FaqRequestMapperImpl : FaqRequestMapper {

/**
* FAQ 등록 Web Request 를 애플리케이션 영역에서 사용될 Dto 로 매핑합니다.
*/
override fun createFaqWebRequestToDto(createFaqWebRequest: CreateFaqWebRequest): CreateFaqRequest =
CreateFaqRequest(
question = createFaqWebRequest.question,
answer = createFaqWebRequest.answer
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package team.msg.domain.faq.presentation

import javax.validation.Valid
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
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 team.msg.domain.faq.mapper.FaqRequestMapper
import team.msg.domain.faq.presentation.web.CreateFaqWebRequest
import team.msg.domain.faq.service.FaqService

@RestController
@RequestMapping("/FAQ")
class FaqController(
private val faqService: FaqService,
private val faqRequestMapper: FaqRequestMapper
) {
@PostMapping
fun createFaq(@RequestBody @Valid webRequest: CreateFaqWebRequest): ResponseEntity<Void> {
val request = faqRequestMapper.createFaqWebRequestToDto(webRequest)
faqService.createFaq(request)
return ResponseEntity.status(HttpStatus.CREATED).build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package team.msg.domain.faq.presentation.data.request

data class CreateFaqRequest(
val question: String,
val answer: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package team.msg.domain.faq.presentation.web

import javax.validation.constraints.NotBlank

data class CreateFaqWebRequest(
@field:NotBlank
val question: String,

@field:NotBlank
val answer: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package team.msg.domain.faq.service

import team.msg.domain.faq.presentation.data.request.CreateFaqRequest

interface FaqService {
fun createFaq(createFaqRequest: CreateFaqRequest)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package team.msg.domain.faq.service

import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import team.msg.common.util.UserUtil
import team.msg.domain.admin.exception.AdminNotFoundException
import team.msg.domain.admin.repository.AdminRepository
import team.msg.domain.faq.model.Faq
import team.msg.domain.faq.repository.FaqRepository
import team.msg.domain.faq.presentation.data.request.CreateFaqRequest

@Service
class FaqServiceImpl(
private val faqRepository: FaqRepository,
private val userUtil: UserUtil,
private val adminRepository: AdminRepository
) : FaqService {

/**
* FAQ 등록을 처리하는 비지니스 로직입니다.
* @param FAQ 등록을 처리하기 위한 request dto 입니다.
*/
@Transactional(rollbackFor = [Exception::class])
override fun createFaq(createFaqRequest: CreateFaqRequest) {
val user = userUtil.queryCurrentUser()
val admin = adminRepository.findByUser(user) ?: throw AdminNotFoundException("존재하지 않는 어드민입니다. info : [ userId = ${user.id} ]")

val faq = Faq(
question = createFaqRequest.question,
answer = createFaqRequest.answer,
admin = admin
)

faqRepository.save(faq)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class SecurityConfig(
.mvcMatchers(HttpMethod.PATCH, "/lecture/{id}/approve").hasAnyRole(ADMIN)
.mvcMatchers(HttpMethod.DELETE, "/lecture/{id}/reject").hasAnyRole(ADMIN)

// faq
.mvcMatchers(HttpMethod.POST, "/FAQ").hasRole(ADMIN)

.anyRequest().authenticated()
.and()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package team.msg.global.security.principal
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails
import team.msg.domain.user.enums.Authority
import java.util.UUID
import team.msg.domain.user.model.User

open class AuthDetails(
private val userId: UUID
private val user: User
) : UserDetails {
override fun getAuthorities(): MutableCollection<out GrantedAuthority> =
mutableListOf(SimpleGrantedAuthority(Authority.ROLE_USER.name))
mutableListOf(SimpleGrantedAuthority(user.authority.name))

override fun getPassword(): String? = null

override fun getUsername(): String = userId.toString()
override fun getUsername(): String = user.id.toString()

override fun isAccountNonExpired() = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class AuthDetailsService(
) : UserDetailsService {
override fun loadUserByUsername(username: String?): UserDetails {
val user = userRepository.findByIdOrNull(UUID.fromString(username)) ?: throw UserNotFoundException("존재하지 않는 유저입니다.")
return AuthDetails(user.id)
return AuthDetails(user)
}
}
10 changes: 5 additions & 5 deletions bitgouel-domain/src/main/kotlin/team/msg/domain/fAQ/model/FAQ.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team.msg.domain.fAQ.model
package team.msg.domain.faq.model

import javax.persistence.Column
import javax.persistence.Entity
Expand All @@ -11,16 +11,16 @@ import javax.persistence.ManyToOne
import team.msg.domain.admin.model.Admin

@Entity
class FAQ(
class Faq(

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long,
val id: Long = 0,

@Column(nullable = false)
@Column(nullable = false, columnDefinition = "TEXT")
val question: String,

@Column(nullable = false)
@Column(nullable = false, columnDefinition = "TEXT")
val answer: String,

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package team.msg.domain.fAQ.repository
package team.msg.domain.faq.repository

import org.springframework.data.repository.CrudRepository
import team.msg.domain.fAQ.model.FAQ
import team.msg.domain.faq.model.Faq
import java.util.*

interface FAQRepository : CrudRepository<FAQ, UUID> {
interface FaqRepository : CrudRepository<Faq, UUID> {
}

0 comments on commit 5d12315

Please sign in to comment.