Skip to content

Commit

Permalink
Merge pull request #76 from GSM-MSG/75-feat/query-faq-detail
Browse files Browse the repository at this point in the history
# 75 FAQ 상세 조회
  • Loading branch information
JuuuuHong authored Oct 28, 2023
2 parents 990badb + 45e209e commit a54efca
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package team.msg.domain.faq.exception

import team.msg.domain.faq.exception.constant.FaqErrorCode
import team.msg.global.error.exception.BitgouelException

class FaqNotFoundException(
message: String
) : BitgouelException(message, FaqErrorCode.FAQ_NOT_FOUND.status)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package team.msg.domain.faq.exception.constant

enum class FaqErrorCode(
val status: Int
) {
FAQ_NOT_FOUND(404)
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
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.GetMapping
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 org.springframework.web.bind.annotation.*
import team.msg.domain.faq.mapper.FaqRequestMapper
import team.msg.domain.faq.presentation.data.response.QueryAllFaqsResponse
import team.msg.domain.faq.presentation.data.response.AllFaqResponse
import team.msg.domain.faq.presentation.data.response.FaqDetailsResponse
import team.msg.domain.faq.presentation.web.CreateFaqWebRequest
import team.msg.domain.faq.service.FaqService
import javax.validation.Valid

@RestController
@RequestMapping("/faq")
Expand All @@ -27,8 +24,14 @@ class FaqController(
}

@GetMapping
fun queryAllFaqs(): ResponseEntity<List<QueryAllFaqsResponse>> {
fun queryAllFaqs(): ResponseEntity<AllFaqResponse> {
val response = faqService.queryAllFaqs()
return ResponseEntity.ok(response)
}

@GetMapping("/{id}")
fun queryFaqDetails(@PathVariable id: Long): ResponseEntity<FaqDetailsResponse> {
val response = faqService.queryFaqDetails(id)
return ResponseEntity.ok(response)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package team.msg.domain.faq.presentation.data.response

import team.msg.domain.faq.model.Faq

data class FaqResponse(
val id: Long,
val question: String
) {
companion object {
fun listOf(faqs: List<Faq>): List<FaqResponse> = faqs.map {
FaqResponse(
id = it.id,
question = it.question
)
}

fun detailOf(faq: Faq) = FaqDetailsResponse(
id = faq.id,
question = faq.question,
answer = faq.answer
)
}
}

data class AllFaqResponse(
val faq: List<FaqResponse>
)

data class FaqDetailsResponse(
val id: Long,
val question: String,
val answer: String
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package team.msg.domain.faq.service

import team.msg.domain.faq.presentation.data.request.CreateFaqRequest
import team.msg.domain.faq.presentation.data.response.QueryAllFaqsResponse
import team.msg.domain.faq.presentation.data.response.AllFaqResponse
import team.msg.domain.faq.presentation.data.response.FaqDetailsResponse

interface FaqService {
fun createFaq(createFaqRequest: CreateFaqRequest)
fun queryAllFaqs(): List<QueryAllFaqsResponse>
fun queryAllFaqs(): AllFaqResponse
fun queryFaqDetails(id: Long): FaqDetailsResponse
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package team.msg.domain.faq.service

import org.springframework.data.repository.findByIdOrNull
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.exception.FaqNotFoundException
import team.msg.domain.faq.model.Faq
import team.msg.domain.faq.repository.FaqRepository
import team.msg.domain.faq.presentation.data.request.CreateFaqRequest
import team.msg.domain.faq.presentation.data.response.QueryAllFaqsResponse
import team.msg.domain.faq.presentation.data.response.AllFaqResponse
import team.msg.domain.faq.presentation.data.response.FaqDetailsResponse
import team.msg.domain.faq.presentation.data.response.FaqResponse
import team.msg.domain.faq.repository.FaqRepository

@Service
class FaqServiceImpl(
Expand All @@ -24,7 +28,9 @@ class FaqServiceImpl(
@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 admin = adminRepository.findByUser(user)
?: throw AdminNotFoundException("존재하지 않는 어드민입니다. info : [ userId = ${user.id} ]")

val faq = Faq(
question = createFaqRequest.question,
Expand All @@ -35,17 +41,28 @@ class FaqServiceImpl(
faqRepository.save(faq)
}

/**
* FAQ 전제 조회를 처리하는 비지니스 로직입니다.
*/
@Transactional(rollbackFor = [Exception::class], readOnly = true)
override fun queryAllFaqs(): List<QueryAllFaqsResponse> {
override fun queryAllFaqs(): AllFaqResponse {
val faqs = faqRepository.findAll()

val response = faqs.map {
QueryAllFaqsResponse(
id = it.id,
question = it.question
)
}
val response = AllFaqResponse(
FaqResponse.listOf(faqs)
)

return response
}
/**
* FAQ 상세 조회를 처리하는 비지니스 로직입니다.
* @param FAQ 를 상세 조회하기 위한 id 입니다.
*/
@Transactional(rollbackFor = [Exception::class], readOnly = true)
override fun queryFaqDetails(id: Long): FaqDetailsResponse {
val faq = faqRepository.findByIdOrNull(id)
?: throw FaqNotFoundException("존재하지 않는 faq 입니다. info : [ faqId = $id ]")

return FaqResponse.detailOf(faq)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class SecurityConfig(

// faq
.mvcMatchers(HttpMethod.POST, "/faq").hasRole(ADMIN)
.mvcMatchers(HttpMethod.GET, "/faq").authenticated()
.mvcMatchers(HttpMethod.GET, "/faq").permitAll()
.mvcMatchers(HttpMethod.GET, "/faq/{id}").permitAll()

.anyRequest().authenticated()
.and()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package team.msg.domain.faq.model

import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.FetchType
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne
import team.msg.domain.admin.model.Admin
import javax.persistence.*

@Entity
class Faq(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package team.msg.domain.faq.repository

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

interface FaqRepository : CrudRepository<Faq, UUID> {
interface FaqRepository : JpaRepository<Faq, Long> {
}

0 comments on commit a54efca

Please sign in to comment.